mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-26 19:34:19 +00:00
23 lines
676 B
Python
23 lines
676 B
Python
|
from app import db
|
||
|
|
||
|
|
||
|
class IntEnumColumn(db.TypeDecorator):
|
||
|
impl = db.Integer
|
||
|
|
||
|
def __init__(self, enum_type, *args, **kwargs):
|
||
|
super().__init__(*args, **kwargs)
|
||
|
self.enum_type = enum_type
|
||
|
|
||
|
def process_bind_param(self, value, dialect):
|
||
|
if isinstance(value, self.enum_type) and isinstance(value.value, int):
|
||
|
return value.value
|
||
|
elif isinstance(value, int):
|
||
|
return self.enum_type(value).value
|
||
|
elif isinstance(value, str):
|
||
|
return self.enum_type[value].value
|
||
|
else:
|
||
|
return TypeError()
|
||
|
|
||
|
def process_result_value(self, value, dialect):
|
||
|
return self.enum_type(value)
|