def generate_reports(self, created_at, is_regenerated_report=False, return_json_only=False, manually_approved=False): visit = self.visit patient = visit.patient data = self.sierra_result.get('data') prepared_data = prepare_data(self, created_at, data, is_regenerated_report) approved = manually_approved or prepared_data['auto_approved'] if return_json_only: return prepared_data filename = ( '{clinic}_{lastname}_{mrid}_{vnum}_{test_code}_{physician}_{dt}'. format(clinic=self.clinic.name if self.clinic else 'UNKNOWN', lastname=patient.lastname.replace(' ', ''), mrid=visit.mrid, vnum=self.vnum, test_code=self.test_code, physician=self.physician.name.replace(' ', ''), dt=created_at.strftime('%Y%m%dT%H%M'))) for generator in generators: report = app.models.Report( status='approved' if approved else 'pending', content_type=generator.content_type, created_at=created_at) report.content = FileIntent(generator.render(prepared_data), filename='{}.{}'.format( filename, generator.content_type), content_type=generator.mimetype) self.reports.append(report)
def upgrade(): """ Sets all depot files for file typed revisions. Until now, files are both in database and, for the newly created ones, on disk. In order to simplify the migration, this procedure will: - delete the few files on disk, - create all files on disk from database. """ # Creates files depot used in this migration configure_depot() connection = op.get_bind() delete_files_on_disk(connection=connection) select_query = revision_helper.select() \ .where(revision_helper.c.type == 'file') \ .where(revision_helper.c.depot_file.is_(None)) \ .where(func.length(revision_helper.c.file_content) > 0) files = connection.execute(select_query).fetchall() for file in files: file_filename = '{0}{1}'.format( file.label, file.file_extension, ) depot_file_intent = FileIntent( file.file_content, file_filename, file.file_mimetype, ) depot_file_field = UploadedFile(depot_file_intent, 'tracim') update_query = revision_helper.update() \ .where(revision_helper.c.revision_id == file.revision_id) \ .values(depot_file=depot_file_field) connection.execute(update_query)
def _reserve_id(self): # This needs to be a two-step write, since we need to store # the identifier in the nanopub for consistency, and we don't # get the identifier until we write the file! fileid = self.depot.create( FileIntent('', ld.create_id(), 'application/trig')) return fileid
def generate_reports(self, created_at, is_regenerated_report=False, return_json_only=False): filename = ( 'PROFICIENCY_{source}_{name}_{vnum}_{test_code}_{dt}'.format( source=self.source, name=self.name, vnum=self.vnum, test_code=self.test_code, dt=created_at.strftime('%Y%m%dT%H%M'))) data = self.sierra_result.get('data') prepared_data = prepare_profsample_data(self, created_at, data, is_regenerated_report) auto_approved = prepared_data['auto_approved'] if return_json_only: return prepared_data for generator in profsample_generators: report = app.models.Report( status='approved' if auto_approved else 'pending', content_type=generator.content_type, created_at=created_at) report.content = FileIntent(generator.render(prepared_data), filename='{}.{}'.format( filename, generator.content_type), content_type=generator.mimetype) self.reports.append(report)
def new_from(cls, revision: 'ContentRevisionRO') -> 'ContentRevisionRO': """ Return new instance of ContentRevisionRO where properties are copied from revision parameter. Look at ContentRevisionRO._cloned_columns to see what columns are copieds. :param revision: revision to copy :type revision: ContentRevisionRO :return: new revision from revision parameter :rtype: ContentRevisionRO """ new_rev = cls() for column_name in cls._cloned_columns: column_value = getattr(revision, column_name) setattr(new_rev, column_name, column_value) new_rev.updated = datetime.utcnow() if revision.depot_file: new_rev.depot_file = FileIntent( revision.depot_file.file.read(), revision.file_name, revision.file_mimetype, ) return new_rev
def copy( cls, revision: 'ContentRevisionRO', parent: 'Content' ) -> 'ContentRevisionRO': copy_rev = cls() import copy copy_columns = cls._cloned_columns for column_name in copy_columns: # INFO - G-M - 15-03-2018 - set correct parent if column_name == 'parent_id': column_value = copy.copy(parent.id) elif column_name == 'parent': column_value = copy.copy(parent) else: column_value = copy.copy(getattr(revision, column_name)) setattr(copy_rev, column_name, column_value) # copy attached_file if revision.depot_file: copy_rev.depot_file = FileIntent( revision.depot_file.file.read(), revision.file_name, revision.file_mimetype, ) return copy_rev
def parse_attachment(attstring): if not attstring: return m = scrubbed_p.match(attstring) if m: atype = m.group(1) else: return aparts = {} for line in attstring.splitlines(): k, s, v = line.partition(': ') if not s: continue aparts[k.casefold()] = v if 'type' not in aparts.keys() and atype == 'HTML': aparts['type'] = 'text/html' if aparts.get('type') == 'application/pgp-signature': # Unfortunately, Mailman has a tendency to mutilate the # messages, so PGP signatures aren't useful :-( return False if 'url' not in aparts.keys() or '/private/' in aparts['url']: return False r = requests.get(aparts['url'][1:-1]) if not r.ok: log.warning("Failed to download attachment {}".format(r.url)) return False return FileIntent(r.content, aparts.get('name'), aparts.get('type'))
def test_fileintent(self): file_id = self.fs.create(FileIntent(FILE_CONTENT, 'file.txt', 'text/plain')) f = self.fs.get(file_id) assert f.content_type == 'text/plain', f.content_type assert f.filename == 'file.txt', f.filename assert f.read() == FILE_CONTENT
def input_upload(self, input_upload=None, team_name=None): user = request.identity['user'] if not user.admin and not self.group.competition.input_upload_open: abort(403, "Sorry, input upload stage is closed.") if hasattr(input_upload, "file"): try: contents = input_upload.file.read().decode("utf-8") except UnicodeDecodeError: flash('Your input contains invalid characters. ' 'Please correct and try uploading again.', 'danger') redirect(self.base_url) problem = problemlib.load_problem(self.group.competition) try: input = problem.parse_input(StringIO(contents)) except problemlib.FileFormatError as e: flash( f"Your input is not valid: {e}. Please correct and upload again.", "danger", ) redirect(self.base_url) reformatted_contents = StringIO() input.write(reformatted_contents) f = FileIntent( BytesIO(reformatted_contents.getvalue().encode('utf-8')), 'input_group{}.txt'.format(self.group.id), 'application/octet-stream') if self.group.input is None: iput = Input(data=f, group=self.group) DBSession.add(iput) else: self.group.input.data = f DBSession.flush() if team_name is not None: if len(team_name) >= 100: flash('Your team name has been rejected, as it is too long.', 'danger') redirect(self.base_url) if not team_name: flash('You must set a team name.', 'danger') redirect(self.base_url) self.group.name = team_name flash('Thank you. Please return here for output upload on {}' .format(ftime(self.group.competition.output_upload_begins)), 'success') redirect(self.base_url)
def update_file_data(self, item: Content, new_filename: str, new_mimetype: str, new_content: bytes) -> Content: item.owner = self._user item.file_name = new_filename item.file_mimetype = new_mimetype item.depot_file = FileIntent( new_content, new_filename, new_mimetype, ) item.revision_type = ActionDescription.REVISION return item
def test_creation_inputs(self): temp = NamedTemporaryFile() temp.write(FILE_CONTENT) temp.seek(0) for d in (FILE_CONTENT, BytesIO(FILE_CONTENT), temp, create_cgifs('text/plain', FILE_CONTENT, 'file.txt'), FileIntent(FILE_CONTENT, 'file.txt', 'text/plain')): fid = self.fs.create(d, 'filename') f = self.fs.get(fid) assert f.read() == FILE_CONTENT
def input_upload(self, input_upload=None, team_name=None): user = request.identity['user'] if not user.admin and not self.group.competition.input_upload_open: abort(403, "Sorry, input upload stage is closed.") if hasattr(input_upload, "file"): try: contents = file_normalize(input_upload.file.read()) except UnicodeDecodeError: flash( 'Your input contains invalid characters. ' 'Please correct and try uploading again.', 'danger') redirect(self.base_url) if len(contents) > 1E6: abort(400, "Your input exceeds the maxiumum size.") verif_mod = self.group.competition.input_verifier.module try: verif_mod.verify(StringIO(contents)) except verif_mod.VerificationError as e: flash( 'Your input has been rejected for the following reason: ' '{}. Please correct and try uploading again.'.format(e), 'danger') redirect(self.base_url) f = FileIntent(BytesIO(contents.encode('utf-8')), 'input_group{}.txt'.format(self.group.id), 'application/octet-stream') if self.group.input is None: iput = Input(data=f, group=self.group) DBSession.add(iput) else: self.group.input.data = f DBSession.flush() if team_name is not None: if len(team_name) >= 100: flash('Your team name has been rejected, as it is too long.', 'danger') redirect(self.base_url) if not team_name: flash('You must set a team name.', 'danger') redirect(self.base_url) self.group.name = team_name flash( 'Thank you. Please return here for output upload on {}'.format( ftime(self.group.competition.output_upload_begins)), 'success') redirect(self.base_url)
def publish(self, nanopub): ident = nanopub.identifier.replace(self.prefix, "") g = rdflib.ConjunctiveGraph(store=nanopub.store) # This needs to be a two-step write, since we need to store # the identifier in the nanopub for consistency, and we don't # get the identifier until we write the file! fileid = self.depot.create(FileIntent('', ident, 'application/trig')) nanopub.add( (nanopub.identifier, dc.identifier, rdflib.Literal(fileid))) self._idmap[nanopub.identifier] = fileid self.depot.replace( fileid, FileIntent(g.serialize(format="trig"), ident, 'application/trig')) for revised in nanopub.pubinfo.objects(nanopub.assertion.identifier, prov.wasRevisionOf): for nanopub_uri in self.db.subjects(predicate=np.hasAssertion, object=revised): print "Retiring", nanopub_uri self.retire(nanopub_uri) self.db.addN(nanopub.quads()) self.update_listener(nanopub.identifier)
def _convert_to_python(self, value, state=None): if isinstance(value, cgi.FieldStorage): if self.required and not getattr(value, 'filename', None): raise tw2.core.ValidationError('required', self) if (self.extension is not None and not value.filename.endswith(self.extension)): raise tw2.core.ValidationError('badext', self) elif value: raise tw2.core.ValidationError('corrupt', self) elif self.required: raise tw2.core.ValidationError('required', self) return FileIntent(*FileStorage.fileinfo(value))
def test_create_fileintent(self): field = FileIntent(open(self.fake_file.name, 'rb'), u_('à èìòù.gif'), 'image/gif') doc = Document(name=u_('Foo'), second_photo=field) DBSession.add(doc) self._session_flush() DBSession.commit() d = DBSession.query(Document).filter_by(name=u_('Foo')).first() assert d.second_photo.file.read() == self.file_content assert d.second_photo.filename == field._filename assert d.second_photo.url == '/depot/%s' % d.second_photo.path assert d.second_photo.thumb_12x12_url == '/depot/%s' % d.second_photo.thumb_12x12_path assert d.second_photo.url != d.second_photo.thumb_12x12_url assert d.second_photo.content_type == field._content_type
def update_file_data(self, item: Content, new_filename: str, new_mimetype: str, new_content: bytes) -> Content: if new_mimetype == item.file_mimetype and \ new_content == item.depot_file.file.read(): raise SameValueError('The content did not changed') item.owner = self._user item.file_name = new_filename item.file_mimetype = new_mimetype item.depot_file = FileIntent( new_content, new_filename, new_mimetype, ) item.revision_type = ActionDescription.REVISION return item
def publish(self, *nanopubs): #self.db.store.nsBindings = {} with tempfile.TemporaryFile() as data: to_retire = [] for nanopub in nanopubs: fileid = nanopub.identifier.replace(self.prefix, "") r = False now = rdflib.Literal(datetime.utcnow()) for revised in nanopub.pubinfo.objects( nanopub.assertion.identifier, prov.wasRevisionOf): for nanopub_uri in self.db.subjects( predicate=np.hasAssertion, object=revised): nanopub.pubinfo.set( (nanopub_uri, prov.invalidatedAtTime, now)) to_retire.append(nanopub_uri) r = True print "Retiring", nanopub_uri if r: nanopub.pubinfo.set( (nanopub.assertion.identifier, dc.modified, now)) else: nanopub.pubinfo.set( (nanopub.assertion.identifier, dc.created, now)) g = rdflib.ConjunctiveGraph(store=nanopub.store) for graph_part in [ nanopub, nanopub.assertion, nanopub.provenance, nanopub.pubinfo ]: g_part = rdflib.Graph(identifier=graph_part.identifier, store=g.store) g_part.addN([(s, p, o, g_part) for s, p, o in graph_part.triples((None, None, None))]) serialized = g.serialize(format="trig") print "Adding %s bytes to the load file." % len(serialized) del g self.depot.replace( fileid, FileIntent(serialized, fileid, 'application/trig')) data.write(serialized) data.write('\n') data.flush() self.retire(*to_retire) data.seek(0) self.db.store.publish(data, *nanopubs) print "Published %s nanopubs" % len(nanopubs) for nanopub in nanopubs: self.update_listener(nanopub.identifier)
def populate_report(data): sample = data['sample'] if len(sample.reports): return filename = data['filename'] created_at = datetime.fromtimestamp(os.path.getmtime(filename)) with open(filename, 'rb') as fp: report = models.Report(status='approved', content_type='pdf', created_at=created_at) report.content = FileIntent(fp.read(), filename=os.path.basename(filename), content_type='application/pdf') sample.reports.append(report) click.echo('[INFO] Populated missing report to sample-{}'.format( sample.id))
def generate_reports(self, created_at, is_regenerated_report=False): data = self.sierra_result.get('data') filename = ( 'POSCTL_{note}_{lot_number}_{entered_at}_{test_code}_{dt}'.format( note=self.note, lot_number=self.lot_number, entered_at=self.entered_at.strftime('%Y%m%dT%H%M'), test_code=self.test_code, dt=created_at.strftime('%Y%m%dT%H%M'))) prepared_data = prepare_posctl_data(self, created_at, data, is_regenerated_report) auto_approved = prepared_data['auto_approved'] for generator in posctl_generators: report = app.models.Report( status='approved' if auto_approved else 'pending', content_type=generator.content_type, created_at=created_at) report.content = FileIntent(generator.render(prepared_data), filename='{}.{}'.format( filename, generator.content_type), content_type=generator.mimetype) self.reports.append(report)
def submit_output(self, to_group, output_file=None): user = request.identity['user'] to_group = DBSession.query(Group).get(to_group) if not to_group: abort(404, "No such group") if to_group.competition_id != self.group.competition_id: abort(400, "Cannot submit to a group in another competition") if not hasattr(output_file, "file"): abort(400, "Must include file in submission") comp = self.group.competition existing = (DBSession.query(Output) .filter(Output.group_id == self.group.id) .filter(Output.input_id == to_group.input.id) .filter(Output.active == True) .one_or_none()) if not (user.admin or comp.output_upload_open or (comp.resolution_open and existing is not None and not existing.use_ground_truth)): abort(403, "Forbidden to upload this output at this time") try: contents = output_file.file.read().decode("utf-8") except UnicodeDecodeError: return {'status': 'error', 'msg': 'Output contains invalid characters.'} problem = problemlib.load_problem(comp) try: output = problem.parse_output(to_group.input.data.file, StringIO(contents)) except problemlib.FileFormatError as e: return { "status": "error", "msg": f"Output has formatting errors: {e}", } new_contents = StringIO() output.write(new_contents) f = FileIntent( BytesIO(new_contents.getvalue().encode("utf-8")), 'output_from_{}_to_{}.txt'.format(self.group.id, to_group.id), 'application/octet-stream') try: output.verify() except problemlib.VerificationError: ground_truth = VerificationStatus.rejected except Exception: ground_truth = VerificationStatus.waiting else: ground_truth = VerificationStatus.accepted use_ground_truth = ( not comp.verification_begins or datetime.datetime.now() >= comp.verification_begins ) if existing: if comp.resolution_open: existing.active = False else: DBSession.delete(existing) db_output = Output( data=f, group=self.group, input=to_group.input, score=output.score, original=comp.output_upload_open, ground_truth=ground_truth, use_ground_truth=use_ground_truth, ) DBSession.add(db_output) DBSession.flush() return {'status': 'success', 'url': db_output.data.url}
def publish(self, *nanopubs): #self.db.store.nsBindings = {} full_list = [] with tempfile.NamedTemporaryFile(delete=False) as data: to_retire = [] for np_graph in nanopubs: for entity in np_graph.subjects(self.app.NS.whyis.hasContent): localpart = self.db.qname(entity).split(":")[1] filename = secure_filename(localpart) f = DataURLStorage(np_graph.value(entity, self.app.NS.whyis.hasContent), filename=filename) print 'adding file', filename self.app.add_file(f, entity, np_graph) np_graph.remove((entity, self.app.NS.whyis.hasContent, None)) for context in np_graph.contexts(): if (context.identifier,rdflib.RDF.type, np.Nanopublication) in context: nanopub_uri = context.identifier else: continue fileid = nanopub_uri.replace(self.prefix, "") r = False now = rdflib.Literal(datetime.utcnow()) nanopub = Nanopublication(identifier=nanopub_uri, store=np_graph.store) for revised in nanopub.pubinfo.objects(nanopub.assertion.identifier, prov.wasRevisionOf): for nanopub_uri in self.db.subjects(predicate=np.hasAssertion, object=revised): nanopub.pubinfo.set((nanopub_uri, prov.invalidatedAtTime, now)) to_retire.append(nanopub_uri) r = True print "Retiring", nanopub_uri if r: nanopub.pubinfo.set((nanopub.assertion.identifier, dc.modified, now)) else: nanopub.pubinfo.set((nanopub.assertion.identifier, dc.created, now)) g = rdflib.ConjunctiveGraph() for graph_part in [rdflib.Graph(identifier=x.identifier, store=x.store) for x in nanopub, nanopub.assertion, nanopub.provenance, nanopub.pubinfo]: g_part = rdflib.Graph(identifier=graph_part.identifier, store=g.store) for s,p,o in graph_part: g_part.add((s,p,o)) serialized = g.serialize(format="trig") self.depot.replace(fileid, FileIntent(serialized, fileid, 'application/trig')) data.write(serialized) data.write('\n') data.flush() full_list.append(nanopub.identifier) #np_graph.serialize(data, format="trig") #data.write('\n') #data.flush() #print data.name #np_graph.serialize(data, format="trig") #data.write('\n') #data.flush() self.retire(*to_retire) data.seek(0) self.db.store.publish(data, *nanopubs) print "Published %s nanopubs" % len(full_list) for n in full_list: self.update_listener(n)
def submit_output(self, to_group, output_file=None): user = request.identity['user'] to_group = DBSession.query(Group).get(to_group) if not to_group: abort(404, "No such group") if to_group.competition_id != self.group.competition_id: abort(400, "Cannot submit to a group in another competition") if not hasattr(output_file, "file"): abort(400, "Must include file in submission") comp = self.group.competition existing = (DBSession.query(Output).filter( Output.group_id == self.group.id).filter( Output.input_id == to_group.input.id).filter( Output.active == True).one_or_none()) if not (user.admin or comp.output_upload_open or (comp.resolution_open and existing is not None and not existing.use_ground_truth)): abort(403, "Forbidden to upload this output at this time") try: contents = file_normalize(output_file.file.read()) except UnicodeDecodeError: return { 'status': 'error', 'msg': 'Output contains invalid characters.' } if len(contents) > 1E6: return {'status': 'error', 'msg': 'Output exceeds maximum size.'} try: score, _, _ = contents.partition('\n') score = int(score) except ValueError: return { 'status': 'error', 'msg': 'First line must only contain an integer.' } f = FileIntent( BytesIO(contents.encode('utf-8')), 'output_from_{}_to_{}.txt'.format(self.group.id, to_group.id), 'application/octet-stream') if existing: if comp.resolution_open: existing.active = False else: DBSession.delete(existing) output = Output(data=f, group=self.group, input=to_group.input, score=score, original=comp.output_upload_open) verif_mod = self.group.competition.output_verifier.module try: verif_mod.verify(to_group.input.data.file, StringIO(contents)) except verif_mod.VerificationError: output.ground_truth = VerificationStatus.rejected except Exception: output.ground_truth = VerificationStatus.waiting else: output.ground_truth = VerificationStatus.accepted if comp.resolution_open or not comp.verification_begins: output.use_ground_truth = True DBSession.add(output) DBSession.flush() return {'status': 'success', 'url': output.data.url}
def insert(self): admin = self._session.query(User).filter(User.email == "*****@*****.**").one() bob = self._session.query(User).filter(User.email == "*****@*****.**").one() john_the_reader = ( self._session.query(User).filter(User.email == "*****@*****.**").one() ) admin_workspace_api = WorkspaceApi( current_user=admin, session=self._session, config=self._config ) bob_workspace_api = WorkspaceApi( current_user=bob, session=self._session, config=self._config ) content_api = ContentApi(current_user=admin, session=self._session, config=self._config) bob_content_api = ContentApi(current_user=bob, session=self._session, config=self._config) reader_content_api = ContentApi( current_user=john_the_reader, session=self._session, config=self._config ) role_api = RoleApi(current_user=admin, session=self._session, config=self._config) # Workspaces business_workspace = admin_workspace_api.create_workspace( "Business", description="All importants documents", save_now=True ) recipe_workspace = admin_workspace_api.create_workspace( "Recipes", description="Our best recipes", save_now=True ) other_workspace = bob_workspace_api.create_workspace( "Others", description="Other Workspace", save_now=True ) # Workspaces roles role_api.create_one( user=bob, workspace=recipe_workspace, role_level=UserRoleInWorkspace.CONTENT_MANAGER, with_notif=False, ) role_api.create_one( user=john_the_reader, workspace=recipe_workspace, role_level=UserRoleInWorkspace.READER, with_notif=False, ) # Folders content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=business_workspace, label="Tools", do_save=True, do_notify=False, ) menu_workspace = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=business_workspace, label="Menus", do_save=True, do_notify=False, ) dessert_folder = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=recipe_workspace, label="Desserts", do_save=True, do_notify=False, ) content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=recipe_workspace, label="Salads", do_save=True, do_notify=False, ) content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=other_workspace, label="Infos", do_save=True, do_notify=False, ) # Pages, threads, .. tiramisu_page = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=recipe_workspace, parent=dessert_folder, label="Tiramisu Recipes!!!", do_save=True, do_notify=False, ) with new_revision(session=self._session, tm=transaction.manager, content=tiramisu_page): content_api.update_content( item=tiramisu_page, new_content="<p>To cook a greet Tiramisu, you need many ingredients.</p>", new_label="Tiramisu Recipes!!!", ) content_api.save(tiramisu_page, do_notify=False) best_cake_thread = content_api.create( content_type_slug=content_type_list.Thread.slug, workspace=recipe_workspace, parent=dessert_folder, label="Best Cake", do_save=False, do_notify=False, ) best_cake_thread.description = "Which is the best cake?" self._session.add(best_cake_thread) apple_pie_recipe = content_api.create( content_type_slug=content_type_list.File.slug, workspace=recipe_workspace, parent=dessert_folder, label="Apple_Pie", do_save=False, do_notify=False, ) apple_pie_recipe.file_extension = ".txt" apple_pie_recipe.depot_file = FileIntent(b"Apple pie Recipe", "apple_Pie.txt", "text/plain") self._session.add(apple_pie_recipe) brownie_recipe = content_api.create( content_type_slug=content_type_list.File.slug, workspace=recipe_workspace, parent=dessert_folder, label="Brownie Recipe", do_save=False, do_notify=False, ) brownie_recipe.file_extension = ".html" brownie_recipe.depot_file = FileIntent( b"<p>Brownie Recipe</p>", "brownie_recipe.html", "text/html" ) self._session.add(brownie_recipe) fruits_desserts_folder = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=recipe_workspace, label="Fruits Desserts", parent=dessert_folder, do_save=True, do_notify=False, ) content_api.create( content_type_slug=content_type_list.Page.slug, workspace=business_workspace, parent=menu_workspace, label="Current Menu", do_save=True, do_notify=False, ) content_api.create( content_type_slug=content_type_list.Page.slug, workspace=recipe_workspace, parent=fruits_desserts_folder, label="New Fruit Salad", do_save=True, do_notify=False, ) old_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=recipe_workspace, parent=fruits_desserts_folder, label="Fruit Salad", do_save=True, do_notify=False, ) with new_revision(session=self._session, tm=transaction.manager, content=old_fruit_salad): content_api.archive(old_fruit_salad) content_api.save(old_fruit_salad, do_notify=False) bad_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=recipe_workspace, parent=fruits_desserts_folder, label="Bad Fruit Salad", do_save=True, do_notify=False, ) with new_revision(session=self._session, tm=transaction.manager, content=bad_fruit_salad): content_api.delete(bad_fruit_salad) content_api.save(bad_fruit_salad, do_notify=False) # File at the root for test content_api.create( content_type_slug=content_type_list.Page.slug, workspace=other_workspace, label="New Fruit Salad", do_save=True, do_notify=False, ) old_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=other_workspace, label="Fruit Salad", do_save=True, do_notify=False, ) with new_revision(session=self._session, tm=transaction.manager, content=old_fruit_salad): content_api.archive(old_fruit_salad) content_api.save(old_fruit_salad, do_notify=False) bad_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=other_workspace, label="Bad Fruit Salad", do_save=True, do_notify=False, ) with new_revision(session=self._session, tm=transaction.manager, content=bad_fruit_salad): content_api.delete(bad_fruit_salad) content_api.save(bad_fruit_salad, do_notify=False) content_api.create_comment( parent=best_cake_thread, content="<p>What is for you the best cake ever? <br/> I personnally vote for Chocolate cupcake!</p>", do_save=True, do_notify=False, ) bob_content_api.create_comment( parent=best_cake_thread, content="<p>What about Apple Pie? There are Awesome!</p>", do_save=True, do_notify=False, ) reader_content_api.create_comment( parent=best_cake_thread, content="<p>You are right, but Kouign-amann are clearly better.</p>", do_save=True, do_notify=False, ) with new_revision(session=self._session, tm=transaction.manager, content=best_cake_thread): bob_content_api.update_content( item=best_cake_thread, new_content="What is the best cake?", new_label="Best Cakes?" ) bob_content_api.save(best_cake_thread, do_notify=False) with new_revision(session=self._session, tm=transaction.manager, content=tiramisu_page): bob_content_api.update_content( item=tiramisu_page, new_content="<p>To cook a great Tiramisu, you need many ingredients.</p>", new_label="Tiramisu Recipe", ) bob_content_api.save(tiramisu_page, do_notify=False) self._session.flush()
def insert(self): admin = self._session.query(models.User) \ .filter(models.User.email == '*****@*****.**') \ .one() bob = self._session.query(models.User) \ .filter(models.User.email == '*****@*****.**') \ .one() admin_workspace_api = WorkspaceApi( current_user=admin, session=self._session, ) bob_workspace_api = WorkspaceApi( current_user=bob, session=self._session, ) content_api = ContentApi( current_user=admin, session=self._session, ) role_api = RoleApi( current_user=admin, session=self._session, ) # Workspaces w1 = admin_workspace_api.create_workspace('w1', save_now=True) w2 = bob_workspace_api.create_workspace('w2', save_now=True) w3 = admin_workspace_api.create_workspace('w3', save_now=True) # Workspaces roles role_api.create_one( user=bob, workspace=w1, role_level=UserRoleInWorkspace.CONTENT_MANAGER, with_notif=False, ) # Folders w1f1 = content_api.create( content_type=ContentType.Folder, workspace=w1, label='w1f1', do_save=True, ) w1f2 = content_api.create( content_type=ContentType.Folder, workspace=w1, label='w1f2', do_save=True, ) w2f1 = content_api.create( content_type=ContentType.Folder, workspace=w2, label='w2f1', do_save=True, ) w2f2 = content_api.create( content_type=ContentType.Folder, workspace=w2, label='w2f2', do_save=True, ) w3f1 = content_api.create( content_type=ContentType.Folder, workspace=w3, label='w3f3', do_save=True, ) # Pages, threads, .. w1f1p1 = content_api.create( content_type=ContentType.Page, workspace=w1, parent=w1f1, label='w1f1p1', do_save=True, ) w1f1t1 = content_api.create( content_type=ContentType.Thread, workspace=w1, parent=w1f1, label='w1f1t1', do_save=False, ) w1f1t1.description = 'w1f1t1 description' self._session.add(w1f1t1) w1f1d1_txt = content_api.create( content_type=ContentType.File, workspace=w1, parent=w1f1, label='w1f1d1', do_save=False, ) w1f1d1_txt.file_extension = '.txt' w1f1d1_txt.depot_file = FileIntent( b'w1f1d1 content', 'w1f1d1.txt', 'text/plain', ) self._session.add(w1f1d1_txt) w1f1d2_html = content_api.create( content_type=ContentType.File, workspace=w1, parent=w1f1, label='w1f1d2', do_save=False, ) w1f1d2_html.file_extension = '.html' w1f1d2_html.depot_file = FileIntent( b'<p>w1f1d2 content</p>', 'w1f1d2.html', 'text/html', ) self._session.add(w1f1d2_html) w1f1f1 = content_api.create( content_type=ContentType.Folder, workspace=w1, label='w1f1f1', parent=w1f1, do_save=True, ) w2f1p1 = content_api.create( content_type=ContentType.Page, workspace=w2, parent=w2f1, label='w2f1p1', do_save=True, ) self._session.flush()
def insert(self): admin = self._session.query(models.User) \ .filter(models.User.email == '*****@*****.**') \ .one() bob = self._session.query(models.User) \ .filter(models.User.email == '*****@*****.**') \ .one() john_the_reader = self._session.query(models.User) \ .filter(models.User.email == '*****@*****.**') \ .one() admin_workspace_api = WorkspaceApi( current_user=admin, session=self._session, config=self._config, ) bob_workspace_api = WorkspaceApi(current_user=bob, session=self._session, config=self._config) content_api = ContentApi(current_user=admin, session=self._session, config=self._config) bob_content_api = ContentApi(current_user=bob, session=self._session, config=self._config) reader_content_api = ContentApi(current_user=john_the_reader, session=self._session, config=self._config) role_api = RoleApi( current_user=admin, session=self._session, config=self._config, ) # Workspaces business_workspace = admin_workspace_api.create_workspace( 'Business', description='All importants documents', save_now=True, ) recipe_workspace = admin_workspace_api.create_workspace( 'Recipes', description='Our best recipes', save_now=True, ) other_workspace = bob_workspace_api.create_workspace( 'Others', description='Other Workspace', save_now=True, ) # Workspaces roles role_api.create_one( user=bob, workspace=recipe_workspace, role_level=UserRoleInWorkspace.CONTENT_MANAGER, with_notif=False, ) role_api.create_one( user=john_the_reader, workspace=recipe_workspace, role_level=UserRoleInWorkspace.READER, with_notif=False, ) # Folders tool_workspace = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=business_workspace, label='Tools', do_save=True, do_notify=False, ) menu_workspace = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=business_workspace, label='Menus', do_save=True, do_notify=False, ) dessert_folder = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=recipe_workspace, label='Desserts', do_save=True, do_notify=False, ) salads_folder = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=recipe_workspace, label='Salads', do_save=True, do_notify=False, ) other_folder = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=other_workspace, label='Infos', do_save=True, do_notify=False, ) # Pages, threads, .. tiramisu_page = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=recipe_workspace, parent=dessert_folder, label='Tiramisu Recipes!!!', do_save=True, do_notify=False, ) with new_revision( session=self._session, tm=transaction.manager, content=tiramisu_page, ): content_api.update_content( item=tiramisu_page, new_content= '<p>To cook a greet Tiramisu, you need many ingredients.</p>', # nopep8 new_label='Tiramisu Recipes!!!', ) content_api.save(tiramisu_page) best_cake_thread = content_api.create( content_type_slug=content_type_list.Thread.slug, workspace=recipe_workspace, parent=dessert_folder, label='Best Cake', do_save=False, do_notify=False, ) best_cake_thread.description = 'Which is the best cake?' self._session.add(best_cake_thread) apple_pie_recipe = content_api.create( content_type_slug=content_type_list.File.slug, workspace=recipe_workspace, parent=dessert_folder, label='Apple_Pie', do_save=False, do_notify=False, ) apple_pie_recipe.file_extension = '.txt' apple_pie_recipe.depot_file = FileIntent( b'Apple pie Recipe', 'apple_Pie.txt', 'text/plain', ) self._session.add(apple_pie_recipe) Brownie_recipe = content_api.create( content_type_slug=content_type_list.File.slug, workspace=recipe_workspace, parent=dessert_folder, label='Brownie Recipe', do_save=False, do_notify=False, ) Brownie_recipe.file_extension = '.html' Brownie_recipe.depot_file = FileIntent( b'<p>Brownie Recipe</p>', 'brownie_recipe.html', 'text/html', ) self._session.add(Brownie_recipe) fruits_desserts_folder = content_api.create( content_type_slug=content_type_list.Folder.slug, workspace=recipe_workspace, label='Fruits Desserts', parent=dessert_folder, do_save=True, ) menu_page = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=business_workspace, parent=menu_workspace, label='Current Menu', do_save=True, ) new_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=recipe_workspace, parent=fruits_desserts_folder, label='New Fruit Salad', do_save=True, ) old_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=recipe_workspace, parent=fruits_desserts_folder, label='Fruit Salad', do_save=True, do_notify=False, ) with new_revision( session=self._session, tm=transaction.manager, content=old_fruit_salad, ): content_api.archive(old_fruit_salad) content_api.save(old_fruit_salad) bad_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=recipe_workspace, parent=fruits_desserts_folder, label='Bad Fruit Salad', do_save=True, do_notify=False, ) with new_revision( session=self._session, tm=transaction.manager, content=bad_fruit_salad, ): content_api.delete(bad_fruit_salad) content_api.save(bad_fruit_salad) # File at the root for test new_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=other_workspace, label='New Fruit Salad', do_save=True, ) old_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=other_workspace, label='Fruit Salad', do_save=True, ) with new_revision( session=self._session, tm=transaction.manager, content=old_fruit_salad, ): content_api.archive(old_fruit_salad) content_api.save(old_fruit_salad) bad_fruit_salad = content_api.create( content_type_slug=content_type_list.Page.slug, workspace=other_workspace, label='Bad Fruit Salad', do_save=True, ) with new_revision( session=self._session, tm=transaction.manager, content=bad_fruit_salad, ): content_api.delete(bad_fruit_salad) content_api.save(bad_fruit_salad) content_api.create_comment( parent=best_cake_thread, content= '<p>What is for you the best cake ever? </br> I personnally vote for Chocolate cupcake!</p>', # nopep8 do_save=True, ) bob_content_api.create_comment( parent=best_cake_thread, content='<p>What about Apple Pie? There are Awesome!</p>', do_save=True, ) reader_content_api.create_comment( parent=best_cake_thread, content= '<p>You are right, but Kouign-amann are clearly better.</p>', do_save=True, ) with new_revision( session=self._session, tm=transaction.manager, content=best_cake_thread, ): bob_content_api.update_content( item=best_cake_thread, new_content='What is the best cake?', new_label='Best Cakes?', ) bob_content_api.save(best_cake_thread) with new_revision( session=self._session, tm=transaction.manager, content=tiramisu_page, ): bob_content_api.update_content( item=tiramisu_page, new_content= '<p>To cook a great Tiramisu, you need many ingredients.</p>', # nopep8 new_label='Tiramisu Recipe', ) bob_content_api.save(tiramisu_page) self._session.flush()