Example #1
0
 def tags(cls):
     if not hasattr(cls, "Tag"):
         # Create the Tag model
         tag_attrs = {
             "id":
             db.Column(db.Integer, primary_key=True),
             "objects":
             db.relationship(
                 cls,
                 secondary=lambda: cls.__tags_association_table__,
                 backref="related_tags",
             ),
         }
         cls.Tag = type(f"{cls.__name__}Tag", (ClassifierMixin, db.Model),
                        tag_attrs)
         # The many-to-many association table
         cls.__tags_association_table__ = _prepare_association_table(
             table_name=f"{cls.__tablename__}s_tags",
             remote1=cls.__tablename__,
             remote2=cls.Tag.__tablename__,
         )
     return association_proxy(
         "related_tags",
         "title",
         creator=lambda t: cls.Tag.get_or_create(title=t))
Example #2
0
 def last_edited_by(cls):
     return db.relationship(
         "User",
         foreign_keys=[cls.editor_id],
         info=dict(label=lazy_gettext("Author"),
                   description=lazy_gettext("")),
     )
Example #3
0
class User(db.Model, UserMixin, SQLAEvent):
    id = db.Column(db.Integer, primary_key=True)
    user_name = db.Column(
        db.Unicode(128),
        nullable=False,
        unique=True,
        index=True,
        info=dict(
            label=lazy_gettext("User Name"),
            description=lazy_gettext(
                "A unique name for this user (used for login)"),
        ),
    )
    email = db.Column(
        db.String(255),
        unique=True,
        info=dict(label=lazy_gettext("Email"), description=lazy_gettext("")),
    )
    password = db.Column(
        db.String(255),
        info=dict(label=lazy_gettext("Password"),
                  description=lazy_gettext("")),
    )
    active = db.Column(
        db.Boolean,
        info=dict(
            label=lazy_gettext("Active"),
            description=lazy_gettext(
                "Activate or deactivate this user account"),
        ),
    )
    confirmed_at = db.Column(db.DateTime(),
                             info=dict(label=lazy_gettext("Confirmed At")))
    roles = db.relationship(
        "Role",
        secondary=roles_users,
        backref=db.backref("users", lazy="dynamic"),
        info=dict(label=lazy_gettext("Roles"), description=lazy_gettext("")),
    )
    # TODO: remove this one
    name = property(fget=lambda self: self.profile.name)

    @validates("password")
    def validate_password(self, key, value):
        """TBD later"""
        return value

    @validates("email")
    def validate_email(self, key, value):
        if not is_valid_email(value):
            raise ValueError("Invalid email address for %r" % self)
        return value

    def __str__(self):
        return self.user_name

    def __repr__(self):
        return "User(user_name={})".format(self.user_name)
Example #4
0
 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))
Example #5
0
 def parent(cls):
     return db.relationship(
         cls,
         remote_side=cls.id,
         info=dict(
             label="Parent Page",
             description="The page under which this page will be added",
         ),
     )
Example #6
0
 def children(cls):
     return db.relationship(
         cls,
         lazy="dynamic",
         join_depth=2,
         cascade="all, delete-orphan",
         order_by=f"{cls.__name__}.{cls.__children_ordering_column__}",
         info=dict(label="Children", description=""),
     )
Example #7
0
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()>"
Example #8
0
 def comments(cls):
     if not hasattr(cls, "Comment"):
         comment_attrs = {"id": db.Column(db.Integer, primary_key=True)}
         cls.Comment = type(
             f"{cls.__name__}Comment", (CommentMixin, db.Model), comment_attrs
         )
         # The many-to-many association table
         cls.__comments_association_table__ = _prepare_association_table(
             table_name=f"{cls.__tablename__}s_comments",
             remote1=cls.__tablename__,
             remote2=cls.Comment.__tablename__,
         )
     return db.relationship(
         cls.Comment, secondary=cls.__comments_association_table__, backref="objects"
     )
Example #9
0
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
Example #10
0
class SettingsProfile(SQLAEvent, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(
        db.Unicode(255),
        unique=True,
        nullable=False,
        info=dict(
            label="Profile Name:",
            description="A Unique name for this settings profile.",
        ),
    )
    is_active = db.Column(
        db.Boolean,
        default=False,
        info=dict(
            label="Active",
            description=
            "Sets or unsets this settings profile as the default profile",
        ),
    )
    settings = db.relationship("Settings", backref="profile", uselist=False)

    def on_init(self):
        if self.settings is None:
            self.settings = Settings()

    def after_flush_postexec(self, session, is_modified):
        if self.is_active:
            thistbl = get_owning_table(self, "is_active")
            up = (db.update(thistbl).where(
                db.and_(thistbl.c.id != self.id,
                        thistbl.c.is_active == True)).values(is_active=False))
            db.session.execute(up)

    def __str__(self):
        return self.name

    def __repr__(self):
        return "SettingsProfile(name='{}')".format(self.name)