Example #1
0
    def test_restriction(self):
        """JSONAlchemy - restriction"""
        from flask_login import login_user, logout_user
        from invenio.ext.login.legacy_user import UserInfo
        from invenio.modules.jsonalchemy.reader import Reader
        from invenio.modules.jsonalchemy.wrappers import SmartJson

        blob = {'_id': 1}

        json = Reader.translate(blob,
                                SmartJson,
                                model='test_access_base',
                                master_format='json',
                                namespace='testsuite')
        self.assertIsNotNone(json)
        self.assertTrue('restriction' in json)
        self.assertTrue('email' in json['restriction'])
        self.assertTrue(hasattr(json, 'is_authorized'))
        self.assertEqual(json.is_authorized()[0], 0)
        self.assertEqual(json.is_authorized(user_info=UserInfo(1))[0], 0)

        json['restriction']['email'] = UserInfo(1)['email']
        self.assertEqual(json.is_authorized()[0], 1)
        self.assertEqual(json.is_authorized(user_info=UserInfo(1))[0], 0)

        login_user(UserInfo(1))
        self.assertEqual(json.is_authorized()[0], 0)

        logout_user()
        self.assertEqual(json.is_authorized()[0], 1)
Example #2
0
    def test_restriction(self):
        """JSONAlchemy - restriction"""
        from flask.ext.login import login_user, logout_user
        from invenio.ext.login.legacy_user import UserInfo
        from invenio.modules.jsonalchemy.reader import Reader
        from invenio.modules.jsonalchemy.wrappers import SmartJson

        blob = {'_id': 1}

        json = Reader.translate(blob, SmartJson, model='test_access_base',
                                master_format='json', namespace='testsuite')
        self.assertIsNotNone(json)
        self.assertTrue('restriction' in json)
        self.assertTrue('email' in json['restriction'])
        self.assertTrue(hasattr(json, 'is_authorized'))
        self.assertEqual(json.is_authorized()[0], 0)
        self.assertEqual(json.is_authorized(user_info=UserInfo(1))[0], 0)

        json['restriction']['email'] = UserInfo(1)['email']
        self.assertEqual(json.is_authorized()[0], 1)
        self.assertEqual(json.is_authorized(user_info=UserInfo(1))[0], 0)

        login_user(UserInfo(1))
        self.assertEqual(json.is_authorized()[0], 0)

        logout_user()
        self.assertEqual(json.is_authorized()[0], 1)
Example #3
0
 def create(cls, data, model='test_versionable',
            master_format='json', **kwargs):
     document = Reader.translate(
         data, cls, master_format=master_format,
         model=model, namespace='testsuite', **kwargs)
     cls.storage_engine.save_one(document.dumps())
     return document
Example #4
0
 def create(cls, data, model='document_base', master_format='json',
            **kwargs):
     document = Reader.translate(data, cls, master_format=master_format,
                                 model=model, namespace='documentext',
                                 **kwargs)
     cls.storage_engine.save_one(document.dumps())
     signals.document_created.send(document)
     return document
Example #5
0
 def create(cls, data, model='annotation'):
     dic = Reader.translate(data,
                            cls,
                            model=model,
                            master_format='json',
                            namespace="annotationsext")
     cls.storage_engine.save_one(dic.dumps())
     return dic
Example #6
0
 def create(cls,
            data,
            model='test_versionable',
            master_format='json',
            **kwargs):
     document = Reader.translate(data,
                                 cls,
                                 master_format=master_format,
                                 model=model,
                                 namespace='testsuite',
                                 **kwargs)
     cls.storage_engine.save_one(document.dumps())
     return document
Example #7
0
 def create(cls,
            data,
            model='document_base',
            master_format='json',
            **kwargs):
     document = Reader.translate(data,
                                 cls,
                                 master_format=master_format,
                                 model=model,
                                 namespace='documentext',
                                 **kwargs)
     cls.storage_engine.save_one(document.dumps())
     signals.document_created.send(document)
     return document
Example #8
0
    def test_dumps_hidden(self):
        from invenio.modules.jsonalchemy.wrappers import SmartJson
        from invenio.modules.jsonalchemy.reader import Reader

        data = {"title": "Test Title"}

        document = Reader.translate(data, SmartJson, master_format="json", model="test_hidden", namespace="testsuite")

        json = document.dumps()
        self.assertTrue("title" in json)
        self.assertTrue("hidden_basic" in json)

        json = document.dumps(filter_hidden=True)
        self.assertTrue("title" in json)
        self.assertFalse("hidden_basic" in json)
