mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-16 11:00:41 +00:00
Enhance code structure
This commit is contained in:
1
app/extensions/__init__.py
Normal file
1
app/extensions/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
|
2
app/extensions/sqlalchemy/__init__.py
Normal file
2
app/extensions/sqlalchemy/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from .types import ContainerColumn
|
||||
from .types import IntEnumColumn
|
42
app/extensions/sqlalchemy/types.py
Normal file
42
app/extensions/sqlalchemy/types.py
Normal file
@ -0,0 +1,42 @@
|
||||
import json
|
||||
from app import db
|
||||
|
||||
|
||||
class ContainerColumn(db.TypeDecorator):
|
||||
impl = db.String
|
||||
|
||||
def __init__(self, container_type, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.container_type = container_type
|
||||
|
||||
def process_bind_param(self, value, dialect):
|
||||
if isinstance(value, self.container_type):
|
||||
return json.dumps(value)
|
||||
elif isinstance(value, str) and isinstance(json.loads(value), self.container_type):
|
||||
return value
|
||||
else:
|
||||
return TypeError()
|
||||
|
||||
def process_result_value(self, value, dialect):
|
||||
return json.loads(value)
|
||||
|
||||
|
||||
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)
|
0
app/extensions/wtforms/__init__.py
Normal file
0
app/extensions/wtforms/__init__.py
Normal file
14
app/extensions/wtforms/validators.py
Normal file
14
app/extensions/wtforms/validators.py
Normal file
@ -0,0 +1,14 @@
|
||||
from wtforms.validators import ValidationError
|
||||
|
||||
|
||||
def FileSize(max_size_mb):
|
||||
max_size_b = max_size_mb * 1024 * 1024
|
||||
|
||||
def file_length_check(form, field):
|
||||
if len(field.data.read()) >= max_size_b:
|
||||
raise ValidationError(
|
||||
f'File size must be less or equal than {max_size_mb} MB'
|
||||
)
|
||||
field.data.seek(0)
|
||||
|
||||
return file_length_check
|
Reference in New Issue
Block a user