Beispiel #1
0
 def fun(doc):
     if isinstance(doc, document.Document):
         if doc.specials:
             for s, t in doc.specials.items():
                 if isinstance(t, Geometry):
                     result = mapjson(fun, doc.obj)
                     result = {
                         "type": "Feature",
                         "geometry": doc[s],
                         "properties": result
                     }
                     break
             else:
                 result = mapjson(fun, doc.obj)
             return result
     else:
         return doc
Beispiel #2
0
 def fun(doc):
     if isinstance(doc, document.Document):
         if doc.specials:
             for s, t in doc.specials.items():
                 if isinstance(t, Geometry):
                     result = mapjson(fun, doc.obj)
                     result = {
                         "type": "Feature",
                         "geometry": doc[s],
                         "properties": result
                     }
                     break
             else:
                 result = mapjson(fun, doc.obj)
             return result
     else:
         return doc
Beispiel #3
0
    def __init__(self, obj, collection=None, from_db=False, metadata=None):
        self.collection = collection
        self.saved = from_db
        self.metadata = metadata or {}
        self.obj = OrderedDict()

        if self.collection is not None:
            self.schema = self.collection.schema  # this means it's only calculated once. helpful.
        else:
            self.schema = mapjson(lambda x: x(context=self) if callable(x) else x, self.schema)  # turn URL references into URLs

        self._url = None
        self.constructor(obj)
Beispiel #4
0
    def schema(self):
        ret = {
            "id": self.url + ";schema",
            "title": self.title,
            "type": "object",
            "description": self.__doc__ or "*No description provided.*",
            "definitions": self.definitions,
            "collections": {name: coll.url for name, coll in self._collections.items() if not coll.private},
            "methods": {m.slug: method_schema(None, m) for m in self.exposed_methods.values()}

        }
        ret = mapjson(lambda x: x(context=self.suite) if callable(x) else x, ret)
        return ret
Beispiel #5
0
    def __call__(self, reference, results, **kwargs):

        # handle indent the same way python's json library does
        if 'indent' in kwargs:
            kwargs['indent'] = int(kwargs['indent'])

        if 'ordered' in kwargs:
            ordered = bool(kwargs.get('ordered', False))
            del kwargs['ordered']
        else:
            ordered = False

        # fetch a foreign key reference and append it as if it were part of the document.
        if 'fetch' in kwargs:
            fetch = kwargs['fetch'].split(',')
            del kwargs['fetch']
        else:
            fetch = []

        if 'bare_keys' in kwargs:
            bare_keys = bool(kwargs.get('bare_keys', False))
            del kwargs['bare_keys']
        else:
            bare_keys = False

        print(bare_keys)

        # note this is a closure around the fetch parameter. Consider before refactoring out of the method.
        def serialize(doc):
            if isinstance(doc, document.Document):
                ret = doc.json_repr(ordered=ordered, bare_keys=bare_keys)
                for f in fetch:
                    if f in ret:
                        if isinstance(doc[f], list):
                            ret[f] = [d.json_repr(ordered=ordered, bare_keys=bare_keys) for d in doc[f]]
                        elif isinstance(doc[f], dict):
                            ret[f] = {k: v.json_repr(ordered=ordered, bare_keys=bare_keys) for k, v in doc[f].items()}
                        else:
                            ret[f] = doc[f].json_repr(ordered=ordered, bare_keys=bare_keys)
                return ret
            else:
                return doc

        result = mapjson(serialize, results)  # make sure to serialize a full Document structure if we have one.

        if not (isinstance(result, dict) or isinstance(result, list)):
            result = {"_": result}

        return 'application/json', json.dumps(result, default=json_serial(bare_keys=bare_keys), **kwargs)
Beispiel #6
0
    def __init__(self, obj, collection=None, from_db=False, metadata=None):
        self.collection = collection
        self.saved = from_db
        self.metadata = metadata or {}
        self.obj = OrderedDict()

        if self.collection is not None:
            self.schema = self.collection.schema  # this means it's only calculated once. helpful.
        else:
            self.schema = mapjson(lambda x: x(context=self)
                                  if callable(x) else x,
                                  self.schema)  # turn URL references into URLs

        self._url = None
        self.constructor(obj)
Beispiel #7
0
    def __init__(self, application):
        if self.abstract:
            raise CollectionException("Tried to instantiate an abstract collection")

        signals.pre_init.send(self.__class__, instance=self)
        self.title = self.document_class.title
        self.application = application
        self._url = '/'.join((self.application.url, self.slug))
        self.schema['id'] = self.url + ";schema"
        self.schema = mapjson(lambda x: x(context=self.application.suite) if callable(x) else x, self.schema)
        self.log = logging.getLogger(self.application.name + "." + self.name)

        if self.autocomplete_props is None:
            self.autocomplete_props = (self.primary_key,)

        if self.file_storage:
            self.file_storage = self.file_storage(self)

        signals.post_init.send(self.__class__, instance=self)
