def clone(self, new_event, options): if 'attachments' not in options: return folder_mapping = {} attrs = get_simple_column_attrs(AttachmentFolder) for old_folder in self.find_folders(): new_folder = AttachmentFolder(event_id=new_event.id, **{attr: getattr(old_folder, attr) for attr in attrs}) if new_folder.linked_object is None: continue new_folder.acl = old_folder.acl db.session.add(new_folder) folder_mapping[old_folder] = new_folder attrs = get_simple_column_attrs(Attachment) - {'modified_dt'} for old_attachment in self.find_attachments(): folder = folder_mapping.get(old_attachment.folder) if not folder: continue new_attachment = Attachment(folder=folder, user_id=old_attachment.user_id, acl=old_attachment.acl, **{attr: getattr(old_attachment, attr) for attr in attrs}) if new_attachment.type == AttachmentType.file: old_file = old_attachment.file new_attachment.file = AttachmentFile( attachment=new_attachment, user_id=old_file.user_id, filename=old_file.filename, content_type=old_file.content_type ) with old_file.open() as fd: new_attachment.file.save(fd) db.session.add(new_attachment) db.session.flush()
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))
def clone(self, new_event, options): if 'attachments' not in options: return folder_mapping = {} for old_folder in self.find_folders(): new_folder = AttachmentFolder( title=old_folder.title, description=old_folder.description, is_default=old_folder.is_default, is_always_visible=old_folder.is_always_visible, protection_mode=old_folder.protection_mode, link_type=old_folder.link_type, event_id=new_event.id, session_id=old_folder.session_id, contribution_id=old_folder.contribution_id, subcontribution_id=old_folder.subcontribution_id ) if new_folder.linked_object is None: continue new_folder.acl = old_folder.acl db.session.add(new_folder) folder_mapping[old_folder] = new_folder for old_attachment in self.find_attachments(): folder = folder_mapping.get(old_attachment.folder) if not folder: continue new_attachment = Attachment( folder=folder, user_id=old_attachment.user_id, title=old_attachment.title, description=old_attachment.description, type=old_attachment.type, link_url=old_attachment.link_url, protection_mode=old_attachment.protection_mode, acl=old_attachment.acl ) if new_attachment.type == AttachmentType.file: old_file = old_attachment.file new_attachment.file = AttachmentFile( attachment=new_attachment, user_id=old_file.user_id, filename=old_file.filename, content_type=old_file.content_type ) with old_file.open() as fd: new_attachment.file.save(fd) db.session.add(new_attachment) db.session.flush()