File size: 2,915 Bytes
2f3c093
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
        }