Beispiel #8
0
    def __init__(self, application):
        if self.abstract:
            raise CollectionException("Tried to instantiate an abstract collection")

        signals.pre_init.send(self.__class__, instance=self)
        self.title = self.document_class.title
        self.application = application
        self._url = '/'.join((self.application.url, self.slug))
        self.schema['id'] = self.url + ";schema"
        self.schema = mapjson(lambda x: x(context=self.application.suite) if callable(x) else x, self.schema)
        self.log = logging.getLogger(self.application.name + "." + self.name)

        if self.autocomplete_props is None:
            self.autocomplete_props = (self.primary_key,)

        if self.file_storage:
            self.file_storage = self.file_storage(self)

        signals.post_init.send(self.__class__, instance=self)
Beispiel #9
0
    def __call__(self, reference, result, **kwargs):
        if 'geom' in kwargs:
            geometry_field = kwargs['geom']
        else:
            geometry_field = None

        def fun(doc):
            if isinstance(doc, document.Document):
                if doc.specials:
                    for s, t in doc.specials.items():
                        if isinstance(t, Geometry):
                            result = mapjson(fun, doc.obj)
                            result = {
                                "type": "Feature",
                                "geometry": doc[s],
                                "properties": result
                            }
                            break
                    else:
                        result = mapjson(fun, doc.obj)
                    return result
            else:
                return doc

        if 'indent' in kwargs:
            kwargs['indent'] = int(kwargs['indent'])

        if 'ordered' in kwargs:
            ordered = bool(kwargs.get('ordered', False))
            del kwargs['ordered']


        result = mapjson(fun, result)  # make sure to serialize a full Document structure if we have one.

        if isinstance(result, list):
            ret = {
                "type": "FeatureCollection",
                "features": result
            }
        else:
            ret = result
        return 'application/json', json.dumps(ret, indent=4)
Beispiel #10
0
    def __call__(self, reference, result, **kwargs):
        if 'geom' in kwargs:
            geometry_field = kwargs['geom']
        else:
            geometry_field = None

        def fun(doc):
            if isinstance(doc, document.Document):
                if doc.specials:
                    for s, t in doc.specials.items():
                        if isinstance(t, Geometry):
                            result = mapjson(fun, doc.obj)
                            result = {
                                "type": "Feature",
                                "geometry": doc[s],
                                "properties": result
                            }
                            break
                    else:
                        result = mapjson(fun, doc.obj)
                    return result
            else:
                return doc

        if 'indent' in kwargs:
            kwargs['indent'] = int(kwargs['indent'])

        if 'ordered' in kwargs:
            ordered = bool(kwargs.get('ordered', False))
            del kwargs['ordered']

        result = mapjson(
            fun, result
        )  # make sure to serialize a full Document structure if we have one.

        if isinstance(result, list):
            ret = {"type": "FeatureCollection", "features": result}
        else:
            ret = result
        return 'application/json', json.dumps(ret, indent=4)
Beispiel #11
0
    def __init__(self, obj, collection=None, parent=None):
        self.collection = collection
        if self.collection:
            self.schema = self.collection.schema  # this means it's only calculated once. helpful.
        else:
            self.schema = mapjson(lambda x: x(context=x) if callable(x) else x, self.schema)  # turn URL references into URLs

        # todo add methods to the schema.

        if not self.collection and (parent and parent.collection):
            self.collection = parent.collection

        self.parent = parent

        self._url = None
        if self.collection.primary_key in obj:
            self._url = '/'.join((self.collection.url, obj[self.collection.primary_key]))

        self._referenced = True
        self.obj = {}
        if obj:
            for k, v in obj.items():
                self[k] = v
Beispiel #12
0
    def __call__(self, reference, results, **kwargs):

        # handle indent the same way python's json library does
        if 'indent' in kwargs:
            kwargs['indent'] = int(kwargs['indent'])

        if 'ordered' in kwargs:
            ordered = bool(kwargs.get('ordered', False))
            del kwargs['ordered']
        else:
            ordered = False

        # fetch a foreign key reference and append it as if it were part of the document.
        if 'fetch' in kwargs:
            fetch = kwargs['fetch'].split(',')
            del kwargs['fetch']
        else:
            fetch = []

        if 'bare_keys' in kwargs:
            bare_keys = bool(kwargs.get('bare_keys', False))
            del kwargs['bare_keys']
        else:
            bare_keys = False

        # note this is a closure around the fetch parameter. Consider before refactoring out of the method.
        def serialize(doc):
            if isinstance(doc, document.Document):
                ret = doc.json_repr(ordered=ordered, bare_keys=bare_keys)
                for f in fetch:
                    if f in ret:
                        if isinstance(doc[f], list):
                            ret[f] = [
                                d.json_repr(ordered=ordered,
                                            bare_keys=bare_keys)
                                for d in doc[f]
                            ]
                        elif isinstance(doc[f], dict):
                            ret[f] = {
                                k: v.json_repr(ordered=ordered,
                                               bare_keys=bare_keys)
                                for k, v in doc[f].items()
                            }
                        else:
                            ret[f] = doc[f].json_repr(ordered=ordered,
                                                      bare_keys=bare_keys)
                return ret
            else:
                return doc

        result = mapjson(
            serialize, results
        )  # make sure to serialize a full Document structure if we have one.

        if not (isinstance(result, dict) or isinstance(result, list)):
            result = {"_": result}

        from json2html import json2html
        rsp = StringIO()
        rsp.write("""<!doctype html>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">

</head><body>
<!-- Latest compiled and minified JavaScript -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
""")
        rsp.write(
            json2html.convert(
                json=result,
                table_attributes="class=\"table table-bordered table-hover\""))
        rsp.write('</body></html>')
        return 'text/html', rsp.getvalue()
