Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Parameters
----------
name : str
column name (field name) coming form firestore
Raises
-------
FieldNotFound:
if field not found in model class and model config for `missing_field` is **raise_error**
"""
for field in self.field_list.values():
if name in [field.name, field.db_column_name]:
return field
if self.missing_field == 'merge':
f = fields.Field()
f.name = name
return f
if self.missing_field == 'ignore':
return None
if self.missing_field == 'raise_error':
raise FieldNotFound(f'Field "{name}" not found in model "{cls.__name__}"')
from google.cloud.firestore_v1 import ArrayUnion, ArrayRemove
from fireo.fields import Field, errors
class ListField(Field):
"""Array field for firestore
Example
-------
class User(Model):
subjects = ListField()
u = User()
u.subjects = ['English', 'Math']
"""
# Override method
def db_value(self, val):
if type(val) in [list, ArrayUnion, ArrayRemove] or val is None:
# check if user defined to set the value as lower case
if self.model_cls._meta.to_lowercase:
from fireo.fields import Field, errors
class MapField(Field):
"""Map field for firestore
Examples
--------
.. code-block:: python
class User(Model):
marks = MapField()
u = User()
u.marks = {'Math': 70, 'English': 80}
"""
# Override method
def db_value(self, val):
if type(val) is dict or val is None:
# check if user defined to set the value as lower case
from datetime import datetime
from fireo.fields import Field, errors
from google.cloud import firestore
from google.cloud.firestore_v1.transforms import Sentinel
from google.api_core.datetime_helpers import DatetimeWithNanoseconds
class DateTime(Field):
"""Date Time field for firestore
allowed_attributes = [auto]
Examples
--------
class User(Model):
created = DateTime()
u = User()
u.created = datetime.datetime.now()
"""
allowed_attributes = ['auto']
def attr_auto(self, attr_val, field_val):
from datetime import datetime
from fireo.fields import Field, errors
from google.cloud import firestore
from google.cloud.firestore_v1.transforms import Sentinel
from google.api_core.datetime_helpers import DatetimeWithNanoseconds
class DateTime(Field):
"""Date Time field for firestore
allowed_attributes = [auto]
Examples
--------
class User(Model):
created = DateTime()
u = User()
u.created = datetime.datetime.now()
"""
allowed_attributes = ['auto']
empty_value_attributes = allowed_attributes
from fireo.fields import Field, errors
class BooleanField(Field):
"""Boolean field for firestore
Examples
-------
class User(Model):
is_student = BooleanField()
u = User()
u.is_student = True
"""
# Override method
def db_value(self, val):
if type(val) is bool or val is None:
return val
raise errors.InvalidFieldType(f'Invalid field type. Field "{self.name}" expected {bool}, '
from fireo.fields import Field, errors
from google.cloud import firestore
class GeoPoint(Field):
"""GeoPoint field for firestore
Examples
-------
.. code-block:: python
class User(Model):
location = GeoPoint()
u = User()
u.location = fireo.GeoPoint(latitude=123.23, longitude=421.12)
"""
# Override method
def db_value(self, val):
if type(val) is firestore.GeoPoint or val is None:
return val