-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathenums.py
80 lines (71 loc) · 2.46 KB
/
enums.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
import graphene
from user.enums import enum_map as user_enum_map
from user_group.enums import enum_map as user_group_enum_map
from project.enums import enum_map as project_enum_map
from analysis_framework.enums import enum_map as analysis_framework_enum_map
from lead.enums import enum_map as lead_enum_map
from entry.enums import enum_map as entry_enum_map
from export.enums import enum_map as export_enum_map
from quality_assurance.enums import enum_map as quality_assurance_enum_map
from analysis.enums import enum_map as analysis_enum_map
from notification.enums import enum_map as notification_enum_map
from unified_connector.enums import enum_map as unified_connector_enum_map
from assisted_tagging.enums import enum_map as assisted_tagging_enum_map
from ary.enums import enum_map as ary_enum_map
from assessment_registry.enums import enum_map as assessment_reg_enum_map
ENUM_TO_GRAPHENE_ENUM_MAP = {
**user_enum_map,
**user_group_enum_map,
**project_enum_map,
**analysis_framework_enum_map,
**lead_enum_map,
**entry_enum_map,
**export_enum_map,
**quality_assurance_enum_map,
**analysis_enum_map,
**notification_enum_map,
**unified_connector_enum_map,
**assisted_tagging_enum_map,
**ary_enum_map,
**assessment_reg_enum_map,
}
ENUM_TO_GRAPHENE_ENUM_DESCRIPTION_MAP = {
enum: getattr(enum._meta.enum, '__description__', {})
for enum in ENUM_TO_GRAPHENE_ENUM_MAP.values()
}
def generate_type_for_enum(name, Enum):
EnumMetaType = type(
f'AppEnumCollection{name}',
(graphene.ObjectType,),
{
'enum': graphene.NonNull(Enum),
'label': graphene.NonNull(graphene.String),
'description': graphene.String(),
}
)
return graphene.Field(
graphene.List(
graphene.NonNull(EnumMetaType),
),
resolver=lambda *_: [
EnumMetaType(
enum=e,
label=e.label,
description=ENUM_TO_GRAPHENE_ENUM_DESCRIPTION_MAP[Enum].get((e.value, e.label)),
)
for e in Enum._meta.enum
]
)
def generate_type_for_enums(name):
return type(
name,
(graphene.ObjectType,),
{
enum_field_name: generate_type_for_enum(
enum_field_name,
enum_,
)
for enum_field_name, enum_ in ENUM_TO_GRAPHENE_ENUM_MAP.items()
},
)
AppEnumCollection = generate_type_for_enums('AppEnumCollection')