| from typing import Dict, Any
|
| import json
|
| from adware_dashboard.models import Adware, Payload, DeploymentMethod
|
|
|
| class AdwareSerializer:
|
| @staticmethod
|
| def serialize(adware: Adware) -> Dict[str, Any]:
|
| """
|
| Serializes an Adware object to a dictionary.
|
|
|
| Args:
|
| adware (Adware): The Adware object to serialize.
|
|
|
| Returns:
|
| Dict[str, Any]: The serialized dictionary.
|
| """
|
| return {
|
| 'id': adware.id,
|
| 'name': adware.name,
|
| 'description': adware.description,
|
| 'target_os': adware.target_os,
|
| 'persistence_method': adware.persistence_method,
|
| 'payload_id': adware.payload.id,
|
| 'deployment_method_id': adware.deployment_method.id,
|
| 'config': json.loads(adware.config) if adware.config else None
|
| }
|
|
|
| @staticmethod
|
| def deserialize(data: Dict[str, Any]) -> Dict[str, Any]:
|
| """
|
| Deserializes data to a dictionary suitable for creating or updating an Adware object.
|
|
|
| Args:
|
| data (Dict[str, Any]): The data to deserialize.
|
|
|
| Returns:
|
| Dict[str, Any]: The deserialized dictionary.
|
| """
|
| return {
|
| 'name': data.get('name'),
|
| 'description': data.get('description'),
|
| 'target_os': data.get('target_os'),
|
| 'persistence_method': data.get('persistence_method'),
|
| 'payload_id': data.get('payload_id'),
|
| 'deployment_method_id': data.get('deployment_method_id'),
|
| 'config': json.dumps(data.get('config')) if data.get('config') else None
|
| }
|
|
|
| class PayloadSerializer:
|
| @staticmethod
|
| def serialize(payload: Payload) -> Dict[str, Any]:
|
| """
|
| Serializes a Payload object to a dictionary.
|
|
|
| Args:
|
| payload (Payload): The Payload object to serialize.
|
|
|
| Returns:
|
| Dict[str, Any]: The serialized dictionary.
|
| """
|
| return {
|
| 'id': payload.id,
|
| 'name': payload.name,
|
| 'description': payload.description,
|
| 'file_path': payload.file_path
|
| }
|
|
|
| class DeploymentMethodSerializer:
|
| @staticmethod
|
| def serialize(deployment_method: DeploymentMethod) -> Dict[str, Any]:
|
| """
|
| Serializes a DeploymentMethod object to a dictionary.
|
|
|
| Args:
|
| deployment_method (DeploymentMethod): The DeploymentMethod object to serialize.
|
|
|
| Returns:
|
| Dict[str, Any]: The serialized dictionary.
|
| """
|
| return {
|
| 'id': deployment_method.id,
|
| 'name': deployment_method.name,
|
| 'description': deployment_method.description,
|
| 'config_schema': json.loads(deployment_method.config_schema) if deployment_method.config_schema else None
|
| } |