class CommentObject(Object, RenderedTextMixin): __mapper_args__ = {'polymorphic_identity': COMMENT_ID} CAPABILITIES = [Object.Capabilities.listed_for_activity] in_reply_to_id = db.Column(db.Integer, db.ForeignKey("objects.id"), nullable=True)
class Activity(db.Model): """ We record activities on objects through this """ @unique class Capabilities(Enum): pass __tablename__ = "activities" query_class = AccessQuery id = db.Column(db.Integer, primary_key=True) subject_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False) discriminator = db.Column('verb', db.String(100), nullable=False) created_at = db.Column('created_at', db.DateTime(), nullable=False, server_default=func.now()) object_id = db.Column(db.Integer, db.ForeignKey("objects.id"), nullable=True) whom_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=True) payload = db.Column('payload', JSONB, nullable=True) __mapper_args__ = {'polymorphic_on': discriminator} subject = db.relationship(User, backref=db.backref("activities"), foreign_keys=subject_id) object = db.relationship(Object, backref=db.backref("activities"))
class Object(db.Model): """ This is the primary base class for all kind of objects we know of inside the system """ @unique class Capabilities(Enum): # This type is to be shown in default lists # like 'top', 'latest' etc listed = 'listed' # If the type isn't listed but has `listed_for_activity` # it can show up in lists about activitys, for example # when an object got liked listed_for_activity = 'a_listable' # This can be searched for searchable = 'searchable' __tablename__ = "objects" query_class = ObjectQuery id = db.Column(db.Integer, primary_key=True) discriminator = db.Column('type', db.String(100), nullable=False) created_at = db.Column('created_at', db.DateTime(), nullable=False, server_default=func.now()) payload = db.Column('payload', JSONB, nullable=True) owner_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False) belongs_to = db.Column(db.Integer, db.ForeignKey("objects.id"), nullable=True) # children = db.relationship("Object", backref=db.backref('belongs_to', # remote_side=id)) __mapper_args__ = {'polymorphic_on': discriminator} owner = db.relationship(User, backref=db.backref('objects'))
from beavy.app import db from beavy.models.activity import Activity from beavy.models.object import Object from sqlalchemy import event class Like(Activity): __mapper_args__ = { 'polymorphic_identity': 'like' } Object.likes_count = db.Column(db.Integer()) @event.listens_for(Like, 'after_insert') def update_object_likes_count(mapper, connection, target): # unfortunately, we can't do an aggregate in update directly... likes_count = Like.query.filter(Like.object_id == target.object_id).count() Object.query.filter(Object.id == target.object_id ).update({'likes_count': likes_count})
from beavy.models.object import Object from beavy.models.object import User from beavy.common.payload_property import PayloadProperty from beavy.utils.url_converters import ModelConverter from beavy.app import db PM_ID = "private_message" # Define models PMParticipants = db.Table( '{}_participants'.format(PM_ID), db.Column('user_id', db.Integer(), db.ForeignKey(User.id), nullable=False), db.Column('pm_id', db.Integer(), db.ForeignKey("objects.id"), nullable=False)) class PrivateMessage(Object): __mapper_args__ = {'polymorphic_identity': PM_ID} title = PayloadProperty('title') # db.Column(db.String(255), nullable=False) participants = db.relationship('User', secondary=PMParticipants, backref=db.backref('{}s'.format(PM_ID), lazy='dynamic'))
class User(db.Model, UserMixin): __LOOKUP_ATTRS__ = [] id = db.Column(db.Integer, primary_key=True) created_at = db.Column('created_at', db.DateTime(), nullable=False, server_default=func.now()) email = db.Column(db.String(255), unique=True, nullable=False) name = db.Column(db.String(255)) password = db.Column(db.String(255)) active = db.Column(db.Boolean()) confirmed_at = db.Column(db.DateTime()) last_login_at = db.Column(db.DateTime()) current_login_at = db.Column(db.DateTime()) last_login_ip = db.Column(db.String(255)) current_login_ip = db.Column(db.String(255)) login_count = db.Column(db.Integer()) roles = db.relationship('Role', secondary=roles_users, backref=db.backref('users', lazy='dynamic')) def __str__(self): return "<User #{} '{}' ({})>".format(self.id, self.name or "", self.email)
from flask.ext.security import UserMixin from flask_security.forms import ConfirmRegisterForm, RegisterForm, TextField from sqlalchemy import func from beavy.app import db # Define models roles_users = db.Table( 'roles_users', db.Column('user_id', db.Integer(), db.ForeignKey('user.id'), nullable=False), db.Column('role_id', db.Integer(), db.ForeignKey('role.id'), nullable=False)) RegisterForm.name = TextField('Full Name') ConfirmRegisterForm.name = TextField('Full Name') class User(db.Model, UserMixin): __LOOKUP_ATTRS__ = [] id = db.Column(db.Integer, primary_key=True) created_at = db.Column('created_at', db.DateTime(), nullable=False, server_default=func.now()) email = db.Column(db.String(255), unique=True, nullable=False) name = db.Column(db.String(255)) password = db.Column(db.String(255))
class Role(db.Model, RoleMixin): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(80), unique=True, nullable=False) description = db.Column(db.String(255))