def add_file(self, f, entity, nanopub): entity = rdflib.URIRef(entity) old_nanopubs = [] for np_uri, np_assertion, in self.db.query('''select distinct ?np ?assertion where { hint:Query hint:optimizer "Runtime" . graph ?assertion {?e whyis:hasFileID ?fileid} ?np np:hasAssertion ?assertion. }''', initNs=NS.prefixes, initBindings=dict(e=rdflib.URIRef(entity))): if not self._can_edit(np_uri): raise Unauthorized() old_nanopubs.append((np_uri, np_assertion)) fileid = self.file_depot.create(f.stream, f.filename, f.mimetype) nanopub.add((nanopub.identifier, NS.sio.isAbout, entity)) nanopub.assertion.add((entity, NS.whyis.hasFileID, Literal(fileid))) if current_user._get_current_object() is not None and hasattr(current_user, 'identifier'): nanopub.assertion.add((entity, NS.dc.contributor, current_user.identifier)) nanopub.assertion.add((entity, NS.dc.created, Literal(datetime.utcnow()))) nanopub.assertion.add((entity, NS.ov.hasContentType, Literal(f.mimetype))) nanopub.assertion.add((entity, NS.RDF.type, NS.mediaTypes[f.mimetype])) nanopub.assertion.add((NS.mediaTypes[f.mimetype], NS.RDF.type, NS.dc.FileFormat)) nanopub.assertion.add((entity, NS.RDF.type, NS.mediaTypes[f.mimetype.split('/')[0]])) nanopub.assertion.add((NS.mediaTypes[f.mimetype.split('/')[0]], NS.RDF.type, NS.dc.FileFormat)) nanopub.assertion.add((entity, NS.RDF.type, NS.pv.File)) if current_user._get_current_object() is not None and hasattr(current_user, 'identifier'): nanopub.pubinfo.add((nanopub.assertion.identifier, NS.dc.contributor, current_user.identifier)) nanopub.pubinfo.add((nanopub.assertion.identifier, NS.dc.created, Literal(datetime.utcnow()))) return old_nanopubs
def _can_edit(self, uri): if self.managed: return True if current_user._get_current_object() is None: # This isn't null even when not authenticated, unless we are an autonomic agent. return True if not hasattr(current_user, 'identifier'): # This is an anonymous user. return False if current_user.has_role('Publisher') or current_user.has_role('Editor') or current_user.has_role('Admin'): return True if self.db.query('''ask { ?nanopub np:hasAssertion ?assertion; np:hasPublicationInfo ?info. graph ?info { ?assertion dc:contributor ?user. } }''', initBindings=dict(nanopub=uri, user=current_user.identifier), initNs=dict(np=self.NS.np, dc=self.NS.dc)): #print "Is owner." return True return False
def signup(): user_object = user_signup_schema.load(request.json) if user_object.errors: return BadRequest(message=user_object.errors) check_user = User.query.filter(User.username == user_object.data['username']).first() if check_user: return BadRequest(message='user exist') if user_object.data['password'] != user_object.data['password_repeat']: return BadRequest(message='password not match') user = user_datastore.create_user( username=user_object.data['username'], password=generate_password_hash(user_object.data['password']), active=True) db.session.commit() user_created.send(current_user._get_current_object(), user=user) return jsonify({'result': user_schema.dump(user).data})