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 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'))
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))