Beispiel #1
0
 def _create_new_entity_doc(self, aggregation_paths, centroid, entity_type,
                            geometry, gr_id, id, location, short_code):
     doc = EntityDocument(id)
     self._set_document(doc)
     # add aggregation paths
     if is_string(entity_type):
         entity_type = [entity_type]
     doc.entity_type = entity_type
     if location is not None:
         doc.location = location
     if geometry is not None:
         doc.geometry = geometry
     if centroid is not None:
         doc.centroid = centroid
     if gr_id is not None:
         doc.gr_id = gr_id
     if short_code is not None:
         doc.short_code = short_code
     if aggregation_paths is not None:
         reserved_names = (attributes.TYPE_PATH, attributes.GEO_PATH)
         for name in aggregation_paths.keys():
             if name in reserved_names:
                 raise ValueError(
                     u'Attempted to add an aggregation path with a reserved name'
                 )
             self.set_aggregation_path(name, aggregation_paths[name])
Beispiel #2
0
def _entity_by_short_code(dbm, short_code, entity_type):
    rows = dbm.view.entity_by_short_code(key=[entity_type, short_code],
                                         include_docs=True)
    if is_empty(rows):
        raise DataObjectNotFound("Entity", "Unique Identification Number (ID)",
                                 short_code)
    doc = EntityDocument.wrap(rows[0]['doc'])
    return Entity.new_from_doc(dbm, doc)
Beispiel #3
0
    def __init__(self, dbm, entity_type=None, location=None, aggregation_paths=None,
                 geometry=None, centroid=None, gr_id=None, id=None, short_code=None):
        """
        Construct a new entity.

        Note: _couch_document is used for 'protected' factory methods and
        should not be passed in standard construction.

        If _couch_document is passed, the other args are ignored

        entity_type may be a string (flat type) or sequence (hierarchical type)
        """
        assert isinstance(dbm, DatabaseManager)
        assert entity_type is None or is_sequence(entity_type) or is_string(entity_type)
        assert location is None or is_sequence(location)
        assert aggregation_paths is None or isinstance(aggregation_paths, dict)
        assert geometry is None or isinstance(geometry, dict)
        assert centroid is None or isinstance(centroid, list)
        assert gr_id is None or is_string(gr_id)
        DataObject.__init__(self, dbm)

        # Are we being constructed from an existing doc, in which case all the work is
        # in _set_document?
        if entity_type is None:
            return

        # Not made from existing doc, so create a new one
        doc = EntityDocument(id)
        self._set_document(doc)

        # add aggregation paths
        if is_string(entity_type):
            entity_type = [entity_type]
        doc.entity_type = entity_type

        if location is not None:
            doc.location = location

        if geometry is not None:
            doc.geometry = geometry

        if centroid is not None:
            doc.centroid = centroid

        if gr_id is not None:
            doc.gr_id = gr_id

        if short_code is not None:
            doc.short_code = short_code

        if aggregation_paths is not None:
            reserved_names = (attributes.TYPE_PATH, attributes.GEO_PATH)
            for name in aggregation_paths.keys():
                if name in reserved_names:
                    raise ValueError(u'Attempted to add an aggregation path with a reserved name')
                self.set_aggregation_path(name, aggregation_paths[name])
Beispiel #4
0
def by_short_codes(dbm, short_codes, entity_type, limit=None):
    kwargs = {'include_docs': True, 'reduce': False}
    if limit:
        kwargs['limit'] = limit

    keys = [[entity_type, short_code] for short_code in short_codes]
    kwargs["keys"] = keys

    rows = dbm.view.by_short_codes(**kwargs)
    if is_empty(rows):
        raise DataObjectNotFound(entity_type[0],
                                 "Unique Identification Number (ID)", "")
    docs = [EntityDocument.wrap(row['doc']) for row in rows]
    return [Entity.new_from_doc(dbm, doc) for doc in docs]
Beispiel #5
0
    def __init__(self,
                 dbm,
                 entity_type=None,
                 location=None,
                 aggregation_paths=None,
                 geometry=None,
                 centroid=None,
                 gr_id=None,
                 id=None,
                 short_code=None):
        """
        Construct a new entity.

        Note: _couch_document is used for 'protected' factory methods and
        should not be passed in standard construction.

        If _couch_document is passed, the other args are ignored

        entity_type may be a string (flat type) or sequence (hierarchical type)
        """
        assert isinstance(dbm, DatabaseManager)
        assert entity_type is None or is_sequence(entity_type) or is_string(
            entity_type)
        assert location is None or is_sequence(location)
        assert aggregation_paths is None or isinstance(aggregation_paths, dict)
        assert geometry is None or isinstance(geometry, dict)
        assert centroid is None or isinstance(centroid, list)
        assert gr_id is None or is_string(gr_id)
        DataObject.__init__(self, dbm)

        # Are we being constructed from an existing doc, in which case all the work is
        # in _set_document?
        if entity_type is None:
            return

        # Not made from existing doc, so create a new one
        doc = EntityDocument(id)
        self._set_document(doc)

        # add aggregation paths
        if is_string(entity_type):
            entity_type = [entity_type]
        doc.entity_type = entity_type

        if location is not None:
            doc.location = location

        if geometry is not None:
            doc.geometry = geometry

        if centroid is not None:
            doc.centroid = centroid

        if gr_id is not None:
            doc.gr_id = gr_id

        if short_code is not None:
            doc.short_code = short_code

        if aggregation_paths is not None:
            reserved_names = (attributes.TYPE_PATH, attributes.GEO_PATH)
            for name in aggregation_paths.keys():
                if name in reserved_names:
                    raise ValueError(
                        u'Attempted to add an aggregation path with a reserved name'
                    )
                self.set_aggregation_path(name, aggregation_paths[name])