Serializers

LogsSchema

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

  class LogsSchema(Schema):
    action = fields.Function(
        lambda o: o.get_action_display(),
    )
    changes = fields.Function(
        lambda o: get_change(o),
    )
    actor = fields.Method("get_actor")
    avatar_actor = fields.Method("get_avatar_actor")
    description = fields.Function(
        lambda o: str(o),
    )
    timestamp = fields.Method("get_created")

    def get_actor(self, obj):
        try:
            return obj.actor.email
        except Exception:
            return "system"

    def get_avatar_actor(self, obj):
        try:
            return models.UserProfile.objects.get(user=obj.actor).get_photo()
        except Exception:
            return "/static/img/photo_default.png"

    def get_created(self, obj):
        date = change_utc_date(obj.timestamp)
        return date.strftime("%d-%m-%Y @ %H:%M:%S")

def get_change(obj):
    changes = {}
    if obj.changes:
        dict_changes = json.loads(obj.changes)
        for key, value in dict_changes.items():
            changes[key] = value

    return changes

All the fields are modified from methods and functions, whose data source is the information coming from the model.

  • The action field indicates what type of action was performed on the database. -changes is an object resulting from the get_change function which obtains the string of the field changes of the model (It is in test) and formats it in json, returning an object.

  • The actor field to get the user who generated the change. If there is no user (because it was an anonymous user, that is, not authenticated), “system” is returned.

  • avatar_actor is a method field to get the avatar of the user (actor) who made the change; if one is not registered, a default avatar is returned.

  • The description field to display a descriptive text of the action.

  • timestamp is a method type field to obtain the date of creation under a predetermined format.

ModelsLogsSchema

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

  class ModelsLogsSchema(Schema):
    id = fields.Integer()
    model = fields.Str()
    model_name = fields.Str(data_key="modelName")
    is_active = fields.Boolean(data_key="isActive")

The id, model, model_name and is_active fields are values ​​coming from the ModelListLog model.

UrlsLogsSchema

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

 class UrlsLogsSchema(Schema):
    id = fields.Integer()
    path = fields.Str()
    is_active = fields.Boolean(data_key="isActive")

The id, path, and is_active fields are values ​​from the UrlListLog model.

AuditStrategySchema

Validates the fields for the strategy change in the change_strategy view.

  class AuditStrategySchema(Schema):
    strategy = fields.Str(required=True)

The strategy field is required.

LogsUrlsSchema

Schema que recibe un queryset del modelo AuditUrlLog y lo prepara en objecto al frontend.

  class LogsUrlsSchema(Schema):
    actor = fields.Method("get_actor")
    avatar_actor = fields.Method("get_avatar_actor")
    url = fields.Str()
    method = fields.Str()
    params = fields.Str()
    ip = fields.Str()
    user_agent = fields.Str(data_key="userAgent")
    created = fields.Method("get_created")

    def get_actor(self, obj):
        try:
            return obj.actor.email
        except Exception:
            return "system"

    def get_avatar_actor(self, obj):
        try:
            return models.UserProfile.objects.get(user=obj.actor).get_photo()
        except Exception:
            return "/static/img/photo_default.png"

    def get_created(self, obj):
        date = change_utc_date(obj.created)
        return date.strftime("%d-%m-%Y @ %H:%M:%S")

The url, method, params, ip, user_agent fields come from the AuditUrlLog model. The rest of the fields are methods.

  • The actor field to get the user who will finish the change. If there is no user (because it was an anonymous user, that is, not authenticated), “system” is returned.

  • avatar_actor is a method field to obtain the avatar of the user (actor) who made the change; if one is not registered, a default avatar is returned.

  • created is a method type field to obtain the date of creation under a predetermined format.

EventsSchema

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

  class EventsSchema(Schema):
    actor = fields.Method("get_actor")
    avatar_actor = fields.Method("get_avatar_actor")
    activity_text = fields.Str()
    params = fields.Str()
    created = fields.Method("get_created")

    def get_actor(self, obj):
        try:
            return obj.actor.email
        except Exception:
            return "system"

    def get_avatar_actor(self, obj):
        try:
            return models.UserProfile.objects.get(user=obj.actor).get_photo()
        except Exception:
            return "/static/img/photo_default.png"

    def get_created(self, obj):
        date = change_utc_date(obj.created)
        return date.strftime("%d-%m-%Y @ %H:%M:%S")

The activity_text, params fields come from the AuditEventLog model. The rest of the fields are methods.

  • The actor field to get the user who generated the change. If there is no user (because it was an anonymous user, that is, not authenticated), “system” is returned.

  • avatar_actor is a method field to obtain the avatar of the user (actor) who made the change; if one is not registered, a default avatar is returned.

  • created is a method type field to obtain the date of creation under a default format.