Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from fireo.database import db
from fireo.fields.base_field import Field
from fireo.fields import errors
from fireo.utils import utils
from google.cloud import firestore
class NestedModel(Field):
"""Model inside another model"""
def __init__(self, model, *args, **kwargs):
super().__init__(*args, **kwargs)
# Check nested model class is subclass for Model
from fireo.models import Model
if not issubclass(model, Model):
raise errors.NestedModelTypeError(f'Nested model {model.__name__} must be inherit from Model class')
self.nested_model = model
def valid_model(self, model_instance):
"""Check nested model and passing model is same"""
if self.nested_model == model_instance.__class__:
return True
raise errors.NestedModelTypeError(f'Invalid nested model type. Field "{self.name}" required value type '
f'{self.nested_model.__name__}, but got {model_instance.__class__.__name__}')
from fireo.fields import errors
from fireo.fields.base_field import Field
import re
class TextField(Field):
"""Text field for Models
Define text for models
allowed_attributes = ['max_length', 'to_lowercase']
Examples
--------
class User(Model):
age = TextField()
"""
allowed_attributes = ['max_length', 'to_lowercase', 'format']
u = User()
u.user_id = "custom_doc_id"
u.save()
# After save id will be saved in `user_id`
print(self.user_id) # custom_doc_id
"""
def contribute_to_model(self, model_cls, name):
self.name = name
setattr(model_cls, name, None)
model_cls._meta.add_model_id(self)
class TextField(Field):
"""Text field for Models
Define text for models
Examples
--------
class User(Model):
age = TextField()
"""
pass
def __init__(self, model, *args, **kwargs):
super().__init__(*args, **kwargs)
# Check nested model class is subclass for Model
from fireo.models import Model
if not issubclass(model, Model):
raise errors.NestedModelTypeError(f'Nested model {model.__name__} must be inherit from Model class')
self.nested_model = model
def valid_model(self, model_instance):
"""Check nested model and passing model is same"""
if self.nested_model == model_instance.__class__:
return True
raise errors.NestedModelTypeError(f'Invalid nested model type. Field "{self.name}" required value type '
f'{self.nested_model.__name__}, but got {model_instance.__class__.__name__}')
class ReferenceField(Field):
"""Reference of other model
A DocumentReference refers to a document location in a Firestore database and
can be used to write, read, or listen to the location. The document at the referenced
location may or may not exist.
Attributes
----------
allowed_attribute: ['auto_load']
Allow reference field to load automatically or not
model_ref:
Reference of the model
auto_load:
Reference field load setting, load it auto or not
Call user specify method when reference document is loaded
"""
try:
m = getattr(self.model_cls, method_name)
if not callable(m):
raise errors.AttributeTypeError(f'Attribute {m} is not callable in model {self.model_cls.__name__} '
f'field {self.name}')
self.on_load = method_name
except AttributeError as e:
raise errors.AttributeMethodNotDefined(f'Method {method_name} is not defined for attribute on_load in '
f'model {self.model_cls.__name__} field {self.name}') from e
return field_val
class IDField(Field):
"""Specify custom id for models
User can specify model id and will save with the same id in firestore otherwise it will
return None and generate later from firestore and attached to model
Example
--------
.. code-block:: python
class User(Mode):
user_id = IDField()
u = User()
u.user_id = "custom_doc_id"
u.save()
# After save id will be saved in `user_id`
from fireo.database import db
from fireo.fields import errors
from fireo.fields.base_field import Field
from fireo.utils import utils
from google.cloud import firestore
class ReferenceField(Field):
"""Reference of other model
A DocumentReference refers to a document location in a Firestore database and
can be used to write, read, or listen to the location. The document at the referenced
location may or may not exist.
Attributes
----------
allowed_attribute: ['auto_load']
Allow reference field to load automatically or not
model_ref:
Reference of the model
auto_load:
Reference field load setting, load it auto or not
from google.cloud.firestore_v1 import Increment
from fireo.fields import errors
from fireo.fields.base_field import Field
class NumberField(Field):
"""Number field for Models
Define numbers for models integer, float etc
allowed_attributes = ['int_only', 'float_only', range]
Examples
--------
class User(Model):
age = NumberField()
"""
allowed_attributes = ['int_only', 'float_only', 'range']
def attr_range(self, attr_val, field_val):
"""Method for attribute range"""
from fireo.fields import errors
from fireo.fields.base_field import Field
class NestedModel(Field):
"""Model inside another model"""
def __init__(self, model, *args, **kwargs):
super().__init__(*args, **kwargs)
# Check nested model class is subclass for Model
from fireo.models import Model
if not issubclass(model, Model):
raise errors.NestedModelTypeError(f'Nested model "{model.__name__}" must be inherit from Model class')
self.nested_model = model
def valid_model(self, model_instance):
"""Check nested model and passing model is same"""
# return False if no Nested model apply
if model_instance is None:
return False