Example #1
0
 def __init__(self, **kwargs):
     Document.__init__(self, **kwargs)
     self.caller_phone = PhoneNumber.standardize(kwargs.get('caller_phone'))
     self.dialed_phone = PhoneNumber.standardize(kwargs.get('dialed_phone'))
     self.answer_phone = PhoneNumber.standardize(kwargs.get('answer_phone'))
     self.timestamp = CallTimeStamp.standardize(kwargs.get('timestamp'))
     self.duration = CallDuration.sanitize(kwargs.get('duration'))
Example #2
0
 def to_dict(document: Document):
     raw = BsonEncoder().encode(document.to_mongo())
     document = loads(raw)
     fields = ['_id', 'inserted_at', 'updated_at', 'is_deleted']
     for key in document.copy().keys():
         if key in fields:
             del document[key]
     return document
Example #3
0
 def save(self, *args, **kwargs):
     Document.save(self, *args, **kwargs)
     from elasticsearch import Elasticsearch
     es = Elasticsearch(hosts=app.config.get('ELASTICSEARCH_HOSTS'))
     es.index(index='campus_list', doc_type='campus', id=str(self.id), body={
         'univ_name': '%s%s' % (self.univ_name, self.univ_type),
         'campus_name': self.campus_name
     })
Example #4
0
 def save(self, safe=True, force_insert=False, validate=True):
     if self.publish_time is None:
         self.publish_time = datetime.now()
     if self.slug is None:
         self.slug = self.publish_time.strftime("%Y.%b") + "-" + self.title.lower().replace(" ", "-")
     if self.publish_day is None:
         self.publish_day = int(self.publish_time.strftime("%Y%m%d"))
     Document.save(self, safe=safe, force_insert=force_insert, validate=validate)
Example #5
0
    def __init__(self, facebook_id, nome, *args, **kwargs):
        Document.__init__(self, *args, **kwargs)

        if not nome:
            raise ValueError('nome')

        if not facebook_id:
            raise ValueError('facebook_id')

        self.nome = nome
        self.facebook_id = facebook_id
Example #6
0
def delete_user(schema: mongoengine.Document, chat_id: str) -> str:
    try:
        if not db_utils.check_account_exist(
                schema, chat_id) or not schema.objects(chat_id=chat_id):
            raise mongoengine.DoesNotExist(f'No such account: {chat_id}')
        target = schema.objects(chat_id=chat_id)
        target.delete()
        return f'{chat_id} removed'
    except mongoengine.DoesNotExist as e:
        return f'{e}'
    except Exception as e:
        return f'{e}'
Example #7
0
    def validate(self, clean=True):
        """Calls the parent `validate(self, clean)`. If `ValidationError` is raised, checks if
        the error is about the emptiness of the field `name`. If yes, raises a new properly
        formatted `ValidationError`, if no raises the error as it is.

        :param self: the document to validate.
        :param clean: whether to run the `clean(self)` function or not.
        """
        try:
            Document.validate(self, clean)
        except ValidationError as e:
            error = e.errors.get('__all__')
            raise ValidationError(errors={error.field_name: error.message}) if error is not None else e
Example #8
0
def app() -> Flask:
    yield create_app('testing')

    # DB cleanups after each run
    db = Document._get_db()
    for name in db._collections.keys():
        db._collections[name]._documents = OrderedDict()
Example #9
0
def to_json(doc: Document):
    if isinstance(doc, list):
        t = []
        for d in doc:
            t.append(to_json(d))
        return t
    return json.loads(doc.to_json())
Example #10
0
 def delete(self):
     redis = getattr(self, '__redis__', False)
     if redis:
         key = self._getKey(self.guid)
         j.core.db.delete(key)
     else:
         return Document.delete(self)
def import_document(document: Document, rows: List, name_field: str,
                    last_name_field: str, birth_date_field: str,
                    birth_place_field: str, fiscal_code_field: str,
                    address_field: str, zip_code_field: str, city_field: str,
                    province_field: str, gender_field: str, phone_field: str,
                    email_field: str) -> List:
    documents = []
    for row in rows:
        date = row[birth_date_field].split("/")
        birth_date = "-".join([date[2], date[1], date[0]
                               ]) if len(date) > 1 else ""

        doc = document(name=row.get(name_field, ""),
                       last_name=row.get(last_name_field, ""),
                       birth_date=birth_date,
                       birth_place=row.get(birth_place_field),
                       fiscal_code=row.get(fiscal_code_field),
                       address=row.get(address_field, ""),
                       zip_code=row.get(zip_code_field, ""),
                       city=row.get(city_field, ""),
                       province=row.get(province_field, ""),
                       gender=row.get(gender_field, ""),
                       phone=row.get(phone_field, ""),
                       email=row.get(email_field, ""))

        if not doc.is_empty():
            documents.append(doc)

    document.objects.insert(documents)
    return [document.jsonify() for document in documents]
