コード例 #1
0
    def __init__(self, data=None, files=None, instance=None,
                 save_as_new=False, prefix=None, queryset=None, **kwargs):
        if self.parent_fk_name:
            if instance is None:
                self.instance = self.fk.model()
            else:
                self.instance = instance

            self.save_as_new = save_as_new
            if self.instance.id:
                object = getattr(instance, self.parent_fk_name)
                if object and object.id:
                    qs = self.model.objects.filter(pk=object.id)
                else:
                    qs = self.model.objects.none()
            else:
                qs = self.model.objects.none()
                self.extra = 1
            BaseModelFormSet.__init__(self, data, files, prefix=prefix,
                                      queryset=qs, **kwargs)
        else:
            BaseInlineFormSet.__init__(self,
                data=data,
                files=files,
                instance=instance,
                save_as_new=save_as_new, prefix=prefix, queryset=queryset, **kwargs)
コード例 #2
0
ファイル: admin.py プロジェクト: liokm/django-submodel
    def __init__(self, data=None, files=None, instance=None, save_as_new=False, prefix=None, queryset=None, **kwargs):
        from django.db.models.fields.related import RelatedObject

        if instance is None:
            self.instance = self.fk.rel.to()
        else:
            self.instance = instance
        self.save_as_new = save_as_new
        # is there a better way to get the object descriptor?
        self.rel_name = RelatedObject(self.fk.rel.to, self.model, self.fk).get_accessor_name()
        if queryset is None:
            queryset = self.model._default_manager
        # manually set queryset as evaluated
        obj = self.model.get_field().value_from_object(self.instance)
        if obj:
            queryset._result_cache = [obj]
        BaseModelFormSet.__init__(self, data, files, prefix=prefix, queryset=queryset, **kwargs)
コード例 #3
0
 def total_form_count(self):
     if self.data or self.files:
         if hasattr(self.management_form, 'cleaned_data'):
             return self.management_form.cleaned_data[TOTAL_FORM_COUNT]
         else:
             return 0
     else:
         return DjangoBaseModelFormSet.total_form_count(self)
コード例 #4
0
    def clean(self):
        BaseModelFormSet.clean(self)
        if not self.is_valid():
            return
        deleted_ids = set()
        ids_to_data = {}
        parents = {}
        # first pass: get the deleted items and map parents to items
        for i, data in enumerate(self.cleaned_data):
            ids_to_data[data['id']] = data
            if data.get('DELETE'):
                deleted_ids.add(data['id'])
            if data.get('parent'):
                parents.setdefault(data['parent'], set()).add(i)

        # second pass: check for cycles
        for data in self.cleaned_data:
            category = data
            s = set([category['id']])
            while category['parent']:
                if category['parent'] in s:
                    if len(s) == 1:  # parent set to itself
                        error = ("A category cannot be its own parent. "
                                 "Please change the parent category")
                    else:
                        error = ("Some categories have conflicting parents.  "
                                 "Please change one or more of the parent "
                                 "categories")
                    names = ', '.join([category.name for category in s])
                    raise forms.ValidationError('%s: %s' % (error, names))
                category = ids_to_data[category['parent']]
                s.add(category['id'])

        # third pass: set children of deleted items to None:
        for parent in deleted_ids:
            if parent not in parents:
                continue
            for form_index in parents[parent]:
                form = self.forms[form_index]
                form.instance.parent = None
                form.instance.save()
コード例 #5
0
ファイル: forms.py プロジェクト: MechanisM/mirocommunity
    def clean(self):
        BaseModelFormSet.clean(self)
        if not self.is_valid():
            return
        deleted_ids = set()
        ids_to_data = {}
        parents = {}
        # first pass: get the deleted items and map parents to items
        for i, data in enumerate(self.cleaned_data):
            ids_to_data[data['id']] = data
            if data.get('DELETE'):
                deleted_ids.add(data['id'])
            if data.get('parent'):
                parents.setdefault(data['parent'], set()).add(i)

        # second pass: check for cycles
        for data in self.cleaned_data:
            category = data
            s = set([category['id']])
            while category['parent']:
                if category['parent'] in s:
                    if len(s) == 1: # parent set to itself
                        error = ("A category cannot be its own parent. "
                                 "Please change the parent category")
                    else:
                        error = ("Some categories have conflicting parents.  "
                                 "Please change one or more of the parent "
                                 "categories")
                    names = ', '.join([category.name for category in s])
                    raise forms.ValidationError('%s: %s' % (error, names))
                category = ids_to_data[category['parent']]
                s.add(category['id'])

        # third pass: set children of deleted items to None:
        for parent in deleted_ids:
            if parent not in parents:
                continue
            for form_index in parents[parent]:
                form = self.forms[form_index]
                form.instance.parent = None
                form.instance.save()
