Beispiel #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))
Beispiel #2
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))
Beispiel #3
0
 def _process(self):
     self.folder.is_deleted = True
     logger.info('Folder %s deleted by %s', self.folder, session.user)
     signals.attachments.folder_deleted.send(self.folder, user=session.user)
     flash(
         _("Folder \"{name}\" deleted").format(name=self.folder.title),
         'success')
     return jsonify_data(
         attachment_list=_render_attachment_list(self.object))
Beispiel #4
0
 def _process(self):
     self.attachment.is_deleted = True
     logger.info('Attachment {} deleted by {}'.format(
         self.attachment, session.user))
     signals.attachments.attachment_deleted.send(self.attachment,
                                                 user=session.user)
     flash(
         _("Attachment \"{name}\" deleted").format(
             name=self.attachment.title), 'success')
     return jsonify_data(
         attachment_list=_render_attachment_list(self.object))
Beispiel #5
0
def add_attachment_link(data, linked_object):
    """Add a link attachment to linked_object."""
    folder = data.pop('folder', None)
    if not folder:
        folder = AttachmentFolder.get_or_create_default(linked_object=linked_object)
    assert folder.object == linked_object
    link = Attachment(user=session.user, type=AttachmentType.link, folder=folder)
    link.populate_from_dict(data, skip={'acl', 'protected'})
    if link.is_self_protected:
        link.acl = data['acl']
    db.session.flush()
    logger.info('Attachment %s added by %s', link, session.user)
    signals.attachments.attachment_created.send(link, user=session.user)
Beispiel #6
0
def add_attachment_link(data, linked_object):
    """Add a link attachment to linked_object"""
    folder = data.pop('folder', None)
    if not folder:
        folder = AttachmentFolder.get_or_create_default(linked_object=linked_object)
    assert folder.object == linked_object
    link = Attachment(user=session.user, type=AttachmentType.link, folder=folder)
    link.populate_from_dict(data, skip={'acl', 'protected'})
    if link.is_self_protected:
        link.acl = data['acl']
    db.session.flush()
    logger.info('Attachment %s added by %s', link, session.user)
    signals.attachments.attachment_created.send(link, user=session.user)
Beispiel #7
0
 def _process(self):
     defaults = FormDefaults(self.folder, protected=self.folder.is_protected)
     form = AttachmentFolderForm(obj=defaults, linked_object=self.object)
     if form.validate_on_submit():
         form.populate_obj(self.folder, skip={'acl'})
         if self.folder.is_protected:
             self.folder.acl = form.acl.data
         logger.info('Folder {} edited by {}'.format(self.folder, session.user))
         signals.attachments.folder_updated.send(self.folder, user=session.user)
         flash(_("Folder \"{name}\" updated").format(name=self.folder.title), 'success')
         return jsonify_data(attachment_list=_render_attachment_list(self.object))
     return jsonify_template('attachments/create_folder.html', form=form,
                             protection_message=_render_protection_message(self.object))
Beispiel #8
0
 def _process(self):
     form = AttachmentFolderForm(obj=FormDefaults(is_always_visible=True), linked_object=self.object)
     if form.validate_on_submit():
         folder = AttachmentFolder(object=self.object)
         form.populate_obj(folder, skip={'acl'})
         if folder.is_self_protected:
             folder.acl = form.acl.data
         db.session.add(folder)
         logger.info('Folder %s created by %s', folder, session.user)
         signals.attachments.folder_created.send(folder, user=session.user)
         flash(_("Folder \"{name}\" created").format(name=folder.title), 'success')
         return jsonify_data(attachment_list=_render_attachment_list(self.object))
     return jsonify_template('attachments/create_folder.html', form=form,
                             protection_message=_render_protection_message(self.object))
Beispiel #9
0
 def _process(self):
     defaults = FormDefaults(self.folder, protected=self.folder.is_self_protected)
     form = AttachmentFolderForm(obj=defaults, linked_object=self.object)
     if form.validate_on_submit():
         form.populate_obj(self.folder, skip={'acl'})
         if self.folder.is_self_protected:
             # can't use `=` because of https://bitbucket.org/zzzeek/sqlalchemy/issues/3583
             self.folder.acl |= form.acl.data
             self.folder.acl &= form.acl.data
         logger.info('Folder %s edited by %s', self.folder, session.user)
         signals.attachments.folder_updated.send(self.folder, user=session.user)
         flash(_("Folder \"{name}\" updated").format(name=self.folder.title), 'success')
         return jsonify_data(attachment_list=_render_attachment_list(self.object))
     return jsonify_template('attachments/create_folder.html', form=form,
                             protection_message=_render_protection_message(self.object))
Beispiel #10
0
    def _process(self):
        form = AddAttachmentLinkForm(linked_object=self.object)
        if form.validate_on_submit():
            folder = form.folder.data or AttachmentFolder.get_or_create_default(linked_object=self.object)
            link = Attachment(user=session.user, type=AttachmentType.link)
            form.populate_obj(link, skip={'acl'})
            if link.is_protected:
                link.acl = form.acl.data
            link.folder = folder

            db.session.flush()
            logger.info('Attachment {} added by {}'.format(link, session.user))
            signals.attachments.attachment_created.send(link, user=session.user)
            flash(_("The link has been added"), 'success')
            return jsonify_data(attachment_list=_render_attachment_list(self.object))
        return jsonify_template('attachments/add_link.html', form=form,
                                protection_message=_render_protection_message(self.object),
                                folders_protection_info=_get_folders_protection_info(self.object))
