Skip to content

Commit 8fac8ae

Browse files
Merge pull request #1250 from syucream/fix/refine-attr-types
Cleanup old fashion attr types
2 parents a3ae196 + 1a8129b commit 8fac8ae

File tree

5 files changed

+18
-136
lines changed

5 files changed

+18
-136
lines changed

airone/lib/http.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from django.utils.encoding import smart_str
1111

1212
from airone.lib.acl import ACLObjType
13-
from airone.lib.types import AttrTypes, AttrTypeValue
13+
from airone.lib.types import AttrTypeValue
1414
from entity import models as entity_models
1515
from entry import models as entry_models
1616
from job.models import JobOperation, JobStatus
@@ -182,8 +182,8 @@ def render(request, template, context={}):
182182
context["config"][app] = config.TEMPLATE_CONFIG
183183

184184
context["attr_type"] = {}
185-
for attr_type in AttrTypes:
186-
context["attr_type"][attr_type.NAME] = attr_type.TYPE
185+
for name, type in AttrTypeValue.items():
186+
context["attr_type"][name] = type
187187
context["attr_type_value"] = AttrTypeValue
188188

189189
# set Construct for Entity status

airone/lib/types.py

-122
Original file line numberDiff line numberDiff line change
@@ -23,132 +23,10 @@ class AttrType(enum.IntEnum):
2323
ARRAY_ROLE = _ARRAY | ROLE
2424

2525

26-
class MetaAttrType(type):
27-
def __eq__(cls, comp):
28-
if isinstance(comp, int):
29-
return cls.TYPE == comp
30-
elif isinstance(comp, str):
31-
return cls.NAME == comp
32-
else:
33-
return cls.TYPE == comp.TYPE
34-
35-
def __ne__(cls, comp):
36-
return not cls == comp
37-
38-
def __repr__(cls):
39-
return str(cls.TYPE)
40-
41-
def __int__(cls):
42-
return cls.TYPE
43-
44-
45-
class AttrTypeObj(metaclass=MetaAttrType):
46-
NAME = "entry"
47-
TYPE = AttrType.OBJECT
48-
DEFAULT_VALUE = None
49-
50-
51-
# STRING-type restricts data size to AttributeValue.MAXIMUM_VALUE_LENGTH
52-
class AttrTypeStr(metaclass=MetaAttrType):
53-
NAME = "string"
54-
TYPE = AttrType.STRING
55-
DEFAULT_VALUE = ""
56-
57-
58-
class AttrTypeNamedObj(metaclass=MetaAttrType):
59-
NAME = "named_entry"
60-
TYPE = AttrType.NAMED_OBJECT
61-
DEFAULT_VALUE = {"name": "", "id": None}
62-
63-
64-
class AttrTypeArrObj(metaclass=MetaAttrType):
65-
NAME = "array_entry"
66-
TYPE = AttrType.ARRAY_OBJECT
67-
DEFAULT_VALUE = []
68-
69-
70-
class AttrTypeArrStr(metaclass=MetaAttrType):
71-
NAME = "array_string"
72-
TYPE = AttrType.ARRAY_STRING
73-
DEFAULT_VALUE = []
74-
75-
76-
class AttrTypeArrNamedObj(metaclass=MetaAttrType):
77-
NAME = "array_named_entry"
78-
TYPE = AttrType.ARRAY_NAMED_OBJECT
79-
DEFAULT_VALUE: Any = dict().values()
80-
81-
82-
class AttrTypeArrGroup(metaclass=MetaAttrType):
83-
NAME = "array_group"
84-
TYPE = AttrType.ARRAY_GROUP
85-
DEFAULT_VALUE = []
86-
87-
88-
class AttrTypeText(metaclass=MetaAttrType):
89-
NAME = "textarea"
90-
TYPE = AttrType.TEXT
91-
DEFAULT_VALUE = ""
92-
93-
94-
class AttrTypeBoolean(metaclass=MetaAttrType):
95-
NAME = "boolean"
96-
TYPE = AttrType.BOOLEAN
97-
DEFAULT_VALUE = False
98-
99-
100-
class AttrTypeGroup(metaclass=MetaAttrType):
101-
NAME = "group"
102-
TYPE = AttrType.GROUP
103-
DEFAULT_VALUE = None
104-
105-
106-
class AttrTypeDate(metaclass=MetaAttrType):
107-
NAME = "date"
108-
TYPE = AttrType.DATE
109-
DEFAULT_VALUE = None
110-
111-
112-
class AttrTypeRole(metaclass=MetaAttrType):
113-
NAME = "role"
114-
TYPE = AttrType.ROLE
115-
DEFAULT_VALUE = None
116-
117-
118-
class AttrTypeArrRole(metaclass=MetaAttrType):
119-
NAME = "array_role"
120-
TYPE = AttrType.ARRAY_ROLE
121-
DEFAULT_VALUE = []
122-
123-
124-
class AttrTypeDatetime(metaclass=MetaAttrType):
125-
NAME = "datetime"
126-
TYPE = AttrType.DATETIME
127-
DEFAULT_VALUE = None
128-
129-
130-
AttrTypes = [
131-
AttrTypeStr,
132-
AttrTypeObj,
133-
AttrTypeNamedObj,
134-
AttrTypeArrStr,
135-
AttrTypeArrObj,
136-
AttrTypeArrNamedObj,
137-
AttrTypeArrGroup,
138-
AttrTypeText,
139-
AttrTypeBoolean,
140-
AttrTypeGroup,
141-
AttrTypeDate,
142-
AttrTypeRole,
143-
AttrTypeArrRole,
144-
AttrTypeDatetime,
145-
]
14626
AttrTypeValue = {
14727
"object": AttrType.OBJECT,
14828
"string": AttrType.STRING,
149-
"named": AttrType._NAMED,
15029
"named_object": AttrType.NAMED_OBJECT,
151-
"array": AttrType._ARRAY,
15230
"array_object": AttrType.ARRAY_OBJECT,
15331
"array_string": AttrType.ARRAY_STRING,
15432
"array_named_object": AttrType.ARRAY_NAMED_OBJECT,

entity/views.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
http_post,
1818
render,
1919
)
20-
from airone.lib.types import AttrType, AttrTypes
20+
from airone.lib.types import AttrType, AttrTypeValue
2121
from entry.models import AttributeValue, Entry
2222
from job.models import Job
2323
from user.models import History
@@ -70,7 +70,7 @@ def create(request):
7070
for x in Entity.objects.filter(is_active=True)
7171
if request.user.has_permission(x, ACLType.Readable)
7272
],
73-
"attr_types": AttrTypes,
73+
"attr_types": AttrTypeValue,
7474
}
7575
return render(request, "create_entity.html", context)
7676

