class User(db.Model): """ User is the model to map the database to an Object. """ __tablename__ = 'users' def __init__(self, id=None, email='', profile=None): self.id = id self.email = email self.profile = profile if not None else Profile(user_id=self.id) id = db.Column(db.BigInteger, primary_key=True) email = db.Column(db.String(128), unique=True, nullable=False) profile = db.relationship('Profile', backref='user', lazy=True, uselist=False) threads = db.relationship('Thread', secondary=thread_users, back_populates='users') created_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) def _asdict(self): return { 'id': self.id, 'email': self.email, 'created_at': self.created_at, 'updated_at': self.updated_at }
class Message(db.Model): """ Message maps a db message row to an Object """ __tablename__ = 'messages' def __init__(self, user_id, thread_id, body): self.user_id = user_id self.thread_id = thread_id self.body = body id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.BigInteger, db.ForeignKey('users.id'), nullable=False) user = db.relationship('User') thread_id = db.Column(db.Integer, db.ForeignKey('threads.id'), nullable=False) body = db.Column(db.Text, nullable=False) created_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) def __repr__(self): return '<Message %r %r %r %r>' % (self.id, self.user_id, self.thread_id, self.updated_at) def _asdict(self): return { 'id': self.id, 'user_id': self.user_id, 'thread_id': self.thread_id, 'body': self.body, 'created_at': self.created_at, 'updated_at': self.updated_at }
class Thread(db.Model): """ Thread is the model to map the database to an object. """ __tablename__ = 'threads' id = db.Column(db.Integer, primary_key=True) is_active = db.Column(db.Boolean, default=True) users = db.relationship('User', secondary=thread_users, lazy='subquery', back_populates='threads') messages = db.relationship('Message', backref='thread', lazy=True) last_message_from = db.Column(db.BigInteger, db.ForeignKey('users.id'), nullable=True) message_pairs = db.Column(db.Integer) created_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) def __repr__(self): return '<Thread %d %r %r>' % (self.id, self.is_active, self.updated_at) def _asdict(self): return { 'id': self.id, 'is_active': self.is_active, 'users': self.users, 'created_at': self.created_at, 'updated_at': self.updated_at }
import datetime as dt from court.database import db """ thread_users is the table that represents the many to many relationship between threads and users. """ thread_users = db.Table( 'thread_users', db.Column('user_id', db.BigInteger, db.ForeignKey('users.id'), primary_key=True), db.Column('thread_id', db.Integer, db.ForeignKey('threads.id'), primary_key=True), ) class Thread(db.Model): """ Thread is the model to map the database to an object. """ __tablename__ = 'threads' id = db.Column(db.Integer, primary_key=True) is_active = db.Column(db.Boolean, default=True) users = db.relationship('User', secondary=thread_users, lazy='subquery',
class Profile(db.Model): """ Profile maps a db User row to an Object. """ __tablename__ = 'profiles' def __init__(self, user_id=0, first_name='', last_name='', profile_picture=''): self.user_id = int(user_id) self.first_name = first_name self.last_name = last_name self.profile_picture = profile_picture id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.BigInteger, db.ForeignKey('users.id'), nullable=False) first_name = db.Column(db.String(128), nullable=False) last_name = db.Column(db.String(128), nullable=False) profile_picture = db.Column(db.String(512), default='') gender = db.Column(db.String(128), nullable=False, default='') preferred_gender = db.Column(db.String(128), nullable=False, default='') # Male/Female/Both color = db.Column(db.String(128), nullable=False, default='') animal = db.Column(db.String(128), nullable=False, default='') _interests = db.Column(db.String, default='{}') @property def interests(self): value = json.loads(self._interests) if (type(value) is str): value = json.loads(value) return value @interests.setter def interests(self, value): if (type(value) is str): value = json.loads(value) self._interests = json.dumps(value) _match_history = db.Column(db.String, default='{}') @property def match_history(self): return json.loads(self._match_history) @match_history.setter def match_history(self, value): self._match_history = json.dumps(value) created_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) updated_at = db.Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) def _asdict(self): return { 'id': self.id, 'user_id': self.user_id, 'first_name': self.first_name, 'last_name': self.last_name, 'profile_picture': self.profile_picture, 'gender': self.gender, 'preferred_gender': self.preferred_gender, 'color': self.color, 'animal': self.animal, 'interests': self.interests, 'created_at': self.created_at, 'updated_at': self.updated_at }