diff --git a/invenio/modules/annotations/api.py b/invenio/modules/annotations/api.py index 63de3be53..c188fcf0f 100644 --- a/invenio/modules/annotations/api.py +++ b/invenio/modules/annotations/api.py @@ -1,142 +1,139 @@ # -*- coding: utf-8 -*- ## ## This file is part of Invenio. ## Copyright (C) 2014 CERN. ## ## Invenio is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License as ## published by the Free Software Foundation; either version 2 of the ## License, or (at your option) any later version. ## ## Invenio is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Invenio; if not, write to the Free Software Foundation, Inc., ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. from invenio.base.globals import cfg from invenio.modules.jsonalchemy.reader import Reader from invenio.modules.jsonalchemy.wrappers import SmartJsonLD class QueryIterator(): def __init__(self, cls, cursor): self.cursor = cursor self.i = -1 self.cls = cls def __iter__(self): return self def next(self): self.i += 1 if self.i >= self.cursor.count(): raise StopIteration else: return self.cls(self.cursor[self.i]) def count(self): return self.cursor.count() def __getitem__(self, i): return self.cls(self.cursor[i]) class Annotation(SmartJsonLD): __storagename__ = 'annotations' @classmethod - def create(cls, data, model='annotation', store=True): + def create(cls, data, model='annotation'): dic = Reader.translate(data, cls, model=model, master_format='json', namespace="annotationsext") - if store: - return cls.storage_engine.save_one(dic.dumps()) - else: - # useful for testing, until we have a mock DB - return dic + cls.storage_engine.save_one(dic.dumps()) + return dic @classmethod def search(cls, query): return QueryIterator(cls, cls.storage_engine.search(query)) def translate(self, context_name, ctx): dump = self.dumps() res = {} if context_name == "oaf": from invenio.modules.accounts.models import User res["@id"] = cfg["CFG_SITE_URL"] + \ "/api/annotations/export/?_id=" + \ dump["_id"] res["@type"] = "oa:Annotation" u = User.query.filter(User.id == dump["who"]).one() res["annotatedBy"] = { "@id": cfg["CFG_SITE_URL"] + "/api/accounts/account/?id=" + str(u.id), "@type": "foaf:Person", "name": u.nickname, "mbox": {"@id": "mailto:" + u.email}} if "annotation" in self.model_info["names"]: res["hasTarget"] = { "@type": ["cnt:ContentAsXML", "dctypes:Text"], "@id": cfg["CFG_SITE_URL"] + dump["where"], "cnt:characterEncoding": "utf-8", "format": "text/html"} elif "annotation_note" in self.model_info["names"]: res["hasTarget"] = { "@id": "oa:hasTarget", "@type": "oa:SpecificResource", "hasSource": cfg["CFG_SITE_URL"] + "/record/" + str(dump["where"]["record"]), "hasSelector": { "@id": "oa:hasSelector", "@type": "oa:FragmentSelector", "value": dump["where"]["marker"], "dcterms:conformsTo": cfg["CFG_SITE_URL"] + "/api/annotations/notes_specification"}} res["motivatedBy"] = "oa:commenting" res["hasBody"] = { "@id": "oa:hasBody", "@type": ["cnt:ContentAsText", "dctypes:Text"], "chars": dump["what"], "cnt:characterEncoding": "utf-8", "format": "text/plain"} res["annotatedAt"] = dump["when"] return res raise NotImplementedError def get_jsonld_multiple(annos, context="oaf", new_context={}, format="full"): return [a.get_jsonld(context=context, new_context=new_context, format=format) for a in annos] -def add_annotation(model='annotation', store=True, **kwargs): - return Annotation.create(kwargs, model, store=store) +def add_annotation(model='annotation', **kwargs): + return Annotation.create(kwargs, model) def get_annotations(which): return Annotation.search(which) def get_count(uid, target): private = 0 if uid: private = get_annotations({"where": target, "who": uid, "perm": {"public": False, "groups": []}}).count() public = get_annotations({"where": target, "perm": {"public": True, "groups": []}}).count() return {"public": public, "private": private, "total": public+private} diff --git a/invenio/modules/annotations/testsuite/test_api.py b/invenio/modules/annotations/testsuite/test_api.py index a3d2e6af1..264b82c50 100644 --- a/invenio/modules/annotations/testsuite/test_api.py +++ b/invenio/modules/annotations/testsuite/test_api.py @@ -1,90 +1,96 @@ # -*- coding: utf-8 -*- ## ## This file is part of Invenio. ## Copyright (C) 2014 CERN. ## ## Invenio is free software; you can redistribute it and/or ## modify it under the terms of the GNU General Public License as ## published by the Free Software Foundation; either version 2 of the ## License, or (at your option) any later version. ## ## Invenio is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with Invenio; if not, write to the Free Software Foundation, Inc., ## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. __revision__ = "$Id$" from datetime import datetime from invenio.base.wrappers import lazy_import from invenio.testsuite import make_test_suite, run_test_suite, nottest, \ InvenioTestCase CFG = lazy_import('invenio.base.globals.cfg') USER = lazy_import('invenio.modules.accounts.models.User') API = lazy_import('invenio.modules.annotations.api') NOTEUTILS = lazy_import('invenio.modules.annotations.noteutils') COMMENT = lazy_import('invenio.modules.comments.models.CmtRECORDCOMMENT') -class TestAnnotation(InvenioTestCase): +class AnnotationTestCase(InvenioTestCase): + + def setUp(self): + self.app.config['ANNOTATIONS_ENGINE'] = \ + "invenio.modules.jsonalchemy.jsonext.engines.memory:MemoryStorage" + + +class TestAnnotation(AnnotationTestCase): + def test_initialization(self): u = USER(id=1) - a = API.Annotation.create({"who": u, "what": "lorem", "where": "/"}, - store=False) + a = API.Annotation.create({"who": u, "what": "lorem", "where": "/"}) self.assert_(len(a.validate()) == 0) self.assert_(type(a["when"]) == datetime) self.assert_(a["who"].get_id() == 1) # invalid annotation a = API.Annotation.create({"who": u, "what": "lorem", "where": "/", "perm": {"public": True, "groups": []}, - "uuid": "1m"}, - store=False) + "uuid": "1m"}) self.assert_(len(a.validate()) == 1) def test_jsonld(self): u = USER(id=1, nickname="johndoe") a = API.Annotation.create({"who": u, "what": "lorem", "where": "/", - "perm": {"public": True, "groups": []}}, - store=False) + "perm": {"public": True, "groups": []}}) ld = a.get_jsonld("oaf") self.assert_(ld["hasTarget"]["@id"] == CFG["CFG_SITE_URL"] + "/") self.assert_(ld["hasBody"]["chars"] == "lorem") -class TestJSONLD(InvenioTestCase): +class TestJSONLD(AnnotationTestCase): @nottest def test(self): u = USER(id=1) data = {"who": u, "what": "lorem", "where": {"record": 1, "marker": "P.1_T.2a.2_L.100"}, "comment": 1} - a = API.add_annotation(model='annotation_note', store=False, **data) + a = API.add_annotation(model='annotation_note', **data) # JSONAlchemy issue with overwritting fields self.assert_(len(a.validate()) == 0) ld = a.get_jsonld("oaf", new_context={"ST": "http://www.w3.org/ns/oa#" "FragmentSelector"}, format="compacted") self.assert_(ld["http://www.w3.org/ns/oa#hasTarget"] - ["http://www.w3.org/ns/oa#hasSelector"]["@type"] == "ST") + ["http://www.w3.org/ns/oa#hasSelector"] + ["@type"] == "ST") self.assert_(ld["http://www.w3.org/ns/oa#hasTarget"] ["http://www.w3.org/ns/oa#hasSelector"] ["http://www.w3.org/1999/02/22-rdf-syntax-ns#value"] == "P.1_T.2a.2_L.100") TEST_SUITE = make_test_suite(TestAnnotation, TestJSONLD) if __name__ == "__main__": run_test_suite(TEST_SUITE)