コード例 #6
0
    def clean(self):
        BaseModelFormSet.clean(self)

        if any(self.errors):
            # don't bother doing anything if the form isn't valid
            return

        for form in list(self.deleted_forms):
            form.cleaned_data[DELETION_FIELD_NAME] = False
            form.instance.status = models.Video.REJECTED
            form.instance.save()
        bulk_edits = self.extra_forms[0].cleaned_data
        for key in list(bulk_edits.keys()): # get the list because we'll be
                                            # changing the dictionary
            if not bulk_edits[key]:
                del bulk_edits[key]
        bulk_action = self.data.get('bulk_action', '')
        if bulk_action:
            bulk_edits['action'] = bulk_action
        if bulk_edits:
            for form in self.initial_forms:
                if not form.cleaned_data['BULK']:
                    continue
                for key, value in bulk_edits.items():
                    if key == 'action': # do something to the video
                        method = getattr(self, 'action_%s' % value)
                        method(form)
                    elif key == 'tags':
                        form.cleaned_data[key] = value
                    elif key == 'categories':
                        # categories append, not replace
                        form.cleaned_data[key] = (
                            list(form.cleaned_data[key]) +
                            list(value))
                    elif key == 'authors':
                        form.cleaned_data[key] = value
                    else:
                        setattr(form.instance, key, value)

        self.can_delete = False
コード例 #7
0
ファイル: forms.py プロジェクト: therewasaguy/mirocommunity
    def clean(self):
        BaseModelFormSet.clean(self)

        if any(self.errors):
            # don't bother doing anything if the form isn't valid
            return

        for form in list(self.deleted_forms):
            form.cleaned_data[DELETION_FIELD_NAME] = False
            form.instance.status = models.Video.REJECTED
            form.instance.save()
        bulk_edits = self.extra_forms[0].cleaned_data
        for key in list(bulk_edits.keys()):  # get the list because we'll be
            # changing the dictionary
            if not bulk_edits[key]:
                del bulk_edits[key]
        bulk_action = self.data.get('bulk_action', '')
        if bulk_action:
            bulk_edits['action'] = bulk_action
        if bulk_edits:
            for form in self.initial_forms:
                if not form.cleaned_data['BULK']:
                    continue
                for key, value in bulk_edits.items():
                    if key == 'action':  # do something to the video
                        method = getattr(self, 'action_%s' % value)
                        method(form)
                    elif key == 'tags':
                        form.cleaned_data[key] = value
                    elif key == 'categories':
                        # categories append, not replace
                        form.cleaned_data[key] = (
                            list(form.cleaned_data[key]) + list(value))
                    elif key == 'authors':
                        form.cleaned_data[key] = value
                    else:
                        setattr(form.instance, key, value)

        self.can_delete = False
コード例 #8
0
    def __init__(self,
                 data=None,
                 files=None,
                 instance=None,
                 save_as_new=False,
                 prefix=None,
                 queryset=None,
                 **kwargs):
        if self.parent_fk_name:
            if instance is None:
                self.instance = self.fk.model()
            else:
                self.instance = instance

            self.save_as_new = save_as_new
            if self.instance.id:
                object = getattr(instance, self.parent_fk_name)
                if object and object.id:
                    qs = self.model.objects.filter(pk=object.id)
                else:
                    qs = self.model.objects.none()
            else:
                qs = self.model.objects.none()
                self.extra = 1
            BaseModelFormSet.__init__(self,
                                      data,
                                      files,
                                      prefix=prefix,
                                      queryset=qs,
                                      **kwargs)
        else:
            BaseInlineFormSet.__init__(self,
                                       data=data,
                                       files=files,
                                       instance=instance,
                                       save_as_new=save_as_new,
                                       prefix=prefix,
                                       queryset=queryset,
                                       **kwargs)
コード例 #9
0
 def clean(self):
     bulk_edits = self.extra_forms[0].cleaned_data
     for key in list(bulk_edits.keys()):  # get the list because we'll be
         # changing the dictionary
         if bulk_edits[key] in ['', None] or key == 'id':
             del bulk_edits[key]
     if bulk_edits:
         for form in self.bulk_forms:
             for key, value in bulk_edits.items():
                 if key == 'auto_categories':
                     # categories append, not replace
                     form.cleaned_data[key] = (
                         list(form.cleaned_data[key]) + list(value))
                 else:
                     form.cleaned_data[key] = value
             form.instance = construct_instance(form, form.instance,
                                                form._meta.fields,
                                                form._meta.exclude)
     return BaseModelFormSet.clean(self)
コード例 #10
0
ファイル: forms.py プロジェクト: MechanisM/mirocommunity
 def clean(self):
     bulk_edits = self.extra_forms[0].cleaned_data
     for key in list(bulk_edits.keys()): # get the list because we'll be
                                         # changing the dictionary
         if bulk_edits[key] in ['', None] or key == 'id':
             del bulk_edits[key]
     if bulk_edits:
         for form in self.bulk_forms:
             for key, value in bulk_edits.items():
                 if key == 'auto_categories':
                     # categories append, not replace
                     form.cleaned_data[key] = (
                         list(form.cleaned_data[key]) +
                         list(value))
                 else:
                     form.cleaned_data[key] = value
             form.instance = construct_instance(form, form.instance,
                                                form._meta.fields,
                                                form._meta.exclude)
     return BaseModelFormSet.clean(self)
コード例 #11
0
 def initial_form_count(self):
     if self.data or self.files:
         cleaned_data = getattr(self.management_form, 'cleaned_data', {})
         return cleaned_data.get(INITIAL_FORM_COUNT, 0)
     else:
         return DjangoBaseModelFormSet.initial_form_count(self)
コード例 #12
0
 def total_form_count(self):
     if self.data or self.files:
         cleaned_data = getattr(self.management_form, 'cleaned_data', {})
         return cleaned_data.get(TOTAL_FORM_COUNT, 0)
     else:
         return DjangoBaseModelFormSet.total_form_count(self)