Exemple #1
0
    def save_form(self, form, obj):
        # Attachment deletion
        if 'current_attachments' in form.request.POST:
            for pk in form.request.POST.getlist('current_attachments'):
                try:
                    doc = Document.query().get(pk=pk)
                except Document.DoesNotExist:
                    continue

                # Remove the attachment first
                for attachment in doc['attachments'].filter(attached_to=obj):
                    attachment.delete()

                # Removes the file only if this is the unique attachment the doc has
                if doc['attachments'].count() == 0:
                    self.remove_file(form, obj, doc)
                    doc.delete()

        # New attachments
        if 'new_attachments' in form.request.FILES:
            # Supporting multiple files uploaded
            for attfile in form.request.FILES.getlist('new_attachments'):
                doc = Document(
                    _save=True,
                    title=os.path.splitext(attfile.name)[0],
                    mime_type=attfile.content_type,
                    )

                self.save_file(form, obj, doc, attfile)

                # Attaching document to the object
                doc.attach_to(obj)
Exemple #2
0
    def save_form(self, form, obj):
        # Attachment deletion
        if 'current_attachments' in form.request.POST:
            for pk in form.request.POST.getlist('current_attachments'):
                try:
                    doc = Document.query().get(pk=pk)
                except Document.DoesNotExist:
                    continue

                # Remove the attachment first
                for attachment in doc['attachments'].filter(attached_to=obj):
                    attachment.delete()

                # Removes the file only if this is the unique attachment the doc has
                if doc['attachments'].count() == 0:
                    self.remove_file(form, obj, doc)
                    doc.delete()

        # New attachments
        if 'new_attachments' in form.request.FILES:
            # Supporting multiple files uploaded
            for attfile in form.request.FILES.getlist('new_attachments'):
                doc = Document(
                    _save=True,
                    title=os.path.splitext(attfile.name)[0],
                    mime_type=attfile.content_type,
                )

                self.save_file(form, obj, doc, attfile)

                # Attaching document to the object
                doc.attach_to(obj)
    def get_initial(self, initial=None):
        initial = super(AttachmentsModelForm, self).get_initial(initial)

        if self.instance and self.can_do_attachments():
            initial['current_attachments'] = Document.query().attached_to(self.instance)
            self.fields['current_attachments'].widget.attachments = initial['current_attachments']
        else:
            self.fields['new_attachments'].widget = forms.HiddenInput()
            self.fields['current_attachments'].widget = forms.HiddenInput()

        return initial
    def get_initial(self, initial=None):
        initial = super(AttachmentsModelForm, self).get_initial(initial)

        if self.instance and self.can_do_attachments():
            initial["current_attachments"] = Document.query().attached_to(self.instance)
            self.fields["current_attachments"].widget.attachments = initial["current_attachments"]
        else:
            self.fields["new_attachments"].widget = forms.HiddenInput()
            self.fields["current_attachments"].widget = forms.HiddenInput()

        return initial