Exemplo n.º 1
0
    def create(self, app_id, user_id, bucket, ip_address, document):
        """ Create operation for CRUD.
         Saves entity to db in this format:
         {
             app_id: $app_id,
             user_id: $user_id,
             bucket: $bucket,
             _id: $id
             $document
         }
         $document saves in root of document.
         Function creations new id and set it to _id field, when _id is not filled in user document.
         If user document's _id already exists, InvalidDocumentIdError will be raised.
         Parameters:
         app_id: String, application id
         user_id: String, user id
         document: Dict, user document
         bucket: String, document type (bucket)

         Returns key of inserted entity """

        # validations
        if document is None:
            raise InvalidDocumentError('Document must be not null')
        if type(document) is not DICT_TYPE:
            raise InvalidDocumentError('Document must be instance of dict type')


        # check if a key is already exists, if it isn't - generate new
        # check if a key is already exists, if it isn't - generate new
        if extf.KEY not in document:
            document[extf.KEY] = uuid4()

        document = _from_external_to_internal(app_id, user_id, bucket, document)
        # add required fields to document
        document[intf.APP_ID] = app_id
        document[intf.USER_ID] = user_id
        # adding str(False) to hashid means that document isn't deleted
        document[intf.HASHID] = sha1(app_id+user_id+bucket+str(False)).hexdigest()
        document[intf.IP_ADDRESS] = ip_address
        document[intf.DELETED] = False

        document[reservedf.BUCKET] = bucket
        document[reservedf.CREATED_AT] = datetime.utcnow()

        id = document[intf.ID]
        removed_doc_criteria = {
            intf.ID: id,
            intf.DELETED: True
        }
        if self._is_document_exists(removed_doc_criteria):
            # if removed doc with same id exists - update it
            del document[intf.ID]
            self.entities.update(removed_doc_criteria, {'$set': document}, multi=False)
        else:
            self.entities.insert(document)

        return _external_key(id)
Exemplo n.º 2
0
def _to_external(document):
    if not document:
        return None

    external = _filter_int_fields(document)
    external = _from_internal_to_external(external)
    external[extf.KEY] = _external_key(document[intf.ID])

    return external
Exemplo n.º 3
0
 def to_external(cls, key, dbref, *args, **kwargs):
     """
     :param key: document key,
     :param dbref: bson.dbref.DBRef
     :return: external view of DBRef
     :rtype: :class:`Pointer`
     """
     bucket = dbref.collection
     id = dbref.id
     document_key = _external_key(id)
     return key, Pointer(bucket, document_key)