def change_view(self, request, object_id, extra_context=None): extra_context = extra_context or {} page = get_object_or_404(Page, id=object_id) parent = page.get_parent() parent = parent.id if parent else '' if request.POST: template = request.POST.get('template', page.template) content_form = stoat_forms.get_content_form(template, request.POST) else: initial = {} for pc in page.pagecontent_set.all(): # Ugly hack to store booleans as text. if pc.typ == 'bool': try: val = True if int(pc.content) else False except ValueError: val = True else: val = pc.get_content() initial['content_' + clean_field_title(pc.title)] = val content_form = stoat_forms.get_content_form(page.template, initial=initial) extra_context.update({ 'content_form': content_form, 'media': content_form.media + self.media, 'parent': parent, }) return self._django_change_view(request, object_id, extra_context=extra_context)
def change_view(self, request, object_id, extra_context=None): extra_context = extra_context or {} page = get_object_or_404(Page, id=object_id) parent = page.get_parent() parent = parent.id if parent else '' if request.POST: template = request.POST.get('template', page.template) content_form = stoat_forms.get_content_form(template, request.POST) else: initial = {} for pc in page.pagecontent_set.all(): # 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) extra_context.update({ 'content_form': content_form, 'media': content_form.media + self.media, 'parent': parent, }) return self._django_change_view(request, object_id, extra_context=extra_context)
def get_content_form(tname, data=None, initial=None): fs = get_fields(tname) if data: data = dict((k, v) for k, v in data.items() if k.startswith('content_')) form = PageContentForm(data) elif initial: form = PageContentForm(initial=initial) else: form = PageContentForm() for title, typ, options in fs: f = _get_field(typ, title, options) if f: form.fields['content_' + clean_field_title(title)] = f return form
def get_content_form(tname, data=None, initial=None): fs = get_fields(tname) if data: data = dict( (k, v) for k, v in data.items() if k.startswith('content_')) form = PageContentForm(data) elif initial: form = PageContentForm(initial=initial) else: form = PageContentForm() for title, typ, options in fs: f = _get_field(typ, title, options) if f: form.fields['content_' + clean_field_title(title)] = f return form
def save_model(self, request, obj, form, change): obj.save() if not form.ignore_content: # Use the authoritative list of fields, because browsers won't send along # boolean fields that are unchecked (False) at all. # 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()