class Message(db.Model): __tablename__ = 'chat_messages' id = db.Column(db.Integer(), primary_key=True) text = db.Column(db.Unicode()) created_date = db.Column(db.DateTime()) room_id = db.Column(db.Integer(), db.ForeignKey('chat_rooms.id')) user_name = db.Column(db.String(), db.ForeignKey('users.username'))
class Post(db.Model): id_post = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(80)) content = db.Column(db.String(255)) logo = db.Column(db.String(255), default=None, nullable=True) create_date = db.Column(db.DateTime, default=datetime.date.today()) update_date = db.Column(db.DateTime, default=datetime.date.today()) tag_id = db.Column(db.Integer, db.ForeignKey('tag.id_tag')) blog_id = db.Column(db.Integer, db.ForeignKey('blog.id_blog')) status = db.Column(db.Boolean, default=True) _tablename_ = 'post'
class Megazine(db.Model): # __tablename__ = 'megazine' id = db.Column(db.Integer, primary_key=True) id_pub = db.Column(db.String(150), unique=True) id_author = db.Column(db.String(150), db.ForeignKey('user.id_pub')) title = db.Column(db.String(40)) about = db.Column(db.String(100)) timestamp = db.Column(db.DateTime, default=datetime.now()) def __init__(self, id_author, title, about): self.id_pub = 'megazine-' + str(uuid1()) self.id_author = id_author self.title = title self.about = about @classmethod def find_by_id(cls, id_pub): return cls.query.filter_by(id_pub=id_pub).first() @classmethod def find_by_author(cls, id_author): return cls.query.filter_by(id_author=id_author).first() # simpan def save_to_db(self): db.session.add(self) db.session.commit() # hapus def delete_to_db(self): db.session.delete(self) db.session.commit() def json(self): return { 'id_pub': self.id_pub, 'id_author': self.id_author, 'title': self.title, 'about': self.about, 'timestamp': str(self.timestamp) } # di gunakan untuk debug def __repr__(self): return ('title {}'.format(self.title))
from flask_security import RoleMixin, UserMixin from core.db import db from core.view import BaseAdminView 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) description = db.Column(db.String(255)) def __str__(self): return self.name class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key = True) first_name = db.Column(db.String(255)) last_name = db.Column(db.String(255)) email = db.Column(db.String(255), unique = True, nullable = False) password = db.Column(db.String(255), nullable = False) active = db.Column(db.Boolean(), nullable = False) roles = db.relationship('Role', secondary = roles_users, backref = 'users') def __str__(self): return self.first_name + " " + self.last_name + " <" + self.email + ">"
class Subscription(db.Model): __tablename__ = 'chat_subscriptions' room_id = db.Column(db.Integer(), db.ForeignKey('chat_rooms.id')) user_id = db.Column(db.Integer(), db.ForeignKey('users.id'))