예제 #1
0
    def __init__(self, parent, node=None, attach=False, enlarge=True, reserved=(), skip_slug=False, *args, **kwargs):
        """
            Django will put the extra slug field at the bottom, below
            all model fields. I want it just after the title field
        """
        super(BaseForm, self).__init__(*args, **kwargs)
        slug = self.fields.pop("slug")
        titlepos = self.fields.keyOrder.index("title")
        self.fields.insert(titlepos + 1, "slug", slug)
        self.node = node
        self.parent = parent
        self.attach = attach
        self.reserved = reserved
        self.advanced_fields = self.initial_advanced_fields

        if attach:
            self.fields.pop("slug")

        templates = template_registry.get(self._meta.model, [])
        if templates:
            self.fields["template"] = forms.ChoiceField(choices=templates, required=False)
        else:
            self.fields.pop("template")  ## will default to content_view

        self.fields["state"] = forms.ChoiceField(
            choices=self.workflow_choices(), initial=self.workflow_default(), required=False
        )
        if skip_slug:
            self.fields.pop("slug")

        if enlarge:
            for enlargable_field in self.fields.values():
                self.enlarge_field(enlargable_field)

        ## make the description textarea a bit smaller
        if "description" in self.fields:
            self.fields["description"].widget.attrs["rows"] = 4

        if "tags" in self.fields:
            self.fields["tags"].widget.attrs["class"] = "tagManager"
            self.fields["tags"].required = False

        ## lightforms don't have a language field
        if "language" in self.fields:
            if self.node:
                ## construct allowed languages. Exclude any language for which
                ## the node already has content
                current = self.instance.language if self.instance else None
                c = []
                for lpair in translate.languages():
                    if current == lpair[0] or not self.node.content(language=lpair[0], fallback=False):
                        c.append(lpair)
                self.fields["language"].choices = c
            else:
                self.fields["language"].choices = translate.languages()

        for e in type_registry.extenders(self.Meta.model):
            e.extend_form(self, *args, **kwargs)
예제 #2
0
    def save(self, commit=True):
        i = super(BaseForm, self).save(commit=False)

        for e in type_registry.extenders(self.Meta.model):
            e.extend_save(self, i, commit)

        if commit:
            i.save()
            self.save_m2m()

        return i
예제 #3
0
    def __init__(self, parent, node=None, attach=False, enlarge=True,
                 reserved=(),
                 skip_slug=False, *args, **kwargs):
        """
            Django will put the extra slug field at the bottom, below
            all model fields. I want it just after the title field
        """
        super(BaseForm, self).__init__(*args, **kwargs)
        slug = self.fields.pop('slug')
        titlepos = self.fields.keyOrder.index('title')
        self.fields.insert(titlepos+1, 'slug', slug)
        self.node = node
        self.parent = parent
        self.attach = attach
        self.reserved = reserved
        self.advanced_fields = self.initial_advanced_fields

        ## Pass parent to ParentFields
        for field in self.fields.values():
            if isinstance(field, ParentField):
                field.parent = parent

        if attach:
            self.fields.pop('slug')

        templates = template_registry.get(self._meta.model, [])
        if templates:
            self.fields['template'] = forms.ChoiceField(choices=templates,
                                                        required=False)
        else:
            self.fields.pop('template')  ## will default to content_view

        self.fields['state'] = forms.ChoiceField(choices=self.workflow_choices(),
                                                 initial=self.workflow_default(),
                                                 required=False)
        if skip_slug:
            self.fields.pop("slug")

        if enlarge:
            for enlargable_field in self.fields.values():
                self.enlarge_field(enlargable_field)

        ## make the description textarea a bit smaller
        if 'description' in self.fields:
            self.fields['description'].widget.attrs['rows'] = 4


        ## workaround for https://code.djangoproject.com/ticket/21173
        for patch_dt in ("publication", "expire", "created"):
            if patch_dt in self.fields:
                f = self.fields[patch_dt]
                f.widget = DateTimeInput()

        ## lightforms don't have a language field
        if 'language' in self.fields:
            if self.node:
                ## construct allowed languages. Exclude any language for which
                ## the node already has content
                current = self.instance.language if self.instance else None
                c = []
                for lpair in translate.languages():
                    if current == lpair[0] or \
                       not self.node.content(language=lpair[0], fallback=False):
                        c.append(lpair)
                self.fields['language'].choices = c
            else:
                self.fields['language'].choices = translate.languages()

        for e in type_registry.extenders(self.Meta.model):
            e.extend_form(self, *args, **kwargs)

        if "allowed" in self.fields:
            self.fields["allowed"].choices = ((t.name(), t.title)
                for t in sorted(type_registry.values(),
                                key=operator.attrgetter("title")))
            if self.instance:
                allowed = self.instance.allowed
                if allowed == "" and "no_subcontent" in self.fields:
                        self.fields["no_subcontent"].initial = True