def _prepare_association_table(table_name, remote1, remote2): return db.Table( table_name, db.metadata, db.Column(f"{remote1}_id", db.Integer, db.ForeignKey(f"{remote1}.id")), db.Column(f"{remote2}_id", db.Integer, db.ForeignKey(f"{remote2}.id")), )
class Setting(DynamicProp, db.Model): __tablename__ = "setting" id = db.Column(db.Integer, primary_key=True) parent_id = db.Column(db.Integer, db.ForeignKey("settings.id")) category_id = db.Column(db.Integer, db.ForeignKey("setting_category.id")) cat = relationship("SettingCategory", backref="settings") category = association_proxy( "cat", "name", creator=lambda n: SettingCategory.get_or_create(name=n)) def __repr__(self): return "<Setting key={}><category:{}>".format(self.key, self.category)
def editor_id(cls): return db.Column( db.Integer, db.ForeignKey("user.id"), nullable=True, info=dict(label=lazy_gettext("Author")), )
def category_id(cls): if not hasattr(cls, "Category"): category_attrs = { "id": db.Column(db.Integer, primary_key=True), "objects": db.relationship(cls, backref="category"), } cls.Category = type(f"{cls.__name__}Category", (ClassifierMixin, db.Model), category_attrs) return db.Column(db.Integer, db.ForeignKey(cls.Category.id))
class Settings(ImmutableProxiedDictMixin, db.Model, SQLAEvent): id = db.Column(db.Integer, primary_key=True) store = db.relationship( "Setting", collection_class=attribute_mapped_collection("key")) _proxied = association_proxy("store", "value") profile_id = db.Column(db.Integer, db.ForeignKey("settings_profile.id"), nullable=False) def on_init(self): process_func = lambda seq: [Field(**d) for d in seq if type(d) is dict] ready = ((c, process_func(l)) for c, l in current_app.provided_settings) for category, opts in ready: for opt in opts: setting = Setting(key=opt.name) setting.value = opt.default setting.category = str(category) db.session.add(setting) self.store[opt.name] = setting
class Profile(ImmutableProxiedDictMixin, db.Model, TimeStampped): id = db.Column(db.Integer, primary_key=True) user_id = db.Column( db.Integer, db.ForeignKey("user.id"), unique=True, nullable=False, info=dict(label=lazy_gettext("User")), ) user = db.relationship( User, backref=backref("profile", uselist=False, cascade="all, delete-orphan"), single_parent=True, info=dict(label=lazy_gettext("User"), description=lazy_gettext("")), ) extras = db.relationship( "ProfileExtras", collection_class=attribute_mapped_collection("key") ) _proxied = association_proxy("extras", "value") def __repr__(self): return f"<{self.user.user_name}: Profile()>"
class ProfileExtras(DynamicProp, db.Model): id = db.Column(db.Integer, primary_key=True) profile_id = db.Column(db.Integer, db.ForeignKey("profile.id"))
def parent_id(cls): return db.Column(db.Integer, db.ForeignKey(cls.id), info=dict(label="Parent"))
:license: MIT, see LICENSE for more details. """ from sqlalchemy import inspect from sqlalchemy.orm import validates from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy.ext.associationproxy import association_proxy from flask_security import UserMixin, RoleMixin from oy.boot.sqla import db from oy.babel import lazy_gettext from oy.helpers import is_valid_email from oy.models.mixins import TimeStampped, SQLAEvent roles_users = db.Table( "roles_users", db.Column("user_id", db.Integer(), db.ForeignKey("user.id")), db.Column("role_id", db.Integer(), db.ForeignKey("role.id")), ) class Role(db.Model, RoleMixin): id = db.Column(db.Integer(), primary_key=True) name = db.Column( db.String(80), unique=True, info=dict( label=lazy_gettext("Name"), description=lazy_gettext("A name to identify this role"), ), ) description = db.Column(