Zlacz takie same przyciski podczas naprawiania.
authorŁukasz Rekucki <lrekucki@gmail.com>
Tue, 8 Sep 2009 17:34:14 +0000 (19:34 +0200)
committerŁukasz Rekucki <lrekucki@gmail.com>
Tue, 8 Sep 2009 17:34:14 +0000 (19:34 +0200)
apps/toolbar/management/commands/fixbuttons.py
dump_toolbar.sh

index 2f15525..627ef25 100755 (executable)
@@ -10,26 +10,49 @@ class Command(NoArgsCommand):
     
     def handle_noargs(self, **options):
         buttons = Button.objects.all()
+        print "Validating parameters... "
         for b in buttons:
             params = b.params;
             try:
-                v = json.loads(b.params)
-               
+                v = json.loads(b.params)               
             except ValueError, e:
-                print 'On button %s: ' % b.label, b.params
-                print e
-                # try to fix the bad json
-                
-                # cut the parenthis
+                print 'Trying to fix button "%s" ...' % b.slug
                 if params[0] == u'(':
                     params = params[1:]
                 if params[-1] == u')':
                     params = params[:-1]
+                try:
+                    v = son.loads(re.sub(u'([\\w-]+)\\s*:', u'"\\1": ', params).encode('utf-8'))
+                except ValueError, e:
+                    print "Unable to fix '%s' " % b.params
+                    print "Try to fix this button manually and rerun the script."
+                    return False
 
-                v = json.loads(re.sub(u'([\\w-]+)\\s*:', u'"\\1": ', params).encode('utf-8'))
+            # resave
             b.params = json.dumps(v)
             b.save()
 
+        print "Merge duplicate buttons (if any)..."
+        hash = {}
+        for b in buttons:
+            if b.slug not in hash:
+                hash[b.slug] = b
+                continue
+                
+            # duplicate button
+            print "Found duplicate of '%s'" % b.slug
+            a = hash[b.slug]
+
+            remove_duplicate = True
+            if a.params != b.params:
+                print "Conflicting params for duplicate of '%s'." % b.slug
+                print "Groups will be joined, but won't remove duplicates."
+                remove_duplicate = False
 
+            for g in b.group.all():
+                a.group.add(g)
 
-    
+            b.group.clear()
+
+            a.save()
+            b.delete()
index 58898ee..6dcbbe0 100755 (executable)
@@ -1 +1 @@
-./project/manage.py dumpdata --format=xml toolbar | xmllint --format - > fixtures/przyciski.xml.new
+./project/manage.py dumpdata --format=xml toolbar | xmllint --format - > fixtures/przyciski.new.xml