Example #9
0
def translate(blob, master_format, kwargs=None):
    """Translate from the `master_format` to `JSON`.

    :param blob: String contain the input file.
    :param master_format: Format of the blob, it will used to decide which
        reader to use.
    :param kwargs: Arguments to be used by the reader.
        See :class:`invenio.modules.jsonalchemy.reader.Reader`

    :returns: The blob and the `JSON` representation of the input file created
        by the reader.

    """
    return (blob,
            Reader.translate(blob, Record, master_format,
                             **(kwargs or dict())).dumps())
Example #10
0
def translate(blob, master_format, kwargs=None):
    """Translate from the `master_format` to `JSON`.

    :param blob: String contain the input file.
    :param master_format: Format of the blob, it will used to decide which
        reader to use.
    :param kwargs: Arguments to be used by the reader.
        See :class:`invenio.modules.jsonalchemy.reader.Reader`

    :returns: The blob and the `JSON` representation of the input file created
        by the reader.

    """
    return (blob,
            Reader.translate(blob, Record, master_format,
                             **(kwargs or dict())).dumps())
Example #11
0
    def test_dumps_hidden(self):
        from invenio.modules.jsonalchemy.wrappers import SmartJson
        from invenio.modules.jsonalchemy.reader import Reader

        data = {'title': 'Test Title'}

        document = Reader.translate(
            data, SmartJson, master_format='json',
            model='test_hidden', namespace='testsuite')

        json = document.dumps()
        self.assertTrue('title' in json)
        self.assertTrue('hidden_basic' in json)

        json = document.dumps(filter_hidden=True)
        self.assertTrue('title' in json)
        self.assertFalse('hidden_basic' in json)
Example #12
0
    def test_dumps_hidden(self):
        from invenio.modules.jsonalchemy.wrappers import SmartJson
        from invenio.modules.jsonalchemy.reader import Reader

        data = {'title': 'Test Title'}

        document = Reader.translate(data,
                                    SmartJson,
                                    master_format='json',
                                    model='test_hidden',
                                    namespace='testsuite')

        json = document.dumps()
        self.assertTrue('title' in json)
        self.assertTrue('hidden_basic' in json)

        json = document.dumps(filter_hidden=True)
        self.assertTrue('title' in json)
        self.assertFalse('hidden_basic' in json)
Example #13
0
def convert_marcxml_to_bibfield(marcxml, model=None):
    """Return a SmartJson representation of MARC XML string.

    This function converts a MARCXML string to a JSON-like
    dictionary using the the jsonalchemy (aka. BibField) config.

    :param marcxml: MARCXML string to parse
    :type marcxml: string

    :return: SmartJson object.
    """
    from invenio.modules.jsonalchemy.reader import Reader
    from invenio.modules.jsonalchemy.wrappers import SmartJson

    if not model:
        model = ["__default__"]

    if isinstance(marcxml, text_type):
        marcxml = marcxml.encode(errors='ignore')
    return Reader.translate(marcxml,
                            SmartJson,
                            master_format='marc',
                            namespace='recordext',
                            model=model)
Example #14
0
def convert_marcxml_to_bibfield(marcxml, model=None):
    """Return a SmartJson representation of MARC XML string.

    This function converts a MARCXML string to a JSON-like
    dictionary using the the jsonalchemy (aka. BibField) config.

    :param marcxml: MARCXML string to parse
    :type marcxml: string

    :return: SmartJson object.
    """
    from invenio.modules.jsonalchemy.reader import Reader
    from invenio.modules.jsonalchemy.wrappers import SmartJson

    if not model:
        model = ["__default__"]

    if isinstance(marcxml, text_type):
        marcxml = marcxml.encode(errors='ignore')
    return Reader.translate(marcxml,
                            SmartJson,
                            master_format='marc',
                            namespace='recordext',
                            model=model)
Example #15
0
 def create(cls, data, model='annotation'):
     dic = Reader.translate(data, cls, model=model, master_format='json',
                            namespace="annotationsext")
     cls.storage_engine.save_one(dic.dumps())
     return dic
Example #16
0
 def create(cls, blob, master_format, **kwargs):
     """Create a new record from the blob using the right reader."""
     return Reader.translate(blob, Record, master_format, **kwargs)