Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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)
def _doc_ref(self):
"""create document ref from firestore"""
return self.get_ref().document(utils.get_id(self.model.key))
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
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