Fix: Verify string lengths when creating model objects

This commit is contained in:
Marcus Lindvall 2019-11-20 00:04:37 +01:00
parent 144fdbefb1
commit 1a7ada9d56
2 changed files with 28 additions and 20 deletions

View file

@ -11,13 +11,14 @@ from sqlalchemy.exc import OperationalError
from sqlalchemy.schema import MetaData, Column
from sqlalchemy.types import Integer
from sqlalchemy.orm.collections import InstrumentedList
from sqlalchemy import event
from sqlservice import ModelBase, as_declarative
from pyjeeves import logging
from . import db
from .ext import InstallValidatorListeners
from .ext import install_validator_listner
logger = logging.getLogger("PyJeeves." + __name__)
@ -38,7 +39,6 @@ except OperationalError as e:
class RawBaseModel(ModelBase):
""" Generalize __init__, __repr__ and to_json
Based on the models columns , ForetagKod=1"""
__sa_instrumentation_manager__ = InstallValidatorListeners
__to_dict_filter__ = []
__to_dict_only__ = ()
@ -75,9 +75,10 @@ class RawBaseModel(ModelBase):
filters
)
def _map_columns(self, key):
if key in self.__column_map__:
return self.__column_map__[key]
@classmethod
def _map_columns(cls, key):
if key in cls.__column_map__:
return cls.__column_map__[key]
return key
def _map_keys(self, data={}):
@ -185,3 +186,11 @@ class RawBaseModel(ModelBase):
def delete(self):
db.raw_session.delete(self)
db.raw_session.commit()
# Apply validators for all string attributes in subclasses of RawBaseModel
@event.listens_for(RawBaseModel, 'attribute_instrument')
def receive_attribute_instrument(cls, key, inst):
"listen for the 'attribute_instrument' event"
install_validator_listner(cls, key, inst)