Beispiel #13
0
 def persist_document_files(self, document):
     document.obj = mapjson(partial(_persist, self, document), document.obj)
     return document
Beispiel #14
0
 def persist_document_files(self, document):
     document.obj = mapjson(partial(_persist, self, document), document.obj)
     return document
Beispiel #15
0
    def __call__(self, reference, results, **kwargs):

        # handle indent the same way python's json library does
        if 'indent' in kwargs:
            kwargs['indent'] = int(kwargs['indent'])

        if 'ordered' in kwargs:
            ordered = bool(kwargs.get('ordered', False))
            del kwargs['ordered']
        else:
            ordered = False

        # fetch a foreign key reference and append it as if it were part of the document.
        if 'fetch' in kwargs:
            fetch = kwargs['fetch'].split(',')
            del kwargs['fetch']
        else:
            fetch = []

        if 'bare_keys' in kwargs:
            bare_keys = bool(kwargs.get('bare_keys', False))
            del kwargs['bare_keys']
        else:
            bare_keys = False

        print(bare_keys)

        # note this is a closure around the fetch parameter. Consider before refactoring out of the method.
        def serialize(doc):
            if isinstance(doc, document.Document):
                ret = doc.json_repr(ordered=ordered, bare_keys=bare_keys)
                for f in fetch:
                    if f in ret:
                        if isinstance(doc[f], list):
                            ret[f] = [
                                d.json_repr(ordered=ordered,
                                            bare_keys=bare_keys)
                                for d in doc[f]
                            ]
                        elif isinstance(doc[f], dict):
                            ret[f] = {
                                k: v.json_repr(ordered=ordered,
                                               bare_keys=bare_keys)
                                for k, v in doc[f].items()
                            }
                        else:
                            ret[f] = doc[f].json_repr(ordered=ordered,
                                                      bare_keys=bare_keys)
                return ret
            else:
                return doc

        result = mapjson(
            serialize, results
        )  # make sure to serialize a full Document structure if we have one.

        if not (isinstance(result, dict) or isinstance(result, list)):
            result = {"_": result}

        return 'application/json', json.dumps(
            result, default=json_serial(bare_keys=bare_keys), **kwargs)
Beispiel #16
0
    def __call__(self, reference, results, **kwargs):

        # handle indent the same way python's json library does
        if 'indent' in kwargs:
            kwargs['indent'] = int(kwargs['indent'])

        if 'ordered' in kwargs:
            ordered = bool(kwargs.get('ordered', False))
            del kwargs['ordered']
        else:
            ordered = False

        # fetch a foreign key reference and append it as if it were part of the document.
        if 'fetch' in kwargs:
            fetch = kwargs['fetch'].split(',')
            del kwargs['fetch']
        else:
            fetch = []

        if 'bare_keys' in kwargs:
            bare_keys = bool(kwargs.get('bare_keys', False))
            del kwargs['bare_keys']
        else:
            bare_keys = False

        # note this is a closure around the fetch parameter. Consider before refactoring out of the method.
        def serialize(doc):
            if isinstance(doc, document.Document):
                ret = doc.json_repr(ordered=ordered, bare_keys=bare_keys)
                for f in fetch:
                    if f in ret:
                        if isinstance(doc[f], list):
                            ret[f] = [d.json_repr(ordered=ordered, bare_keys=bare_keys) for d in doc[f]]
                        elif isinstance(doc[f], dict):
                            ret[f] = {k: v.json_repr(ordered=ordered, bare_keys=bare_keys) for k, v in doc[f].items()}
                        else:
                            ret[f] = doc[f].json_repr(ordered=ordered, bare_keys=bare_keys)
                return ret
            else:
                return doc

        result = mapjson(serialize, results)  # make sure to serialize a full Document structure if we have one.

        if not (isinstance(result, dict) or isinstance(result, list)):
            result = {"_": result}

        from json2html import json2html
        rsp = StringIO()
        rsp.write("""<!doctype html>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css">

</head><body>
<!-- Latest compiled and minified JavaScript -->
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
""")
        rsp.write(json2html.convert(json=result, table_attributes="class=\"table table-bordered table-hover\""))
        rsp.write('</body></html>')
        return 'text/html',rsp.getvalue()
Beispiel #17
0
 def dereference(self):
     self.obj = utils.mapjson(self._from_ref, self.obj)
     self._referenced = False
     return self