class ThreadAssoc(db.Model): __tablename__ = 'user_to_thread' __table_args__ = (db.PrimaryKeyConstraint('user_id', 'thread_id'), ) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) # nullable? user = db.relationship('User', back_populates='active_threads') # thread is unaware of assocs referencing it? # on thread deletion can call subroutine to delete thread Assocs (linear time) # should cascade thread deletions to assoc deletions # Many (assocs) to One (Thread) thread_id = db.Column(db.Integer, db.ForeignKey('pleep_threads.id')) thread = db.relationship('Thread') # metadata # by existing this assoc may already suggest metadata state = db.Column(db.Boolean, nullable=False, default=True) notifications = db.Column(db.Integer, nullable=False, default=0) def jsonify(self): return { 'user_id': self.user_id, 'thread_id': self.thread_id, 'state': self.state }
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) designation = db.Column(db.String(50), nullable=False) dept = db.Column(db.String(150), nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) is_admin = db.Column(db.Boolean, default=False, nullable=False) posts = db.relationship('Post', backref='author', lazy=True) answers = db.relationship('Answer', backref='author', lazy=True) def get_reset_token(self, expires_sec=1800): s = Serializer(app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') @staticmethod def verify_reset_token(token): s = Serializer(app.config['SECRET_KEY']) try: user_id = s.loads(token)['user_id'] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}', '{self.designation}', '{self.dept}', '{self.is_admin}')"
class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, nullable=False, primary_key=True, unique=True, autoincrement=True) text_content = db.Column(db.String(1000), nullable=False) data_filename = db.Column(db.String(100), default=None, nullable=True) # tag required to display data_type = db.Column(db.String(100), default=None, nullable=True) visibility = db.Column(db.Boolean, default=True, nullable=False) timestamp = db.Column( db.Integer, nullable=False) # time since Unix Epoch (can be seconds granularity) edit_timestamp = db.Column(db.Integer, default=None) thread_id = db.Column(db.Integer, db.ForeignKey('pleep_threads.id')) thread = db.relationship('Thread', back_populates='posts') # Many (Posts) to One (User) author_id = db.Column(db.Integer, db.ForeignKey('users.id')) author = db.relationship('User', back_populates='myPosts') # many likes to many users likers = db.relationship('User', \ secondary=like_table, \ back_populates='liked') like_count = db.Column(db.Integer, nullable=False, default=0) def jsonify(self): likerIDs = [] for l in self.likers: likerIDs.append(l.id) return {'id': self.id, \ 'thread_id': self.thread_id, \ 'thread_title': self.thread.title, \ 'author_id': self.author_id, \ 'author_name': self.author.name, \ 'author_color': self.author.color, \ 'timestamp': self.timestamp, \ 'edit_timestamp': self.edit_timestamp, \ 'thread_timestamp_close': self.thread.timestamp_close, \ 'text_content': self.text_content, \ 'data_filename': self.data_filename, \ 'data_type' : self.data_type, \ 'visibility': self.visibility, \ 'likes': self.like_count, \ 'likerIDs': likerIDs}
class Thread(db.Model): __tablename__ = 'pleep_threads' id = db.Column(db.Integer, nullable=False, primary_key=True, unique=True, autoincrement=True) title = db.Column(db.String(100), nullable=False) categories = db.Column(db.String(1000)) # csv of 'hashtags' timestamp = db.Column(db.Integer, nullable=False) timestamp_close = db.Column(db.Integer) timestamp_delete = db.Column(db.Integer) closed = db.Column(db.Boolean, nullable=False, default=False) # back ref to display user info easier creator_id = db.Column(db.Integer, db.ForeignKey('users.id')) creator = db.relationship("User", back_populates='myThreads') # One (Thread) to Many (Posts) posts = db.relationship('Post', back_populates='thread') total_likes = db.Column(db.Integer, nullable=False, default=0) # permissions work best with a friends list, otherwise private list # requires knowing all other user's names? # unless it queries for all usernames and filters in realtime for autocomplete read_access = db.Column(db.Boolean, nullable=False, default=True) write_access = db.Column(db.Boolean, nullable=False, default=True) permission_list = db.Column(db.String(1000), nullable=False, default='') # csv? # generate list of post ids def jsonify(self): postIDs = [] for p in self.posts: postIDs.append(p.id) return {'id': self.id, \ 'title': self.title, \ 'categories': self.categories, \ 'timestamp': self.timestamp, \ 'timestamp_close': self.timestamp_close, \ 'timestamp_delete': self.timestamp_delete, \ 'closed': self.closed, \ 'creator': self.creator.name, \ 'creator_id': self.creator_id, \ 'postCount': len(self.posts), \ 'postIDs': postIDs, \ 'total_likes': self.total_likes}
class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password_hash = db.Column(db.String(128)) posts = db.relationship('Post', backref='author', lazy='dynamic') about_me = db.Column(db.String(140)) last_seen = db.Column(db.DateTime, default=datetime.utcnow) followed = db.relationship('User', secondary=followers, primaryjoin=(followers.c.follower_id == id), secondaryjoin=(followers.c.followed_id == id), backref=db.backref('followers', lazy='dynamic'), lazy='dynamic') def __repr__(self): return '<User {}>'.format(self.username) def set_password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def avatar(self, size): digest = md5(self.email.lower().encode('utf-8')).hexdigest() return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format( digest, size) def follow(self, user): if not self.is_following(user): self.followed.append(user) def unfollow(self, user): # print("user id",user.id) if self.is_following(user): self.followed.remove(user) def is_following(self, user): # print(user.id) return self.followed.filter( followers.c.followed_id == user.id).count() > 0 def followed_posts(self): followed = Post.query.join( followers, (followers.c.followed_id == Post.user_id)).filter( followers.c.follower_id == self.id) own = Post.query.filter_by(user_id=self.id) return followed.union(own).order_by(Post.timestamp.desc())
class Post(db.Model): __tablename__ = 'post' id = db.Column('id', db.Integer, primary_key=True) verified = db.Column('verified', db.Integer, default=0, nullable=True) title = db.Column('title', db.String(70), nullable=True) category = db.Column('category', db.String(10), nullable=True) description = db.Column('description', db.String(100), nullable=True) files = db.Column('file', db.String) date = db.Column('Date', db.String, nullable=True) user_id = db.Column('user_id', db.Integer, db.ForeignKey('user.id'), nullable=True) start_time = db.Column("Start Time", db.String, nullable=True) end_time = db.Column('End time', db.String, nullable=True) lesson = db.relationship('Lesson', backref=db.backref('lessons')) def __repr__(self, id, verified, title, category, description, files, date, user_id, start_time, end_time): self.id = id self.verified = verified self.title = title self.category = category self.files = files self.description = description self.date = date self.user_id = user_id self.start_time = start_time self.end_time = end_time
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default="default.jpg") password = db.Column(db.String(60), nullable=False) posts = db.relationship("Post", backref="author", lazy=True) def get_reset_token(self, expires_sec=1800): s = Serializer(current_app.config["SECRET_KEY"], expires_in=expires_sec) return s.dumps({"user_id": self.id}).decode("utf-8") @staticmethod def verify_reset_token(token): s = Serializer(current_app.config["SECRET_KEY"]) try: user_id = s.loads(token)["user_id"] except: return None return User.query.get(user_id) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')"
class User(db.Model, UserMixin): id = db.Column(db.Integer,primary_key=True) username = db.Column(db.String(20),unique=True,nullable=False) email = db.Column(db.String(120),unique=True,nullable=False) image_file = db.Column(db.String(20),nullable=False,default='default.jpg') password = db.Column(db.String(60),nullable=False) lessons = db.relationship('Lesson',backref='author',lazy=True) def __repr__(self): return f"User('{self.username}','{self.email}','{self.image_file}')"
class Post(db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) is_answered = db.Column(db.Boolean, default=False, nullable=False) answers = db.relationship('Answer', backref='post', lazy=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}', '{self.author}')"
class Inventory(db.Model): """Inventories are the groups in which the items are located""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80), unique=True) items = db.relationship('Item') def __init__(self, name): self.name = name def serialize(self): return {'id': self.id, 'name': self.name}
class PostAssoc(db.Model): __tablename__ = 'user_to_post' __table_args__ = (db.PrimaryKeyConstraint('user_id', 'post_id'), ) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) # nullable? user = db.relationship('User', back_populates='active_posts') post_id = db.Column(db.Integer, db.ForeignKey('posts.id')) post = db.relationship('Post') # metadata # postAssocs are made by followees creating posts # postAssocs are removed by...? timeout? state = db.Column(db.Boolean, nullable=False, default=True) notifications = db.Column(db.Integer, nullable=False, default=0) def jsonify(self): return { 'user_id': self.user_id, 'post_id': self.post_id, 'state': self.state }
class Upload(db.Model): __tablename__ = 'upload' id = db.Column('id', db.Integer, primary_key=True) title = db.Column('title', db.String(30)) category = db.Column('category', db.String(30)) description = db.Column('description', db.String(600)) price = db.Column('price', db.Integer) upload_ref = db.Column('upload_ref', db.VARCHAR) timestamp = db.Column(db.DateTime, default=datetime.datetime.utcnow) user_id = db.Column('user_id', db.Integer, db.ForeignKey('user.id'), nullable=False) comments = db.relationship('Comment', backref='upload', lazy='dynamic') def __repr__(self, id, title, category, description, price, upload_ref, user_id): self.id = id self.title = title self.category = category self.description = description self.price = price self.upload_ref = upload_ref self.user_id = user_id
class Item(db.Model): """Items are the objects stored in containers""" id = db.Column(db.Integer, primary_key=True) inventoryId = db.Column(db.Integer, db.ForeignKey('inventory.id'), primary_key=True) name = db.Column(db.String(200)) quantity = db.Column(db.Integer) purchaseDate = db.Column(db.Date) expirationDate = db.Column(db.Date) purchasePrice = db.Column(db.Float) inventory = db.relationship('Inventory') def __init__(self, inventoryId, name, quantity=1, purchaseDate=None, expirationDate=None, purchasePrice=None): self.inventoryId = inventoryId self.name = name self.quantity = quantity self.purchaseDate = purchaseDate self.expirationDate = expirationDate self.purchasePrice = purchasePrice def serialize(self): return { 'id': self.id, 'inventoryId': self.inventoryId, 'name': self.name, 'quantity': self.quantity, 'purchaseDate': serialize_date(self.purchaseDate), 'expirationDate': serialize_date(self.expirationDate), 'purchasePrice': self.purchasePrice }
class User(db.Model, UserMixin): __tablename__ = 'user' id = db.Column('id', db.Integer, primary_key=True) role = db.Column('role', db.Integer, default=0) sub_role = db.Column('sub role', db.Integer, default=1) fullname = db.Column('fullname', db.String(20)) username = db.Column('username', db.String(20), unique=True, nullable=True) password = db.Column('password', db.String(60), nullable=False) image_file = db.Column(db.String(60), nullable=False, default='default.jpg') id_type = db.Column('id_type', db.String(60), nullable=True) id_number = db.Column('id_number', db.String(), nullable=True) id_document = db.Column('id_document', db.String(60), nullable=True) nationality = db.Column('nationality', db.String(60), nullable=True) occupation = db.Column('occupation', db.String(60), nullable=True) email = db.Column('email', db.String(60), nullable=True,unique=True) province = db.Column('province', db.String(60), nullable=True) city = db.Column('city', db.String(60), nullable=True) phone = db.Column('phone', db.BIGINT(), nullable=True) posts = db.relationship('Post', backref='author', lazy=True) uploads = db.relationship('Upload', backref='uploader', lazy=True) lesson = db.relationship('Lesson', backref=db.backref('user_lessons')) comments = db.relationship('Comment', backref='author', lazy='dynamic') followed = db.relationship('User', secondary=followers, primaryjoin=(followers.c.follower_id == id), secondaryjoin=(followers.c.followed_id == id), backref=db.backref('followers', lazy='dynamic'), lazy='dynamic') book = db.relationship('Post', secondary=book,backref=db.backref('bookers', lazy='dynamic')) likes = db.relationship('Upload', secondary=likes,backref=db.backref('liked', lazy='dynamic')) def get_reset_token(self,expires_sec=1800): s = Serializer(current_app.config['SECRET_KEY'],expires_sec) return s.dumps({'id':self.id}).decode('utf-8') @staticmethod def verify_reset_token(token): s = Serializer(current_app.config['SECRET_KEY']) try: id = s.loads(token)['id'] except: return None return User.query.get(id) # def book(self,post): # if not self.booked(post): # self.has_booked.append(post) # def unbook(self,post): # if self.has_booked(post): # self.booked.remove(post) def follow(self, user): if not self.is_following(user): self.followed.append(user) def unfollow(self, user): if self.is_following(user): self.followed.remove(user) def is_following(self, user): return self.followed.filter( followers.c.followed_id == user.id).count() > 0 # is_admin = db.Column(db.Boolean,default=False) def followed_posts(self): followed = Post.query.join( followers, (followers.c.followed_id == Post.user_id)).filter( followers.c.follower_id == self.id) own = Post.query.filter_by(user_id=self.id) return followed.union(own).order_by(Post.timestamp.desc()) def __repr__(self, user_id, role, sub_role, fullname, username, password, image_file, id_type, id_number, id_document, nationality, occupation, email, province, city, phone): self.user_id = user_id self.role = role self.sub_role = sub_role self.fullname = fullname self.username = username self.password = password self.image_file = image_file self.id_type = id_type self.id_number = id_number self.id_document = id_document self.nationality = nationality self.occupation = occupation self.email = email self.province = province self.city = city self.phone = phone
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, unique=True, autoincrement=True) name = db.Column(db.String(50), unique=True, nullable=False) password = db.Column(db.String(100), unique=False, nullable=False) address = db.Column(db.String(50)) color = db.Column(db.String(8), default='#000000') admin = db.Column(db.Boolean, nullable=False, default=False) clout = db.Column(db.Integer, default=0, nullable=False) #clout_static = db.Column(db.Integer, default=0, nullable=False) lifetime_pleeps = db.Column(db.Integer, default=0) timestamp = db.Column(db.Integer, nullable=False) image_filename = db.Column(db.String(100), default=None, nullable=True) image_type = db.Column(db.String(100), default=None, nullable=True) myPosts = db.relationship('Post', back_populates='author') myThreads = db.relationship('Thread', back_populates='creator') # profile_thread, named "their Pleepline", # permission set to read true, write false (only them) # so thread queries must disclude these profile_thread_id = db.Column(db.Integer) # user's feed is active_threads they've followed and # active_posts which are created when a followee creates a post # array of "active" threads, notification of new replies? # One (user) to many (assocs) active_threads = db.relationship('ThreadAssoc', back_populates='user') # order_by='' active_posts = db.relationship('PostAssoc', back_populates='user') liked = db.relationship('Post', \ secondary=like_table, \ back_populates='likers') following = db.relationship('User', \ secondary=follow_table, \ primaryjoin = id==follow_table.c.follower_id, \ secondaryjoin = id==follow_table.c.followee_id, \ back_populates='followers') followers = db.relationship('User', \ secondary=follow_table, \ primaryjoin = id==follow_table.c.followee_id, \ secondaryjoin = id==follow_table.c.follower_id, \ back_populates='following') def jsonify(self): myThreadsIDs = [] for t in self.myThreads: myThreadsIDs.append(t.id) myPostsIDs = [] for p in self.myPosts: myPostsIDs.append(p.id) aThreadsIDs = [] for t in self.active_threads: aThreadsIDs.append(t.thread_id) aPostsIDs = [] for p in self.active_posts: aPostsIDs.append(p.post_id) followingIDs = [] for f in self.following: followingIDs.append(f.id) likedIDs = [] for l in self.liked: likedIDs.append(l.id) return {'id': self.id, 'name': self.name, 'admin': self.admin, \ 'address': self.address, 'color': self.color, 'clout': self.clout, \ 'image_filename': self.image_filename, \ 'myPosts': myPostsIDs, \ 'myThreads': myThreadsIDs, \ 'active_posts': aPostsIDs, \ 'active_threads': aThreadsIDs, \ 'followersCount': len(self.followers), \ 'followingIDs': followingIDs, \ 'profile_thread_id': self.profile_thread_id, \ 'lifetime_pleeps': self.lifetime_pleeps, \ 'likedIDs': likedIDs}