Example #12
0
 def save(self, data=None):
     redis = getattr(self, '__redis__', False)
     if data:
         self._datatomodel(data)
     if redis:
         return self._save_redis(self)
     else:
         return Document.save(self)
    def __init__(self, **data):
        location = data['location']
        try:
            tlsverify = data['tlsverify']
            logger = data['logger']
            mbox_resource_connector = data['resource_connector']
            Resource.__init__(self, location=location,
                              tlsverify=tlsverify,
                              logger=logger,
                              resource_connector=mbox_resource_connector)
        except KeyError:
            pass
        
        Document.__init__(self)

        self.location = location
        self.last_updated = datetime.now()
Example #14
0
    def _doc_to_tuples(self, doc: db.Document, with_ids=True):
        """
        Todo: to be removed
        """
        if not doc:
            return

        table = doc._get_collection_name()

        d_json = json.loads(doc.to_json())
        d_json["id"] = str(doc.id)
        del d_json["_id"]
        ukey = tuple(str(doc[key]) for key in self._table_indices[table])
        if with_ids:
            rdata = (ukey, str(doc.id))
        else:
            rdata = ukey
        return rdata
Example #15
0
def _query(doc: Document,
           filters: tuple = None,
           page: int = 0,
           per_page: int = 0,
           order_by: str = None):
    print('\nQuery params:\n')
    print('filters:%s\norder_by:%s\npage:%d per_page:%d' %
          (filters, order_by, page, per_page))
    objects = None
    if filters:
        objects = doc.objects(**filters)
    else:
        objects = doc.objects()
    if order_by is not None:
        objects = objects.order_by(*order_by)
    if per_page > 0 and per_page > 0:
        objects = objects.limit(per_page).skip((page - 1) * per_page)
    return objects
Example #16
0
def _create_membership(document: Document, id: str):
    if not bson.ObjectId.is_valid(id):
        flask.abort(400)

    start_date = flask.request.form.get('start_date')
    end_date = flask.request.form.get('end_date')

    if start_date is None or end_date is None:
        flask.abort(400)

    document: Document = document.objects(id=id).first()
    if not document:
        flask.abort(404)

    document.memberships.create(start_date=start_date, end_date=end_date)

    document.save()

    return flask.jsonify(document.jsonify())
Example #17
0
 def delete_metadata(cls, obj: Document, keys: Sequence[str],
                     **more_updates) -> int:
     with TimingContext("mongo", "delete_metadata"):
         return obj.update(
             **{
                 f"unset__metadata__{ParameterKeyEscaper.escape(key)}": 1
                 for key in set(keys)
             },
             **more_updates,
         )
Example #18
0
 def remove_document(self, document: Document, id: Text, bot: Text,
                     user: Text):
     try:
         doc = document.objects(bot=bot).get(id=id)
         doc.update(status=False, user=user)
     except DoesNotExist as e:
         logging.info(e)
         raise AppException("Unable to remove document")
     except Exception as e:
         logging.info(e)
         raise AppException("Unable to remove document")
Example #19
0
    def _doc_to_json(self, doc: db.Document, with_ids=True):
        """Rename _id to id, or remove it altogether"""

        if not doc:
            return

        d_json = json.loads(doc.to_json())
        if with_ids:
            d_json["id"] = str(doc.id)

        del d_json["_id"]

        return d_json
Example #20
0
def _query(doc: Document,
           filters: tuple = None,
           page: int = 1,
           per_page: int = 100,
           order_by: str = None) -> str:
    """数据库查询函数

    :param doc: Document的子类型,从哪个collection 中获取数据
    :type doc: Document
    :param filters: QuerySet 的 filter 参数, defaults to None
    :type filters: tuple, optional
    :param page: 数据分页,此处表示页码, defaults to 1
    :type page: int, optional
    :param per_page: 每页文档个数, defaults to 100
    :type per_page: int, optional
    :param order_by: QuerySet 的 order_by, defaults to None
    :type order_by: str, optional
    :return: collection 的 JSON 串
    :rtype: str
    """
    print('\nQuery params:\n')
    print('filters:%s\norder_by:%s\npage:%d per_page:%d' %
          (filters, order_by, page, per_page))
    objects = None
    if filters:
        objects = doc.objects(**filters)
    else:
        objects = doc.objects()
    if per_page > 0 and per_page > 0:
        objects = objects.limit(per_page).skip((page - 1) * per_page)
    if order_by is not None:
        objects = objects.order_by(*order_by)
    if objects is not None and objects.count() > 0:
        return objects.to_json()
    print('Failed to get data from db\n')
    return None
    def _post_teardown(self):
        for collection in self._database.collection_names():
            if collection == 'system.indexes':  # pragma: no cover
                continue

            self._database.drop_collection(collection)

        # Mongoengine models need to forget about their collection (to recreate indexes). Hackish, I know.
        # FIXME: __subclasses__ may only take direct descendants into account!
        for model in Document.__subclasses__():
            if hasattr(model, '_collection'):
                del model._collection

        disconnect()

        super(MongoTestCase, self)._post_teardown()
    def _post_teardown(self):
        super(MongoTestCase, self)._post_teardown()

        for collection in self.database.collection_names():
            if collection == 'system.indexes':
                continue

            self.database.drop_collection(collection)

        # Mongoengine models need to forget about their collection (to recreate indexes). Hackish, I know.
        # pylint: disable=E1101
        for model in Document.__subclasses__():
            if hasattr(model, '_collection'):
                del model._collection

        disconnect()