Beispiel #11
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))
Beispiel #12
0
 def _process(self):
     form = AddAttachmentFilesForm(linked_object=self.object)
     if form.validate_on_submit():
         files = form.files.data
         folder = form.folder.data or AttachmentFolder.get_or_create_default(
             linked_object=self.object)
         for f in files:
             filename = secure_client_filename(f.filename)
             attachment = Attachment(
                 folder=folder,
                 user=session.user,
                 title=f.filename,
                 type=AttachmentType.file,
                 protection_mode=form.protection_mode.data)
             if attachment.is_self_protected:
                 attachment.acl = form.acl.data
             content_type = mimetypes.guess_type(
                 f.filename)[0] or f.mimetype or 'application/octet-stream'
             attachment.file = AttachmentFile(user=session.user,
                                              filename=filename,
                                              content_type=content_type)
             attachment.file.save(f.stream)
             db.session.add(attachment)
             db.session.flush()
             logger.info('Attachment %s uploaded by %s', attachment,
                         session.user)
             signals.attachments.attachment_created.send(attachment,
                                                         user=session.user)
         flash(
             ngettext("The attachment has been uploaded",
                      "{count} attachments have been uploaded",
                      len(files)).format(count=len(files)), 'success')
         return jsonify_data(
             attachment_list=_render_attachment_list(self.object))
     return jsonify_template(
         'attachments/upload.html',
         form=form,
         action=url_for('.upload', self.object),
         protection_message=_render_protection_message(self.object),
         folders_protection_info=_get_folders_protection_info(self.object),
         existing_attachment=None)
Beispiel #13
0
    def _process(self):
        form = AddAttachmentLinkForm(linked_object=self.object)
        if form.validate_on_submit():
            folder = form.folder.data or AttachmentFolder.get_or_create_default(
                linked_object=self.object)
            link = Attachment(user=session.user, type=AttachmentType.link)
            form.populate_obj(link, skip={'acl'})
            if link.is_protected:
                link.acl = form.acl.data
            link.folder = folder

            db.session.flush()
            logger.info('Attachment {} added by {}'.format(link, session.user))
            signals.attachments.attachment_created.send(link,
                                                        user=session.user)
            flash(_("The link has been added"), 'success')
            return jsonify_data(
                attachment_list=_render_attachment_list(self.object))
        return jsonify_template(
            'attachments/add_link.html',
            form=form,
            protection_message=_render_protection_message(self.object),
            folders_protection_info=_get_folders_protection_info(self.object))
Beispiel #14
0
 def _process(self):
     form = AddAttachmentFilesForm(linked_object=self.object)
     if form.validate_on_submit():
         files = request.files.getlist('file')
         folder = form.folder.data or AttachmentFolder.get_or_create_default(linked_object=self.object)
         for f in files:
             filename = secure_filename(f.filename, 'attachment')
             attachment = Attachment(folder=folder, user=session.user, title=to_unicode(f.filename),
                                     type=AttachmentType.file, protection_mode=form.protection_mode.data)
             if attachment.is_self_protected:
                 attachment.acl = form.acl.data
             content_type = mimetypes.guess_type(f.filename)[0] or f.mimetype or 'application/octet-stream'
             attachment.file = AttachmentFile(user=session.user, filename=filename, content_type=content_type)
             attachment.file.save(f.file)
             db.session.add(attachment)
             db.session.flush()
             logger.info('Attachment %s uploaded by %s', attachment, session.user)
             signals.attachments.attachment_created.send(attachment, user=session.user)
         flash(ngettext("The attachment has been uploaded", "{count} attachments have been uploaded", len(files))
               .format(count=len(files)), 'success')
         return jsonify_data(attachment_list=_render_attachment_list(self.object))
     return jsonify_template('attachments/upload.html', form=form, action=url_for('.upload', self.object),
                             protection_message=_render_protection_message(self.object),
                             folders_protection_info=_get_folders_protection_info(self.object))
Beispiel #15
0
 def _process(self):
     self.attachment.is_deleted = True
     logger.info('Attachment %s deleted by %s', self.attachment, session.user)
     signals.attachments.attachment_deleted.send(self.attachment, user=session.user)
     flash(_("Attachment \"{name}\" deleted").format(name=self.attachment.title), 'success')
     return jsonify_data(attachment_list=_render_attachment_list(self.object))
Beispiel #16
0
 def _process(self):
     self.folder.is_deleted = True
     logger.info('Folder {} deleted by {}'.format(self.folder, session.user))
     signals.attachments.folder_deleted.send(self.folder, user=session.user)
     flash(_("Folder \"{name}\" deleted").format(name=self.folder.title), 'success')
     return jsonify_data(attachment_list=_render_attachment_list(self.object))
Beispiel #17
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))