Example #1
0
    def _process(self):
        defaults = FormDefaults(self.attachment, protected=self.attachment.is_self_protected, skip_attrs={'file'})
        if self.attachment.type == AttachmentType.file:
            form = EditAttachmentFileForm(linked_object=self.object, obj=defaults, file=self.attachment)
        else:
            form = EditAttachmentLinkForm(linked_object=self.object, obj=defaults)

        if form.validate_on_submit():
            folder = form.folder.data or AttachmentFolder.get_or_create_default(linked_object=self.object)
            logger.info('Attachment %s edited by %s', self.attachment, session.user)
            form.populate_obj(self.attachment, skip={'acl', 'file'})
            self.attachment.folder = folder
            if self.attachment.is_self_protected:
                # can't use `=` because of https://bitbucket.org/zzzeek/sqlalchemy/issues/3583
                self.attachment.acl |= form.acl.data
                self.attachment.acl &= form.acl.data
            # files need special handling; links are already updated in `populate_obj`
            if self.attachment.type == AttachmentType.file:
                file = form.file.data['added']
                if file:
                    self.attachment.file = AttachmentFile(user=session.user, content_type=file.mimetype,
                                                          filename=secure_filename(file.filename, 'attachment'))
                    self.attachment.file.save(file.stream)

            signals.attachments.attachment_updated.send(self.attachment, user=session.user)
            flash(_("The attachment \"{name}\" has been updated").format(name=self.attachment.title), 'success')
            return jsonify_data(attachment_list=_render_attachment_list(self.object))

        template = ('attachments/upload.html' if self.attachment.type == AttachmentType.file else
                    'attachments/add_link.html')
        return jsonify_template(template, form=form, existing_attachment=self.attachment,
                                action=url_for('.modify_attachment', self.attachment),
                                protection_message=_render_protection_message(self.object),
                                folders_protection_info=_get_folders_protection_info(self.object))
Example #2
0
    def _process(self):
        defaults = FormDefaults(self.attachment,
                                protected=self.attachment.is_protected,
                                skip_attrs={'file'})
        if self.attachment.type == AttachmentType.file:
            file_ = self.attachment.file
            # file_attrs has to be manually "serialized", since it's going to be converted to JSON
            file_attrs = {
                'url':
                url_for('attachments.download',
                        self.attachment,
                        filename=self.attachment.file.filename,
                        from_preview='1'),
                'filename':
                file_.filename,
                'size':
                file_.size,
                'content_type':
                file_.content_type
            }
            form = EditAttachmentFileForm(linked_object=self.object,
                                          obj=defaults,
                                          file=file_attrs)
        else:
            form = EditAttachmentLinkForm(linked_object=self.object,
                                          obj=defaults)

        if form.validate_on_submit():
            folder = form.folder.data or AttachmentFolder.get_or_create_default(
                linked_object=self.object)
            logger.info('Attachment {} edited by {}'.format(
                self.attachment, session.user))
            form.populate_obj(self.attachment, skip={'acl', 'file'})
            self.attachment.folder = folder
            if self.attachment.is_protected:
                # can't use `=` because of https://bitbucket.org/zzzeek/sqlalchemy/issues/3583
                self.attachment.acl |= form.acl.data
                self.attachment.acl &= form.acl.data
            # files need special handling; links are already updated in `populate_obj`
            if self.attachment.type == AttachmentType.file:
                file = request.files['file'] if request.files else None
                if file:
                    self.attachment.file = AttachmentFile(
                        user=session.user,
                        content_type=file.mimetype,
                        filename=secure_filename(file.filename, 'attachment'))
                    self.attachment.file.save(file.file)

            signals.attachments.attachment_updated.send(self.attachment,
                                                        user=session.user)
            flash(
                _("The attachment \"{name}\" has been updated").format(
                    name=self.attachment.title), 'success')
            return jsonify_data(
                attachment_list=_render_attachment_list(self.object))

        template = ('attachments/upload.html' if self.attachment.type
                    == AttachmentType.file else 'attachments/add_link.html')
        return jsonify_template(
            template,
            form=form,
            existing_attachment=self.attachment,
            action=url_for('.modify_attachment', self.attachment),
            protection_message=_render_protection_message(self.object),
            folders_protection_info=_get_folders_protection_info(self.object))