Beispiel #1
0
 def _prep_graph(self, resource, about = None):
     #print '_prep_graph', resource.identifier, about
     content_type = resource.value(app.NS.ov.hasContentType)
     #print resource.graph.serialize(format="nquads")
     g = Graph(store=resource.graph.store,identifier=resource.identifier)
     text = resource.value(app.NS.prov.value)
     if content_type is not None and text is not None:
         #print 'Content type:', content_type, resource.identifier
         html = None
         if content_type.value in ["text/html", "application/xhtml+xml"]:
             html = Literal(text.value, datatype=RDF.HTML)
         if content_type.value == 'text/markdown':
             #print "Aha, markdown!"
             #print text.value
             html = markdown.markdown(text.value, extensions=['rdfa'])
             attributes = ['vocab="%s"' % app.NS.local,
                           'base="%s"'% app.NS.local,
                           'prefix="%s"' % ' '.join(['%s: %s'% x for x in app.NS.prefixes.items()])]
             if about is not None:
                 attributes.append('resource="%s"' % about)
             html = '<div %s>%s</div>' % (' '.join(attributes), html)
             html = Literal(html, datatype=RDF.HTML)
             text = html
             content_type = "text/html"
         if html is not None:
             resource.add(app.NS.sioc.content, html)
             try:
                 g.parse(data=text, format='rdfa')
             except:
                 pass
         else:
             try:
                 sadi.deserialize(g, text, content_type)
             except:
                 pass
Beispiel #2
0
            def put(self, ident):
                uri = self._get_uri(ident)
                #print uri
                if not self._can_edit(uri):
                    return app.login_manager.unauthorized()
                inputGraph = Graph()
                contentType = request.headers['Content-Type']
                sadi.deserialize(inputGraph, request.data, contentType)

                nanopub = inputGraph.resource(URIRef(uri + "#assertion"))
                nanopub_text = nanopub.value(app.NS.prov.value)
                rendered_nanopub = markdown.markdown(nanopub_text,
                                                     extensions=['rdfa'])
                nanopub.set(app.NS.sioc.content, Literal(rendered_nanopub))
                nanopub.set(app.NS.dc.modified, Literal(datetime.utcnow()))
                if nanopub.value(app.NS.sioc.reply_of):
                    nanopub.value(app.NS.sioc.reply_of).add(
                        app.NS.sioc.has_reply, nanopub)

                self.local_resource.update(uri, inputGraph)
                return '', 201
Beispiel #3
0
            def post(self, ident=None):
                if ident is not None:
                    return self.put(ident)
                else:
                    default_prefix = URIRef('urn:nanopub')
                    inputGraph = ConjunctiveGraph()
                    contentType = request.headers['Content-Type']

                    sadi.deserialize(inputGraph, request.data, contentType)
                    nanopub_id = ld.create_id()
                    nanopub_uri = app.NS.local["pub/%s" % nanopub_id]
                    processed_graph = ConjunctiveGraph()
                    nanopub_graph = ConjunctiveGraph(
                        identifier=nanopub_uri, store=processed_graph.store)
                    quads = [
                        x for x in ld.rebase(inputGraph, default_prefix,
                                             nanopub_uri)
                    ]
                    processed_graph.addN(quads)

                    assertion, provenance, pubinfo = self._prepare_nanopub(
                        nanopub_graph.resource(nanopub_uri))

                    pubinfo_graph = Graph(processed_graph.store,
                                          pubinfo.identifier)

                    assertion_in_pubinfo = pubinfo_graph.resource(
                        assertion.identifier)
                    assertion_in_pubinfo.add(app.NS.dc.created,
                                             Literal(datetime.utcnow()))
                    assertion_in_pubinfo.add(app.NS.dc.contributor,
                                             current_user.resUri)

                    #print "Adding a nanopublication"
                    print processed_graph.serialize(format="trig")

                    app.db.addN(processed_graph.quads())

                return '', 201
Beispiel #4
0
 def _get_graph(self):
     inputGraph = ConjunctiveGraph()
     contentType = request.headers['Content-Type']
     sadi.deserialize(inputGraph, request.data, contentType)
     return inputGraph