Steve Losh avatar Steve Losh committed 56ede49

Support booleans as page data. Ugh.

Comments (0)

Files changed (2)

         obj.save()
 
         if not form.ignore_content:
-            content_data = [(k.split('_', 1)[1], v)
-                            for k, v in form.data.items()
-                            if k.startswith('content_')]
+            # Use the authoritative list of fields, because browsers won't send along
+            # boolean fields that are unchecked (False) at all.
 
-            for title, content in content_data:
-                pc = PageContent.objects.get(page=obj, cleaned_title=title)
-                pc.content = content
+            # This is ugly, but we have to do it.
+            #
+            # fields: list of (clean_field_title, field_type) pairs
+            # fieldnames: list of clean_field_title
+            # fieldtypes: dict of clean_field_title -> field_type pairs
+
+            fields = [(clean_field_title(f[0]), f[1])
+                      for f in stemplates.get_fields_bare(obj.template)]
+            fieldnames = [f[0] for f in fields]
+            fieldtypes = dict(fields)
+
+            # content is going to be a dict of
+            # clean_field_title -> cleaned data pairs
+
+            # Default to 0, because browsers won't even send along a field for
+            # a checkbox element that's unchecked.
+            content = dict([(f, 0) for f in fieldnames])
+
+            # Now update content with the appropriate data from the form.
+            for k, v in form.data.items():
+                if k.startswith('content_'):
+                    fieldname = k.split('_', 1)[1]
+                    fieldtype = fieldtypes[fieldname]
+
+                    if fieldtype == 'bool':
+                        v = 1
+
+                    content[fieldname] = v
+
+            for k, v in content.items():
+                pc = PageContent.objects.get(page=obj, cleaned_title=k)
+                pc.content = v
                 pc.save()
 
 
         else:
             initial = {}
             for pc in page.pagecontent_set.all():
-                initial['content_' + clean_field_title(pc.title)] = pc.content
+                # Ugly hack to store booleans as text.
+                if pc.typ == 'bool':
+                    val = True if int(pc.content) else False
+                else:
+                    val = pc.content
+
+                initial['content_' + clean_field_title(pc.title)] = val
 
             content_form = stoat_forms.get_content_form(page.template, initial=initial)
 
 
 CONTENT_TYPES = (
     ('char', 'char'),
+    ('bool', 'bool'),
     ('text', 'text'),
     ('ckeditor', 'ckeditor'),
     ('img', 'img'),
                 return model.objects.get(id=self.content)
             except model.DoesNotExist:
                 return None
+        elif self.typ == 'bool':
+            return True if int(self.content) else False
         else:
             return self.content
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.