# Serializers ## **GroupsSchema** Schema that receives a queryset from the Groups model and returns an object with the name of the group ```shell class GroupsSchema(Schema): name = fields.Str() ``` ## **UserSchema** Schema that receives a queryset from the CustomUser model and returns an object. ```shell def get_user_profile(user): try: user_profile = models.UserProfile.objects.get(user=user) except models.UserProfile.DoesNotExist: user_profile = models.UserProfile(user=user) user_profile.save() else: return serialiazers.ProfileSchema().dump(user_profile) class UserSchema(Schema): email = fields.Email() first_name = fields.Str(data_key="firstName") last_name = fields.Str(data_key="lastName") is_active = fields.Boolean(data_key="isActive") user_id = fields.Function(lambda o: o.id) groups = fields.Function(lambda o: GroupsSchema(many=True).dump(o.groups.all())) profile = fields.Function(lambda o: get_user_profile(o)) ``` The main fields are: - **path:** Path of the url. - **is_active:** Boolean field that tells the system if the path should be recorded in the log when it is visited by the user. The methods of this model are: - **create_register:** Method to register a new Url. It does not require to instantiate the model. ## **ModelListLog** Model to store the list of System Models. To do this, in each model.py file there must be an object called **apps_audit_models** whose key is the name of the model and the value is the model (an example can be seen at the end of the models section of accounts). ```shell class ModelListLog(TimeStampedModel): model = models.CharField(max_length=100, blank=True, null=True) model_name = models.CharField(max_length=100, blank=True, null=True) is_active = models.BooleanField(default=False) @classmethod def create_register(cls, model: str, model_name: str): if bool(ModelListLog.objects.filter(model_name=model_name)): return None obj = cls() obj.model = model obj.model_name = model_name obj.save() return obj ``` The **email**, **first_name**, **last_name** and **is_active** fields are rendered as they come from the CustomUser model. The rest of the fields are modified and obtained by functions. - The **user_id** field represents the id of the user. - **groups** is a schema relationship function GroupSchema. - **profile** is a relationship function with the ProfileSchema schema of the core app. ## **CreateUserSchema** Schema that validates for the creation of a new userto have a personalized log of a specific point in the flow of the application. ```shell def user_group_validation(value): groups = utils.get_groups() try: groups[value] except KeyError: raise ValidationError("Invalid user group.") class CreateUserSchema(Schema): firstName = fields.Str(required=True) lastName = fields.Str(required=True) email = fields.Email(required=True) group = fields.Str(required=True, validate=user_group_validation) ``` All fields are required. Additionally, the group field contains a validation that verifies that the data entered belongs to the groups enabled in the system. ## **SystemAppNameSchema** Schema that validates the fields to change the name of the application. ```shell class SystemAppNameSchema(Schema): appName = fields.Str(required=True, validate=validate.Length(min=1)) ``` The appName field is required and also cannot be left blank. ## **SystemExpireTimeSchema** Schema that validates the fields to change the session expiration time due to inactivity. ```shell class SystemExpireTimeSchema(Schema): sessionExpireTime = fields.Integer(required=True) ``` The sessionExpireTime field is required. ## **GlobalSettingsScripts** Schema that validates the fields to register custom scripts in the system. ```shell class GlobalSettingsScripts(Schema): header = fields.Str(required=True) body = fields.Str(required=True) footer = fields.Str(required=True) ``` All fields are required. ## **GlobalSettingsSocialConnections** Schema that receives a queryset from the GlobalSettings model and returns an object. ```shell class GlobalSettingsSocialConnections(Schema): id = fields.Str() provider = fields.Method("get_provider_name") active = fields.Boolean() def get_provider_name(self, obj): return obj.get_provider_display() ``` The **id** and **active** fields are presented as they come from the GlobalSettings model. The **provider** field is of type method that returns the name of the provider. ## **GlobalSettingsCreateSocialConnections** Schema that validates the fields to register custom scripts in the system. ```shell lass GlobalSettingsCreateSocialConnections(Schema): id = fields.Str() provider = fields.Str(required=True) name = fields.Str(required=True) client_id = fields.Str(required=True, data_key="clientId") secret_key = fields.Str(required=True, data_key="secretKey") active = fields.Boolean(required=True) ``` All fields are required except for the **id**. ## **EmailProvidersSchema** Schema that receives a queryset from the EmailProvider model and returns an object. ```shell class EmailProvidersSchema(Schema): id = fields.Str() provider = fields.Method("get_provider_name") name = fields.Str() from_email = fields.Email(data_key="email") active = fields.Boolean() def get_provider_name(self, obj): return obj.get_provider_display() ``` The **id, name, from_email** and **active** fields are presented as they come from the EmailProvider model. The **provider** field is of type method that returns the name of the provider. ## **EmailProviderTemplateSchema** Schema that receives a queryset from the EmailProviderTemplate model and returns an object. Additionally, it is also used to register a new Template for a specific provider. ```shell class EmailProviderTemplateSchema(Schema): id = fields.Str() provider = fields.Str() providerId = fields.Method("get_provider_id") name = fields.Str(required=True) template_id = fields.Str(required=True, data_key="templateId") language = fields.Str(required=True) can_delete = fields.Boolean(data_key="canDelete") def get_provider_id(self, obj): return obj.provider.id ``` The **id, provider, name, template_id, language** and **can_delete** fields are rendered as they come from the EmailProviderTemplate model. The **provider_id** field is of type method that returns the id of the provider. ## **EmailProvidersCreateSchema** Schema that validates the fields to register an email provider. ```shell class EmailProvidersCreateSchema(Schema): id = fields.Str() provider = fields.Str(required=True) name = fields.Str(required=True) from_email = fields.Email(required=True, data_key="email") secret_key = fields.Str(required=True, data_key="secretKey") active = fields.Boolean(required=True) ``` All fields are required except **id**. ## **CaptchaProviderSchema** Schema that receives a queryset from the EmailProviderTemplate model and returns an object. ```python class CaptchaProviderSchema(Schema): provider = fields.Str(required=True) site_key = fields.Str(required=True, data_key="siteKey") secret_key = fields.Str(required=True, data_key="secretKey") url_verify = fields.Str(required=True, data_key="urlVerify") configuration = fields.Str(required=True) active = fields.Boolean(required=True) ``` All fields are required.