Skip to content

Commit 256c20c

Browse files
committed
Introduced BaseIntEnum
This has helper method isin() to check specified parameter is set in its Enum object
1 parent 1a5db51 commit 256c20c

File tree

6 files changed

+22
-16
lines changed

6 files changed

+22
-16
lines changed

airone/lib/acl.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
import enum
22

3+
from airone.lib.types import BaseIntEnum
4+
35
__all__ = ["ACLType", "ACLObjType"]
46

57

68
@enum.unique
7-
class ACLObjType(enum.IntEnum):
9+
class ACLObjType(BaseIntEnum):
810
Entity = 1 << 0
911
EntityAttr = 1 << 1
1012
Entry = 1 << 2
1113
EntryAttr = 1 << 3
1214
Category = 1 << 4
1315

1416

15-
class ACLType(enum.IntEnum):
17+
class ACLType(BaseIntEnum):
1618
Nothing = 1 << 0
1719
Readable = 1 << 1
1820
Writable = 1 << 2

airone/lib/elasticsearch.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from airone.lib.acl import ACLType
1212
from airone.lib.log import Logger
13-
from airone.lib.types import AttrType
13+
from airone.lib.types import AttrType, BaseIntEnum
1414
from entity.models import Entity
1515
from entry.settings import CONFIG
1616
from user.models import User
@@ -42,7 +42,7 @@ class AdvancedSearchResults(BaseModel):
4242

4343

4444
@enum.unique
45-
class FilterKey(enum.IntEnum):
45+
class FilterKey(BaseIntEnum):
4646
CLEARED = 0
4747
EMPTY = 1
4848
NON_EMPTY = 2

airone/lib/types.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
from typing import Any
33

44

5+
class BaseIntEnum(enum.IntEnum):
6+
@classmethod
7+
def isin(cls, v):
8+
return v in cls.__members__.values()
9+
10+
511
@enum.unique
6-
class AttrType(enum.IntEnum):
12+
class AttrType(BaseIntEnum):
713
OBJECT = 1 << 0
814
STRING = 1 << 1
915
TEXT = 1 << 2

entry/api_v2/views.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -860,10 +860,10 @@ def _validate_attrinfo(self):
860860
for info in json_loaded_value:
861861
if not any(x in info for x in ["name", "filterKey", "keyword"]):
862862
raise RequiredParameterError("(00)Invalid attrinfo was specified")
863-
if not FilterKey(int(info["filterKey"])):
863+
if not FilterKey.isin(int(info["filterKey"])):
864864
raise RequiredParameterError("(01)Invalid attrinfo was specified")
865865
except Exception as e:
866-
raise RequiredParameterError("(E0)Invalid attrinfo was specified (%s)" % str(e))
866+
raise RequiredParameterError(e)
867867

868868
return json_loaded_value
869869

entry/tests/test_api_v2.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -4928,11 +4928,8 @@ def test_delete_entries_with_all_parameter_and_attrinfo(self):
49284928
# send request to delete all items with attrinfo
49294929
attrinfo_as_str = json.dumps(
49304930
[
4931-
{
4932-
"name": "val",
4933-
"keyword": "hoge",
4934-
"filterKey": "3",
4935-
}
4931+
{"name": "ref", "keyword": "", "filterKey": "0"},
4932+
{"name": "val", "keyword": "hoge", "filterKey": "3"},
49364933
]
49374934
)
49384935
resp = self.client.delete(

job/models.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from acl.models import ACLBase
1616
from airone.lib import auto_complement
1717
from airone.lib.log import Logger
18+
from airone.lib.types import BaseIntEnum
1819
from entity.models import Entity
1920
from entry.models import Entry
2021
from job.settings import CONFIG as JOB_CONFIG
@@ -36,7 +37,7 @@
3637
CUSTOM_PARALLELIZABLE_OPERATIONS = []
3738
CUSTOM_TASKS = {}
3839

39-
class JobOperationCustom(enum.IntEnum): # type: ignore
40+
class JobOperationCustom(BaseIntEnum): # type: ignore
4041
pass
4142

4243

@@ -47,7 +48,7 @@ def _support_time_default(o):
4748

4849

4950
@enum.unique
50-
class JobOperation(enum.IntEnum):
51+
class JobOperation(BaseIntEnum):
5152
# Constant to describes status of each jobs
5253
CREATE_ENTRY = 1
5354
EDIT_ENTRY = 2
@@ -82,14 +83,14 @@ class JobOperation(enum.IntEnum):
8283

8384

8485
@enum.unique
85-
class JobTarget(enum.IntEnum):
86+
class JobTarget(BaseIntEnum):
8687
UNKNOWN = 0
8788
ENTRY = 1
8889
ENTITY = 2
8990

9091

9192
@enum.unique
92-
class JobStatus(enum.IntEnum):
93+
class JobStatus(BaseIntEnum):
9394
PREPARING = 1
9495
DONE = 2
9596
ERROR = 3

0 commit comments

Comments
 (0)