Example #23
0
def _delete_membership(document: Document, id: str, membership_id: str):
    if not bson.ObjectId.is_valid(id):
        flask.abort(400)

    document: Document = document.objects(id=id).first()
    if not document:
        flask.abort(404)
    document.update(pull__memberships___id=membership_id)
    document.reload()

    return flask.jsonify(document.jsonify())
Example #24
0
    def edit_metadata(
        cls,
        obj: Document,
        items: Sequence[MetadataItem],
        replace_metadata: bool,
        **more_updates,
    ) -> int:
        with TimingContext("mongo", "edit_metadata"):
            update_cmds = dict()
            metadata = cls.metadata_from_api(items)
            if replace_metadata:
                update_cmds["set__metadata"] = metadata
            else:
                for key, value in metadata.items():
                    update_cmds[
                        f"set__metadata__{mongoengine_safe(key)}"] = value

            return obj.update(**update_cmds, **more_updates)
Example #25
0
def doc_to_dict(doc: Document):
    """Converts a mongoengine document into a json serializable dictionary.

    This implementation is naive and doesnt take into account name aliases. It
    may also have issues with certain field types

    If the above potentials become a problem, see https://gist.github.com/jason-w/4969476

    :param mongoengine.Document doc: A mongo document extracted through or
        created from mongoengine
    :returns dict:
    """
    retval: dict = doc.to_mongo()

    if '_id' in retval:
        retval['_id'] = str(retval['_id'])

    return retval
Example #26
0
def render_document(doc: Document, view: BaseModel, exclude: Set[str] = None):
    """Converts a mongoengine document into a json serializable dictionary.

    This is not great.

    :param mongoengine.Document doc: A mongo document extracted through or
        created from mongoengine.
    :param pydantic.BaseModel view: The view to use to serialize the data.
    :param set exclude: Any fields to exclude.
    :returns dict:
    """
    data = view(**doc.to_mongo()).dict(exclude=exclude)

    # # cant handle nested objects
    # for key, value in data.items():
    #     if isinstance(value, ObjectId):
    #         print('found object id')
    #         data[key] = str(value)

    return data
Example #27
0
 def to_dict(self):
     d = j.data.serializer.json.loads(Document.to_json(self))
     d.pop("_cls")
     if "_id" in d:
         d.pop("_id")
     return d
Example #28
0
 def __init__(self, *args, **kwargs):
     Document.__init__(self, *args, **kwargs)
     self._gateway = None
Example #29
0
 def __init__(self, facebook_id, nome,  *args, **kwargs):
     Document.__init__(self, *args, **kwargs)
     self.nome = nome
     self.facebook_id = facebook_id
Example #30
0
 def validate(self, clean):
     return Document.validate(self, clean)
Example #31
0
 def __init__(self, *args, **kwargs):
     Document.__init__(self, *args, **kwargs)
     if self.classifier:
         self.clf = pickle.loads(str(self.classifier))
     else:
         self.clf = None
Example #32
0
 def __init__(self, *args, **kwargs):
     Document.__init__(self, *args, **kwargs)
     self.data_criacao = datetime.now()
Example #33
0
 def save(self, *args, **kwargs):
     Document.save(self, *args, **kwargs)
     searchengine.index(self)
Example #34
0
def reset_connection_cache():
    connection._connections = {}
    connection._connection_settings = {}
    connection._dbs = {}
    for document_class in Document.__subclasses__():
        document_class._collection = None
 def get(self, entity: Document):
     entity.objects(id=entity.id)
Example #36
0
 def validate(self, clean=True):
     try:
         Document.validate(self, clean)
     except ValidationError as e:
         error = e.errors.get('__all__')
         raise ValidationError(errors={error.field_name: error.message}) if error is not None else e
    def __init__(self, *args, **kwargs):
        Document.__init__(self, *args, **kwargs)

        if 'password' in kwargs:
            self.password = kwargs['password']
 def remove(self, entity: Document):
     if entity:
         entity.save()
Example #39
0
def update_by_fields_dict(doc: mongoengine.Document, dic: dict):
    doc.update(**dic)
 def add(self, entity: Document):
     if entity:
         entity.save()
Example #41
0
def update_by_document(done_trade: mongoengine.Document):
    dic = filter_dict_by_id(done_trade.to_mongo().to_dict())
    done_trade.update(**dic)
Example #42
0
def check_account_exist(model: mongoengine.Document, chat_id) -> bool:
    try:
        return bool(model.objects(chat_id=str(chat_id)))
    except Exception as e:
        print(e)
        return False
Example #43
0
 def _clear_db(self, config):
     for model in Document.__subclasses__():
         if model == DynamicDocument:
             continue
         model.drop_collection()