def view(name=None, format=None, view=None): current_app.db.store.nsBindings = {} entity, content_type = current_app.get_entity_uri(name, format) resource = current_app.get_resource(entity) # 'view' is the default view fileid = resource.value(current_app.NS.whyis.hasFileID) if fileid is not None and 'view' not in request.args: fileid = fileid.value f = None if current_app.nanopub_depot.exists(fileid): f = current_app.nanopub_depot.get(fileid) elif current_app.file_depot.exists(fileid): f = current_app.file_depot.get(fileid) if f is not None: fsa = FileServeApp(f, current_app.config["file_archive"].get("cache_max_age",3600*24*7)) return fsa if content_type is None: content_type = request.headers['Accept'] if 'Accept' in request.headers else 'text/turtle' #print entity fmt = sadi.mimeparse.best_match([mt for mt in list(DATA_FORMATS.keys()) if mt is not None],content_type) if 'view' in request.args or fmt in HTML_MIME_TYPES: return current_app.render_view(resource) elif fmt in DATA_FORMATS: output_graph = ConjunctiveGraph() result, status, headers = current_app.render_view(resource, view='describe') output_graph.parse(data=result, format="json-ld") return output_graph.serialize(format=DATA_FORMATS[fmt]), 200, {'Content-Type':content_type} #elif 'view' in request.args or sadi.mimeparse.best_match(htmls, content_type) in htmls: else: return current_app.render_view(resource)
def view(name=None, format=None, view=None): self.db.store.nsBindings = {} content_type = None if format is not None: if format in extensions: content_type = extensions[format] else: name = '.'.join([name, format]) #argstring = '&'.join(["%s=%s"%(k,v) for k,v in request.args.iteritems(multi=True) if k != 'value']) if name is not None: #if len(argstring) > 0: # name = name + "?" + argstring entity = self.NS.local[name] elif 'uri' in request.args: entity = URIRef(request.args['uri']) else: entity = self.NS.local.Home #print(request.method, 'view()', entity, view) if request.method == 'POST': print ("uploading file",entity) if len(request.files) == 0: flash('No file uploaded') return redirect(request.url) upload_type = rdflib.URIRef(request.form['upload_type']) self.add_files(entity, [y for x, y in request.files.items(multi=True)], upload_type=upload_type) url = "/about?%s" % urlencode(dict(uri=str(entity), view="view")) print ("redirecting to",url) return redirect(url) elif request.method == 'DELETE': self.delete_file(entity) return '', 204 elif request.method == 'GET': resource = self.get_resource(entity) # 'view' is the default view fileid = resource.value(self.NS.whyis.hasFileID) if fileid is not None and 'view' not in request.args: print (resource.identifier, fileid) f = self.file_depot.get(fileid) fsa = FileServeApp(f, self.config["file_archive"].get("cache_max_age",3600*24*7)) return fsa if content_type is None: content_type = request.headers['Accept'] if 'Accept' in request.headers else 'text/turtle' #print entity fmt = sadi.mimeparse.best_match([mt for mt in list(dataFormats.keys()) if mt is not None],content_type) if 'view' in request.args or fmt in htmls: return render_view(resource) elif fmt in dataFormats: output_graph = ConjunctiveGraph() result, status, headers = render_view(resource, view='describe') output_graph.parse(data=result, format="json-ld") return output_graph.serialize(format=dataFormats[fmt]), 200, {'Content-Type':content_type} #elif 'view' in request.args or sadi.mimeparse.best_match(htmls, content_type) in htmls: else: return render_view(resource)
def internal_serve(self, file_id, size=None): obj = self.model.query.filter_by(file_id=file_id).one_or_none() if obj is None: abort(404) if size in Image.IMG_SIZES: file = obj.uploaded_file.get_thumbnail("sm") else: file = obj.uploaded_file.file return FileServeApp(storedfile=file, cache_max_age=0)
def __call__(self, file_id): obj = self.model.query.filter_by(file_id=file_id).one_or_none() if obj is None: abort(404) return FileServeApp(storedfile=obj.uploaded_file.file, cache_max_age=0)
def render_view(resource): template_args = dict(ns=self.NS, this=resource, g=g, current_user=current_user, isinstance=isinstance, get_entity=get_entity, get_summary=get_summary, rdflib=rdflib, hasattr=hasattr, set=set) view = None if 'view' in request.args: view = request.args['view'] # 'view' is the default view fileid = resource.value(self.NS.graphene.hasFileID) if fileid is not None and view is None: f = self.file_depot.get(fileid) fsa = FileServeApp( f, self.config["file_archive"].get("cache_max_age", 3600 * 24 * 7)) return fsa if view is None: view = 'view' if 'as' in request.args: types = [URIRef(request.args['as']), 0] else: types = list([(x.identifier, 0) for x in resource[RDF.type]]) #print types #if len(types) == 0: types.append([self.NS.RDFS.Resource, 100]) #print view, resource.identifier, types type_string = ' '.join( ["(%s %d '%s')" % (x.n3(), i, view) for x, i in types]) view_query = '''select ?id ?view (count(?mid)+?priority as ?rank) ?class ?c where { values (?c ?priority ?id) { %s } ?c rdfs:subClassOf* ?mid. ?mid rdfs:subClassOf* ?class. ?class ?viewProperty ?view. ?viewProperty rdfs:subPropertyOf* graphene:hasView. ?viewProperty dc:identifier ?id. } group by ?c ?class order by ?rank ''' % type_string #print view_query views = list( self.vocab.query(view_query, initNs=dict(graphene=self.NS.graphene, dc=self.NS.dc))) #print '\n'.join([str(x.asdict()) for x in views]) if len(views) == 0: abort(404) headers = {'Content-Type': "text/html"} extension = views[0]['view'].value.split(".")[-1] if extension in extensions: headers['Content-Type'] = extensions[extension] # default view (list of nanopubs) # if available, replace with class view # if available, replace with instance view return render_template(views[0]['view'].value, **template_args), 200, headers