diff --git a/docs/data_types.rst b/docs/data_types.rst index 2bee9f4..916fd78 100644 --- a/docs/data_types.rst +++ b/docs/data_types.rst @@ -14,6 +14,7 @@ ArrowType .. autoclass:: ArrowType + ChoiceType ^^^^^^^^^^ diff --git a/sqlalchemy_utils/types/locale.py b/sqlalchemy_utils/types/locale.py index 8346244..be5c48f 100644 --- a/sqlalchemy_utils/types/locale.py +++ b/sqlalchemy_utils/types/locale.py @@ -34,6 +34,7 @@ class LocaleType(types.TypeDecorator, ScalarCoercible): user = User() user.locale = Locale('en_US') + session.add(user) session.commit() diff --git a/sqlalchemy_utils/types/weekdays.py b/sqlalchemy_utils/types/weekdays.py index 4be83ec..594e2d8 100644 --- a/sqlalchemy_utils/types/weekdays.py +++ b/sqlalchemy_utils/types/weekdays.py @@ -6,6 +6,46 @@ from .bit import BitType class WeekDaysType(types.TypeDecorator, ScalarCoercible): + """ + WeekDaysType offers way of saving WeekDays objects into database. The + WeekDays objects are converted to bit strings on the way in and back to + WeekDays objects on the way out. + + In order to use WeekDaysType you need to install Babel_ first. + + .. _Babel: http://babel.pocoo.org/ + + :: + + + from sqlalchemy_utils import WeekDaysType, WeekDays + from babel import Locale + + + class Schedule(Base): + __tablename__ = 'schedule' + id = sa.Column(sa.Integer, autoincrement=True) + working_days = sa.Column(WeekDaysType) + + + schedule = Schedule() + schedule.working_days = WeekDays('0001111') + session.add(schedule) + session.commit() + + print schedule.working_days # Thursday, Friday, Saturday, Sunday + + + WeekDaysType also supports scalar coercion: + + :: + + + schedule.working_days = '1110000' + schedule.working_days # WeekDays object + + """ + impl = BitType(WeekDay.NUM_WEEK_DAYS) def process_bind_param(self, value, dialect): diff --git a/tests/types/test_weekdays.py b/tests/types/test_weekdays.py index cfc3266..1dac33c 100644 --- a/tests/types/test_weekdays.py +++ b/tests/types/test_weekdays.py @@ -1,11 +1,17 @@ +from babel import Locale import sqlalchemy as sa from sqlalchemy_utils.types import WeekDaysType from sqlalchemy_utils.primitives import WeekDays +from sqlalchemy_utils import i18n from tests import TestCase class WeekDaysTypeTestCase(TestCase): + def setup_method(self, method): + TestCase.setup_method(self, method) + i18n.get_locale = lambda: Locale('en') + def create_models(self): class Schedule(self.Base): __tablename__ = 'schedule'