Skip to content

Commit f7c936c

Browse files
authored
Merge pull request #30 from TTWShell/feature/EnumExt
added EnumExt implementation
2 parents f03ddd7 + 884bdd5 commit f7c936c

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Change history
22
==============
33

4+
1.2.5a1 (2018-10-25)
5+
6+
* Added EnumExt implementation
7+
48
1.2.5a0 (2018-10-22)
59

610
* Enhance utils.use_kwargs.

hobbit_core/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
VERSION = [1, 2, 5, 'a0']
1+
VERSION = [1, 2, 5, 'a1']

hobbit_core/flask_hobbit/db.py

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,33 @@ def reference_col(tablename, nullable=False, pk_name='id', **kwargs):
6161

6262

6363
class EnumExt(enum.Enum):
64+
""" serialize/deserialize sqlalchemy enum field
6465
"""
65-
TODO:
66-
* extension.
67-
"""
68-
pass
66+
@classmethod
67+
def strict_dump(cls, key, verbose=False):
68+
pos = 1 if verbose else 0
69+
return cls[key].value[pos]
70+
71+
@classmethod
72+
def dump(cls, key, verbose=False):
73+
ret = {'key': cls[key].value[0], 'value': cls[key].value[1]}
74+
if verbose:
75+
ret.update({'label': key})
76+
return ret
77+
78+
@classmethod
79+
def load(cls, val):
80+
pos = 1 if isinstance(val, str) else 0
81+
for elem in cls:
82+
if elem.value[pos] == val:
83+
return elem.name
84+
85+
@classmethod
86+
def to_opts(cls, verbose=False):
87+
opts = []
88+
for elem in cls:
89+
opt = {'key': elem.value[0], 'value': elem.value[1]}
90+
if verbose:
91+
opt.update({'label': elem.name})
92+
opts.append(opt)
93+
return opts

0 commit comments

Comments
 (0)