Пример #1
0
def test_collection_name():
    class User(Model):
        name = TextField()

        class Meta:
            collection_name = 'collection_name_test'

    u = User.collection.create(name="Arfan")

    doc = db.conn.collection('collection_name_test').document(
        utils.get_id(u.key)).get()

    assert doc.id == utils.get_id(u.key)
Пример #2
0
 def __init__(self, model_cls, key=None, query=None):
     super().__init__(model_cls)
     self.transaction_or_batch = None
     self.query = query
     self.id = utils.get_id(key)
     if key:
         super().set_collection_path(key=key)
Пример #3
0
    def _update_doc_key(self, model):
        """Attach key to model for later updating the model

        Attach key to this model for updating this model
        Purpose of attaching this key is user can update
        this model after getting it

        For example:
          u = User.collection.get(user_key)
          u.name = "Updated Name"
          u.update()

        Parameters
        ----------
        model:
            Current model where update key need to attach

        Returns
        -------
        update_doc_key:
            Doc key for updating document
        """
        if self.parent:
            update_doc_key = self.parent + '/' + model.collection_name + '/' + utils.get_id(
                model.key)
        else:
            update_doc_key = model.key
        return update_doc_key
Пример #4
0
 def to_dict(self):
     """Convert model into dict"""
     model_dict = self._get_fields()
     id = 'id'
     if self._meta.id is not None:
         id, _ = self._meta.id
     model_dict[id] = utils.get_id(self.key)
     model_dict['key'] = self.key
     return model_dict
Пример #5
0
def test_column_name():
    u = User3()
    u.name = 'db_column_name_test'
    u.save()

    doc = db.conn.collection('user3').document(utils.get_id(u.key)).get()
    doc_dict = doc.to_dict()

    assert doc_dict['fireo_column'] == 'db_column_name_test'
Пример #6
0
 def __init__(self, model_cls, key):
     super().__init__(model_cls)
     super().set_collection_path(key=key)
     self.model = model_cls()
     # set parent to this model if any
     self.model.parent = utils.get_parent_doc(key)
     # Attach key to this model for updating this model
     # Purpose of attaching this key is user can update
     # this model after getting it
     #
     # For example:
     #   u = User.collection.get(user_key)
     #   u.name = "Updated Name"
     #   u.update()
     self.model.update_doc = key
     self.id = utils.get_id(key)
Пример #7
0
    def update(self, key=None, transaction=None, batch=None):
        """Update the existing document

        Update document without overriding it. You can update selected fields.

        Examples
        --------
        .. code-block:: python
            class User(Model):
                name = TextField()
                age = NumberField()

            u = User.collection.create(name="Azeem", age=25)
            id = u.id

            # update this
            user = User.collection.get(id)
            user.name = "Arfan"
            user.update()

            print(user.name)  # Arfan
            print(user.age)  # 25

        Parameters
        ----------
        key: str
            Key of document which is going to update this is optional you can also set
            the update_doc explicitly

        transaction:
            Firestore transaction

        batch:
            Firestore batch writes
        """

        # Check doc key is given or not
        if key:
            self._update_doc = key

        # make sure update doc in not None
        if self._update_doc is not None and '@temp_doc_id' not in self._update_doc:
            # set parent doc from this updated document key
            self.parent = utils.get_parent_doc(self._update_doc)
            # Get id from key and set it for model
            setattr(self, '_id', utils.get_id(self._update_doc))
            # Add the temp id field if user is not specified any
            if self._id is None and self.id:
                setattr(self._meta, 'id', ('id', fields.IDField()))
        elif self._update_doc is None and '@temp_doc_id' in self.key:
            raise InvalidKey(
                f'Invalid key to update model "{self.__class__.__name__}" ')

        # Get the updated fields
        updated_fields = {}
        for k, v in self._get_fields().items():
            if k in self._field_changed:
                updated_fields[k] = v
            # Get nested fields if any
            # Nested model store as dict in firestore so check values type is dict
            if type(v) is dict:
                # nested field name and value
                for name, value in v.items():
                    if name in self._field_changed:
                        # create the name with parent field name and child name
                        # For example:
                        #   class User(Model):
                        #       address = TextField()
                        #   class Student(Model):
                        #       age = NumberField()
                        #       user = NestedModel(User)
                        #
                        # Then the field name for nested model will be "user.address"
                        updated_fields[k + "." + name] = value
        # pass the model instance if want change in it after save, fetch etc operations
        # otherwise it will return new model instance
        return self.__class__.collection._update(self,
                                                 transaction=transaction,
                                                 batch=batch,
                                                 **updated_fields)
Пример #8
0
 def _doc_ref(self):
     """create document ref from firestore"""
     return self.get_ref().document(utils.get_id(self.model.key))
Пример #9
0
 def _firestore_doc(self, key):
     """Get document from firestore based on key"""
     return db.conn.collection(utils.get_parent(key)).document(
         utils.get_id(key)).get()