def _merge_users(target, source, **kwargs): from fossir.modules.attachments.models.attachments import Attachment, AttachmentFile from fossir.modules.attachments.models.principals import AttachmentPrincipal, AttachmentFolderPrincipal Attachment.find(user_id=source.id).update({Attachment.user_id: target.id}) AttachmentFile.find(user_id=source.id).update( {AttachmentFile.user_id: target.id}) AttachmentPrincipal.merge_users(target, source, 'attachment') AttachmentFolderPrincipal.merge_users(target, source, 'folder')
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)
def _getParams(self): super(FileHook, self)._getParams() self._attachment = Attachment.get(int(self._pathParams['res'])) if not self._attachment: raise HTTPAPIError("File not found", 404)
def _clone_attachment_folder(self, old_folder, new_object): folder_attrs = get_simple_column_attrs(AttachmentFolder) | {'acl'} attachment_attrs = (get_simple_column_attrs(Attachment) | {'user', 'acl'}) - {'modified_dt'} folder = AttachmentFolder(object=new_object) folder.populate_from_attrs(old_folder, folder_attrs) for old_attachment in old_folder.attachments: attachment = Attachment(folder=folder) attachment.populate_from_attrs(old_attachment, attachment_attrs) if attachment.type == AttachmentType.file: old_file = old_attachment.file attachment.file = AttachmentFile( attachment=attachment, user=old_file.user, filename=old_file.filename, content_type=old_file.content_type) with old_file.open() as fd: attachment.file.save(fd)
def materialToXMLMarc21(self, obj, out=None): if not out: out = self._XMLGen for attachment in (Attachment.find(~AttachmentFolder.is_deleted, AttachmentFolder.object == obj, is_deleted=False, _join=AttachmentFolder) .options(joinedload(Attachment.legacy_mapping))): if attachment.can_access(self.__user): self.resourceToXMLMarc21(attachment, out) self._generateAccessList(acl=self._attachment_access_list(attachment), out=out, objId=self._attachment_unique_id(attachment, add_prefix=False))
def _build_base_query(self, added_since=None): query = Attachment.find(Attachment.type == AttachmentType.file, ~AttachmentFolder.is_deleted, ~Attachment.is_deleted, AttachmentFolder.event == self.event, _join=AttachmentFolder) if added_since is not None: query = query.join(Attachment.file).filter( cast(AttachmentFile.created_dt, Date) >= added_since) return query
def dummy_attachment(dummy_user): folder = AttachmentFolder(title='dummy_folder', description='a dummy folder') file_ = AttachmentFile(user=dummy_user, filename='dummy_file.txt', content_type='text/plain') return Attachment(folder=folder, user=dummy_user, title='dummy_attachment', type=AttachmentType.file, file=file_)
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_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.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))
def _process_args(self): self.attachment = Attachment.find_one( id=request.view_args['attachment_id'], is_deleted=False) if self.attachment.folder.is_deleted: raise NotFound