@@ -88,7 +88,7 @@ def edit(request, entity_id):
8888
# - current value of any attributes even if the entity has been deleted
8989
context = {
9090
"entity": entity,
91-
"attr_types": AttrTypes,
91+
"attr_types": AttrTypeValue,
9292
"attributes": [
9393
{
9494
"id": x.id,
@@ -132,7 +132,9 @@ def edit(request, entity_id):
132132
{
133133
"name": "type",
134134
"type": str,
135-
"checker": lambda x: (any([y == int(x["type"]) for y in AttrTypes])),
135+
"checker": lambda x: (
136+
any([y == int(x["type"]) for y in AttrTypeValue.values()])
137+
),
136138
},
137139
{"name": "is_mandatory", "type": bool},
138140
{"name": "is_delete_in_chain", "type": bool},
@@ -236,7 +238,9 @@ def do_edit(request, entity_id, recv_data):
236238
{
237239
"name": "type",
238240
"type": str,
239-
"checker": lambda x: (any([y == int(x["type"]) for y in AttrTypes])),
241+
"checker": lambda x: (
242+
any([y == int(x["type"]) for y in AttrTypeValue.values()])
243+
),
240244
},
241245
{"name": "is_mandatory", "type": bool},
242246
{"name": "is_delete_in_chain", "type": bool},

entry/tests/test_api_v2.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from airone.lib.log import Logger
1414
from airone.lib.test import AironeViewTest
1515
from airone.lib.types import (
16+
AttrDefaultValue,
1617
AttrType,
17-
AttrTypeStr,
1818
AttrTypeValue,
1919
)
2020
from entity.models import Entity, EntityAttr
@@ -396,7 +396,7 @@ def test_retrieve_entry(self):
396396
next(filter(lambda x: x["schema"]["name"] == "opt", resp_data["attrs"])),
397397
{
398398
"type": AttrType.STRING,
399-
"value": {"as_string": AttrTypeStr.DEFAULT_VALUE},
399+
"value": {"as_string": AttrDefaultValue[AttrType.STRING]},
400400
"id": None,
401401
"is_mandatory": False,
402402
"is_readable": True,

templates/edit_entity_content.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
<div class='row'>
4242
<div class='col-4'>
4343
<select class="attr_type" disabled="true">
44-
{% for type in attr_types %}
45-
<option value="{{ type.TYPE }}" {% if attr.type == type.TYPE %}selected="selected"{% endif %}>{{ type.NAME }}</option>
44+
{% for name, type in attr_types.items %}
45+
<option value="{{ type }}" {% if attr.type == type %}selected="selected"{% endif %}>{{ name }}</option>
4646
{% endfor %}
4747
</select>
4848
</div>
@@ -116,8 +116,8 @@
116116
<div class='col-4'>
117117

118118
<select class="attr_type">
119-
{% for type in attr_types %}
120-
<option value="{{ type.TYPE }}">{{ type.NAME }}</option>
119+
{% for name, type in attr_types.items %}
120+
<option value="{{ type }}">{{ name }}</option>
121121
{% endfor %}
122122
</select>
123123

0 commit comments

Comments
 (0)