-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathserializers.py
115 lines (107 loc) · 3.77 KB
/
serializers.py
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from typing import Any, Dict, cast
from django.core.exceptions import ValidationError
from processes.models import Process
from processes.serializers import SimpleProcessSerializer
from rest_framework.serializers import ModelSerializer, PrimaryKeyRelatedField
from targets.models import Target
from targets.serializers import SimpleTargetSerializer
from tasks.models import Task
from tasks.queues import TasksQueue
from tools.enums import Intensity as IntensityEnum
from tools.fields import IntegerChoicesField
from tools.models import Configuration, Intensity
from tools.serializers import ConfigurationSerializer
from users.serializers import SimpleUserSerializer
class TaskSerializer(ModelSerializer):
target_id = PrimaryKeyRelatedField(
many=False,
write_only=True,
required=True,
source="target",
queryset=Target.objects.all(),
)
target = SimpleTargetSerializer(many=False, read_only=True)
process_id = PrimaryKeyRelatedField(
many=False,
write_only=True,
required=False,
source="process",
queryset=Process.objects.all(),
)
process = SimpleProcessSerializer(many=False, read_only=True)
configuration_id = PrimaryKeyRelatedField(
many=False,
write_only=True,
required=False,
source="configuration",
queryset=Configuration.objects.all(),
)
configuration = ConfigurationSerializer(many=False, read_only=True)
intensity = IntegerChoicesField(model=IntensityEnum, required=False)
executor = SimpleUserSerializer(many=False, read_only=True)
class Meta:
model = Task
fields = (
"id",
"target_id",
"target",
"process_id",
"process",
"configuration_id",
"configuration",
"intensity",
"executor",
"scheduled_at",
"repeat_in",
"repeat_time_unit",
"creation",
"enqueued_at",
"start",
"end",
"wordlists",
"executions",
)
read_only_fields = (
"executor",
"creation",
"enqueued_at",
"start",
"end",
"executions",
)
def validate(self, attrs: Dict[str, Any]) -> Dict[str, Any]:
if not attrs.get("intensity"):
attrs["intensity"] = IntensityEnum.NORMAL
if attrs.get("configuration"):
attrs["process"] = None
if not Intensity.objects.filter(
tool=cast(Configuration, attrs.get("configuration")).tool,
value=attrs.get("intensity"),
).exists():
raise ValidationError(
f'Invalid intensity {attrs["intensity"]} for tool {cast(Configuration, attrs.get("configuration")).tool.name}',
code="intensity",
)
elif attrs.get("process"):
attrs["configuration"] = None
else:
raise ValidationError(
{
"configuration": "Invalid task. Process or configuration is required",
"process": "Invalid task. Process or configuration is required",
}
)
if not attrs.get("repeat_in") or not attrs.get("repeat_time_unit"):
attrs["repeat_in"] = None
attrs["repeat_time_unit"] = None
return super().validate(attrs)
def create(self, validated_data: Dict[str, Any]) -> Task:
"""Create instance from validated data.
Args:
validated_data (Dict[str, Any]): Validated data
Returns:
Task: Created instance
"""
task = super().create(validated_data)
TasksQueue().enqueue(task)
return task