-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
fields.py
77 lines (67 loc) · 2.86 KB
/
fields.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
from marshmallow import ValidationError, fields, validate
from common.enum import (
Offliner,
Platform,
ScheduleCategory,
SchedulePeriodicity,
TaskStatus,
WarehousePath,
)
from common.roles import ROLES
from common.schemas import String
# validators
validate_priority = validate.Range(min=0, max=10)
def validate_schedule_name(value) -> bool:
if value == "none":
raise ValidationError("`none` is a restricted keyword")
if not value.strip() or value != value.strip():
raise ValidationError(
"Recipe name cannot contain leading and/or trailing space(s)"
)
return True
validate_not_empty = validate.Length(min=1)
validate_role = validate.OneOf(ROLES.keys())
validate_cpu = validate.Range(min=0)
validate_memory = validate.Range(min=0)
validate_disk = validate.Range(min=0)
validate_lang_code = validate.Length(min=2, max=3)
validate_output = validate.Equal("/output")
validate_category = validate.OneOf(ScheduleCategory.all())
validate_warehouse_path = validate.OneOf(WarehousePath.all())
validate_offliner = validate.OneOf(Offliner.all())
validate_status = validate.OneOf(TaskStatus.all())
validate_event = validate.OneOf(TaskStatus.all_events())
validate_worker_name = validate.Length(min=3)
validate_periodicity = validate.OneOf(SchedulePeriodicity.all())
validate_platform = validate.OneOf(Platform.all())
validate_platform_value = validate.Range(min=0)
# slack target must start with # for channels or @ for usernames
validate_slack_target = validate.Regexp(regex=r"^[#|@].+$")
validate_zim_filename = validate.Regexp(
regex=r"^(.+?_)([a-z\-]{2,3}?_)(.+_|)([\d]{4}-[\d]{2}|{period}).zim$",
error="ZIM filename format is incorrect",
)
validate_zim_title = validate.Length(max=30)
validate_zim_description = validate.Length(max=80)
validate_zim_longdescription = validate.Length(max=4000)
def validate_multiple_of_100(value):
return value % 100 == 0
# reusable fields
skip_field = fields.Integer(
required=False, load_default=0, validate=validate.Range(min=0)
)
limit_field_20_500 = fields.Integer(
required=False, load_default=20, validate=validate.Range(min=0, max=500)
)
limit_field_20_200 = fields.Integer(
required=False, load_default=20, validate=validate.Range(min=0, max=200)
)
priority_field = fields.Integer(required=False, validate=validate_priority)
worker_field = String(required=False, validate=validate_worker_name)
schedule_name_field = String(validate=validate_schedule_name)
category_field = String(required=False, validate=validate_category)
periodicity_field = String(required=False, validate=validate_periodicity)
tag_field = fields.List(String(validate=validate_not_empty), required=False)
offliner_field = String(required=False, validate=validate_offliner)
email_field = fields.Email(required=False, validate=validate_not_empty)
username_field = String(required=True, validate=validate_not_empty)