Пример #1
0
    def closure(cls):
        class_name = cls.__name__ + 'Localized'
        tablename = plural_name(underscorize(class_name))
        if db.metadata.tables.get(tablename) is not None:
            return
        # TODO: pass language from the babel detection
        lang = u'en'
        cls_columns = cls.__table__.get_children()
        columns = dict([(c.name, c.copy()) for c in cls_columns if isinstance(c.type, (db.Unicode, db.UnicodeText))])
        localized_names = columns.keys()
        columns.update({
            'parent_id': db.Column(db.Integer, db.ForeignKey(cls.__tablename__ + '.id'), nullable=False),
            'parent': db.relationship(cls, backref='localized_ref'),
            'locale': db.Column(db.Unicode(255), default=lang, index=True)
        })

        cls_localized = type(class_name, (db.Model, CRUDMixin), columns)

        for field in localized_names:

            def getter(self):
                localized = cls_localized.query.filter_by(parent_id=self.id, locale=lang).first()
                return getattr(localized, field) or None

            def setter(self, value):
                localized = cls_localized.query.filter_by(parent_id=self.id, locale=lang).first() or cls_localized(parent=self, locale=lang)
                setattr(localized, field, value)
                localized.save()

            def expression(self):
                return db.Query(columns[field]).filter(cls_localized.parent_id == self.id, cls_localized.locale == lang).as_scalar()

            setattr(cls, field, hybrid_property(getter, setter, expr=expression))

        closure(cls)
Пример #2
0
 def __tablename__(cls):
     """ We want our app to be more English for pluralization cases
     """
     return plural_name(underscorize(cls.__name__))
Пример #3
0
# -*- encoding: utf-8 -*-
from datetime import datetime
# from sqlalchemy.ext.associationproxy import association_proxy
# from sqlalchemy.ext.declarative import declared_attr

from eventor import db
# from eventor.auth.models import User

from eventor.core.models import CRUDMixin
from eventor.core.utils import plural_name, underscorize


__all__ = ['Event']

plural_under = lambda name: plural_name(underscorize(name))
lazy_cascade = {
    'lazy': 'dynamic',
    'cascade': 'all',
}


class EventStory(db.Model, CRUDMixin):
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    author = db.relationship('User', backref=db.backref('stories', **lazy_cascade), uselist=False)
    name = db.Column(db.Unicode(255), nullable=False)
    description = db.Column(db.UnicodeText, nullable=False)

    @property
    def active(self):
        return self.events.filter(Event.reg_starts <= datetime.utcnow(),
                                  Event.reg_ends >= datetime.utcnow())