class Translation(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.Text, nullable=False) timestamp = db.Column(db.DATETIME, default=datetime.datetime.utcnow()) identifier_id = db.Column(db.Integer, db.ForeignKey('identifier.id'), nullable=False) identifier = db.relationship('Identifier', backref=db.backref('translations')) language_code = db.Column(db.String(5), db.ForeignKey('language.code'), nullable=False) language = db.relationship('Language', backref=db.backref('translations', lazy=True)) def __repr__(self): return '<Translation id=%s lang=%s>' % (self.id, self.language_code) def to_json_dict(self): return { 'id': self.id, 'text': self.text, 'timestamp': self.timestamp.strftime('%Y-%m-%d %H:%M:%S'), 'identifier': self.identifier.name }
class User(db.Model, Serializer): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) def serialize(self): return Serializer.serialize(self)
class Identifier(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), nullable=False) description = db.Column(db.String(255), nullable=True) project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False) project = db.relationship('Project', backref=db.backref('identifiers', lazy=True)) component_id = db.Column(db.Integer, db.ForeignKey('component.id'), nullable=True) component = db.relationship('Component', backref=db.backref('identifiers')) def __repr__(self): return '<Identifier %s>' % self.id def to_json_dict(self): return { 'id': self.id, 'name': self.name, 'componentID': self.component_id, 'componentName': self.component.name if self.component else None, 'description': self.description }
class BaseModel(db.Model): __abstract__ = True id = db.Column(db.Integer, primary_key=True) created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow) deleted_at = db.Column(db.DateTime, nullable=True)
class News(BaseModel): title = db.Column(db.String(255), nullable=False) description = db.Column(db.TEXT, nullable=False) status = db.Column(db.Enum('draft', 'publish'), nullable=False, default='draft') topics = db.relationship(Topics, secondary=news_topics, lazy='subquery', backref=db.backref('news_id', lazy=True), cascade="all,delete")
class Project(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), unique=True, nullable=False) description = db.Column(db.String(500), nullable=True) languages = db.relationship( "LanguageProjectRelation", backref="projects", primaryjoin=id == LanguageProjectRelation.project_id) def __repr__(self): return '<Project %s id=%s>' % (self.name, self.id)
class Language(db.Model): code = db.Column(db.String(5), primary_key=True) name = db.Column(db.String(20), unique=True, nullable=False) flag_image_link = db.Column(db.String(100), nullable=True) projects = db.relationship( "LanguageProjectRelation", backref="languages", primaryjoin=code == LanguageProjectRelation.lang_code) def __repr__(self): return '<Language %s code=%s>' % (self.name, self.code)
class Component(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=True) project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False) project = db.relationship('Project', backref=db.backref('usage_classes', lazy=True)) def __repr__(self): return '<Component %s id=%s>' % (self.name, self.id) def to_json_dict(self): return {'id': self.id, 'name': self.name}
class LanguageProjectRelation(db.Model ): # https://gist.github.com/kirang89/10030736 lang_code = db.Column(db.String(5), db.ForeignKey('language.code'), primary_key=True) project_id = db.Column(db.Integer, db.ForeignKey('project.id'), primary_key=True) is_default = db.Column(db.Boolean, default=False, nullable=False) language = db.relationship('Language', backref=db.backref('language_project_relation')) project = db.relationship('Project', backref=db.backref('language_project_relation')) def __repr__(self): return '<LP-Rel %s %s %s>' % (self.lang_code, self.project_id, self.is_default)
class Topics(BaseModel): name = db.Column(db.String(255), nullable=False, unique=True)
from helpers.database import db from helpers.base_model import BaseModel from apps.topics.topics_model import Topics news_topics = db.Table( 'news_topics', db.Column('news_id', db.Integer, db.ForeignKey('news.id', ondelete='CASCADE'), primary_key=True), db.Column('topics_id', db.Integer, db.ForeignKey('topics.id', ondelete='CASCADE'), primary_key=True)) class News(BaseModel): title = db.Column(db.String(255), nullable=False) description = db.Column(db.TEXT, nullable=False) status = db.Column(db.Enum('draft', 'publish'), nullable=False, default='draft') topics = db.relationship(Topics, secondary=news_topics, lazy='subquery', backref=db.backref('news_id', lazy=True), cascade="all,delete")