class UserModel(Model, DateTimeStampMixin): user_id = IntField(pk=True) credits = IntField(default=0) level = IntField(default=0) xp = FloatField(default=0) @property def xp_required(self) -> float: """ total xp required for the next level """ return self.level * get_settings().XP_SCALE @property def levelup_due(self) -> bool: """ check whether a levelup is due (user has enough xp) """ if self.xp >= self.xp_required: return True return False class Meta: table = "users"
class Sub(Model): type = CharField(max_length=10) type_id = IntField() uid = IntField() live = BooleanField() dynamic = BooleanField() at = BooleanField() bot_id = IntField()
class Tag(Model): id = IntField(pk=True) index_id = CharField(255) male = CharField(1, null=True) female = CharField(1, null=True) tag = CharField(255, null=True) url = CharField(255, null=True)
class IndicateurReferentielCommentaire(models.Model): id = IntField(pk=True) epci_id = CharField(max_length=36) indicateur_id = CharField(max_length=136) value = TextField() created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) latest = BooleanField()
class ActionStatus(models.Model): id = IntField(pk=True) action_id = CharField(max_length=36) epci_id = CharField(max_length=36) avancement = CharField(max_length=36) created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) latest = BooleanField()
class MesureCustom(models.Model): id = IntField(pk=True) uid = CharField(max_length=36) epci_id = CharField(max_length=36) climat_pratic_thematic_id = CharField(max_length=100) name = CharField(max_length=100) created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True)
class UtilisateurDroits(models.Model): id = IntField(pk=True) ademe_user_id = CharField(max_length=300) epci_id = CharField(max_length=36) ecriture = BooleanField() created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) latest = BooleanField()
class AdemeUtilisateur(models.Model): id = IntField(pk=True) ademe_user_id = CharField(max_length=300) email = CharField(max_length=300) nom = CharField(max_length=300) prenom = CharField(max_length=300) created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True)
class ActionMeta(models.Model): id = IntField(pk=True) action_id = CharField(max_length=36) epci_id = CharField(max_length=36) meta = JSONField() created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) latest = BooleanField()
class ActionCustom(models.Model): id = IntField(pk=True) uid = CharField(max_length=36) epci_id = CharField(max_length=36) mesure_id = CharField(max_length=36) name = CharField(max_length=100) description = TextField() created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True)
class Epci(models.Model): id = IntField(pk=True) uid = CharField(max_length=36) insee = CharField(max_length=5) siren = CharField(max_length=9) nom = CharField(max_length=300) created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) latest = BooleanField()
class FicheActionCategorie(models.Model): id = IntField(pk=True) epci_id = CharField(max_length=36) uid = CharField(max_length=36) parent_uid = CharField(max_length=36) nom = CharField(max_length=300) fiche_actions_uids = JSONField() created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) latest = BooleanField() deleted = BooleanField()
class IndicateurPersonnalise(models.Model): id = IntField(pk=True) epci_id = CharField(max_length=36) uid = CharField(max_length=36) custom_id = CharField(max_length=36) nom = CharField(max_length=300) description = TextField() unite = CharField(max_length=36) meta = JSONField() created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True) latest = BooleanField() deleted = BooleanField()
class File(Model): id = IntField(pk=True) index_id = CharField(255) width = IntField(null=True) hash = CharField(64, null=True) haswebp = IntField(null=True) hasavifsmalltn = IntField(null=True) name = TextField(null=True) height = IntField(null=True) hasavif = IntField(null=True)
class FicheAction(models.Model): id = IntField(pk=True) epci_id = CharField(max_length=36) uid = CharField(max_length=36) custom_id = CharField(max_length=36) avancement = CharField(max_length=36) en_retard = BooleanField() referentiel_action_ids = JSONField() referentiel_indicateur_ids = JSONField() titre = CharField(max_length=300) description = TextField() budget = FloatField() personne_referente = CharField(max_length=100) structure_pilote = CharField(max_length=300) elu_referent = CharField(max_length=300) partenaires = CharField(max_length=300) commentaire = TextField() date_debut = CharField(max_length=36) date_fin = CharField(max_length=36) indicateur_personnalise_ids = JSONField() latest = BooleanField() deleted = BooleanField() created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True)
def __new__(mcs, name: str, bases: Tuple[Type, ...], attrs: dict): fields_db_projection: Dict[str, str] = {} fields_map: Dict[str, Field] = {} filters: Dict[str, Dict[str, dict]] = {} fk_fields: Set[str] = set() m2m_fields: Set[str] = set() o2o_fields: Set[str] = set() meta_class: "Model.Meta" = attrs.get("Meta", type("Meta", (), {})) pk_attr: str = "id" # Searching for Field attributes in the class hierarchy def __search_for_field_attributes(base: Type, attrs: dict) -> None: """ Searching for class attributes of type fields.Field in the given class. If an attribute of the class is an instance of fields.Field, then it will be added to the fields dict. But only, if the key is not already in the dict. So derived classes have a higher precedence. Multiple Inheritance is supported from left to right. After checking the given class, the function will look into the classes according to the MRO (method resolution order). The MRO is 'natural' order, in which python traverses methods and fields. For more information on the magic behind check out: `The Python 2.3 Method Resolution Order <https://www.python.org/download/releases/2.3/mro/>`_. """ for parent in base.__mro__[1:]: __search_for_field_attributes(parent, attrs) meta = getattr(base, "_meta", None) if meta: # For abstract classes for key, value in meta.fields_map.items(): attrs[key] = value else: # For mixin classes for key, value in base.__dict__.items(): if isinstance(value, Field) and key not in attrs: attrs[key] = value # Start searching for fields in the base classes. inherited_attrs: dict = {} for base in bases: __search_for_field_attributes(base, inherited_attrs) if inherited_attrs: # Ensure that the inherited fields are before the defined ones. attrs = {**inherited_attrs, **attrs} if name != "Model": custom_pk_present = False for key, value in attrs.items(): if isinstance(value, Field): if value.pk: if custom_pk_present: raise ConfigurationError( f"Can't create model {name} with two primary keys," " only single primary key is supported") if value.generated and not value.allows_generated: raise ConfigurationError( f"Field '{key}' ({value.__class__.__name__}) can't be DB-generated" ) custom_pk_present = True pk_attr = key if not custom_pk_present and not getattr(meta_class, "abstract", None): if "id" not in attrs: attrs = {"id": IntField(pk=True), **attrs} if not isinstance(attrs["id"], Field) or not attrs["id"].pk: raise ConfigurationError( f"Can't create model {name} without explicit primary key if field 'id'" " already present") for key, value in attrs.items(): if isinstance(value, Field): if getattr(meta_class, "abstract", None): value = deepcopy(value) fields_map[key] = value value.model_field_name = key if isinstance(value, OneToOneFieldInstance): o2o_fields.add(key) elif isinstance(value, ForeignKeyFieldInstance): fk_fields.add(key) elif isinstance(value, ManyToManyFieldInstance): m2m_fields.add(key) else: fields_db_projection[key] = value.source_field or key filters.update( get_filters_for_field( field_name=key, field=fields_map[key], source_field=fields_db_projection[key], )) if value.pk: filters.update( get_filters_for_field( field_name="pk", field=fields_map[key], source_field=fields_db_projection[key], )) # Clean the class attributes for slot in fields_map: attrs.pop(slot, None) attrs["_meta"] = meta = MetaInfo(meta_class) meta.fields_map = fields_map meta.fields_db_projection = fields_db_projection meta._filters = filters meta.fk_fields = fk_fields meta.backward_fk_fields = set() meta.o2o_fields = o2o_fields meta.backward_o2o_fields = set() meta.m2m_fields = m2m_fields meta.default_connection = None meta.pk_attr = pk_attr meta.pk = fields_map.get(pk_attr) # type: ignore if meta.pk: meta.db_pk_column = meta.pk.source_field or meta.pk_attr meta._inited = False if not fields_map: meta.abstract = True new_class: Type["Model"] = super().__new__(mcs, name, bases, attrs) for field in meta.fields_map.values(): field.model = new_class for fname, comment in _get_comments(new_class).items(): if fname in fields_map: fields_map[fname].docstring = comment if fields_map[fname].description is None: fields_map[fname].description = comment.split("\n")[0] if new_class.__doc__ and not meta.table_description: meta.table_description = inspect.cleandoc( new_class.__doc__).split("\n")[0] meta._model = new_class meta.finalise_fields() return new_class
class Utilisateur(models.Model): id = IntField(pk=True) ademe_user_id = CharField(max_length=300) vie_privee_conditions = CharField(max_length=300) created_at = DatetimeField(auto_now_add=True) modified_at = DatetimeField(auto_now=True)
class Index(Model): id = IntField(pk=True) index_id = CharField(255)
class User(Model): uid = IntField(pk=True) name = CharField(max_length=20)
class Group(Model): id = IntField(pk=True) admin = BooleanField()