Serializers

The data is sent and received from the frontend as an object (json format), so schemas are required to convert the queryset data from the database queries to objects and send them to the frontend, as well as data validation. and fields that come from it. Below are the account schemas.

LoginSchema

Schema to validate the fields of the login that comes from the frontend.

  class LoginSchema(Schema):
    email = fields.Email(required=True)
    password = fields.Str(required=True)
    captcha = fields.Str()

The email and password fields are required. The captcha field can be omitted, this because the Captcha module does not necessarily have to be active.

RegisterSchema

Schema to validate the fields of the user record that comes from the frontend.

  class RegisterSchema(Schema):
    firstName = fields.Str(required=True)
    lastName = fields.Str(required=True)
    email = fields.Email(required=True)
    password = fields.Str(required=True, validate=password_validation)
    captcha = fields.Str()

The firstName, lastName, email and password fields are required. Additionally, the password field contains a validation to verify that it meets the requirements of the settings variable AUTH_PASSWORD_VALIDATORS.

EmailVerificationSchema

Schema to validate the fields in the request to send the confirmation email.

  class EmailVerificationSchema(Schema):
    email = fields.Email(required=True)

The email field is required.

UserTokenSchema

Validates the data received in the password_reset_from_key view (see the accounts view section).

  class UserTokenSchema(Schema):
    uidb36 = fields.Str(validate=uidb36_validator)
    key = fields.Str(
        required=True,
        error_messages={"required": "The password reset token was invalid."},
    )

The key field is required and the uidb36 field is checked by a validation function that checks if it is correct.

PasswordSchema

Validate the fields of the password change form.

  class PasswordSchema(Schema):
    password = fields.Str(required=True, validate=password_validation)

The password field is required; additionally it contains a validation to verify that it meets the requirements of the settings variable AUTH_PASSWORD_VALIDATORS.

SocialAccountProviderSchema

Schema that receives a queryset from the SocialApplication model and prepares it in object to the frontend.

  class SocialAccountProviderSchema(Schema):
    provider = fields.Str()
    client_id = fields.Str(data_key="clientId")
    active = fields.Boolean()
    settings = fields.Method("get_settings")
    redirect_uri = fields.Method("get_redirect_uri", data_key="redirectUri")

    def get_settings(self, obj):
        try:
            settings = app_settings.SOCIAL_ACCOUNT_PROVIDERS[obj.provider]
        except KeyError:
            return {}

        return settings

    def get_redirect_uri(self, obj):
        endpoint = f"/{obj.provider}/login/callback"
        return utils.build_absolute_uri(None, endpoint)

The provider, client_id and active fields are values ​​coming from the SocialApplication model. The settings and redirect_uri fields are generated by methods.

CaptchaSchema

Schema that receives a queryset from the CaptchaSettings model and prepares it in object to the frontend.

  class CaptchaSchema(Schema):
    provider = fields.Str()
    configuration = fields.Function(
        lambda o: get_params(o),
    )
    active = fields.Method("get_active")

    def get_active(self, obj):
        return obj.is_active()

def get_params(obj):
    params = {}
    if obj.configuration:
        dict_params = json.loads(obj.configuration)
        for key, value in dict_params.items():
            params[key] = value

    return params

The provider field is a value that comes from the CaptchaSettings model. The configuration field uses an external function to get the information from the configuration field of the model and convert it to json. The active field uses an internal method, to access a method of the model.