class Notif(db.Model): id=db.Column(db.Integer(), primary_key=True) title=db.Column(db.String(20)) message=db.Column(db.Text()) def __repr__(self): return f"Notif(' : {self.message}')"
class Ebook(db.Model): id=db.Column(db.Integer, primary_key=True) user_id=db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) date_posted=db.Column(db.DateTime, nullable=False, default=datetime.utcnow) title=db.Column(db.String(40), nullable=False) author=db.Column(db.String(40), nullable=False) def __repr__(self): return f"Ebook('{self.title}', '{self.date_posted}')"
class Reply(db.Model): id=db.Column(db.Integer, primary_key=True) date_posted=db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content=db.Column(db.Text, nullable=False) user_id=db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) comment_id=db.Column(db.Integer, db.ForeignKey('comment.id'), nullable=False) def __repr__(self): return f"Reply('{self.author.username}')"
class Post(db.Model): id=db.Column(db.Integer, primary_key=True) title=db.Column(db.String(150), nullable=False) date_posted=db.Column(db.DateTime(), nullable=False, index=True, default=datetime.utcnow) content=db.Column(db.UnicodeText, nullable=False) user_id=db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) category=db.Column(db.String(20), nullable=False) comment=db.relationship('Comment', backref='post_comments', lazy='dynamic', cascade='all, delete-orphan') tags=db.relationship('Tag', secondary=tags, backref=db.backref('posts', lazy='dynamic'), lazy='dynamic') reads=db.Column(db.Integer, default=0) nbcomments=db.Column(db.Integer, default=0) liked=db.relationship('PostLike', backref='post_liked', lazy='dynamic', cascade='all, delete-orphan') nbrlikes=db.Column(db.Integer, default=0, nullable=False) extend_existing=True @property def read(self): return self.reads @read.setter def read(self, r): self.reads=r @property def like(self): return self.nbrlikes @like.setter def like(self, r): self.nbrlikes=r @like.setter def dislike(self, r): self.nbrlikes=r @property def nbrcomments(self): return self.nbcomments @nbrcomments.setter def nbrcomments(self, c): self.nbcomments=c def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')"
class Comment(db.Model): id=db.Column(db.Integer, primary_key=True) date_posted=db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content=db.Column(db.Text, nullable=False) user_id=db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) post_id=db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False) reply=db.relationship('Reply', backref='comment', lazy='dynamic', cascade='all, delete-orphan') nbrep=db.Column(db.Integer, default=0) @property def rep(self): return self.nbrep @rep.setter def rep(self, rep): self.nbrep+=rep
class Tag(db.Model): id=db.Column(db.Integer(), primary_key=True) title=db.Column(db.String(20)) def __repr__(self): return f"Tag('{self.title}')"
class PostLike(db.Model): id=db.Column(db.Integer, primary_key=True) user_id=db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) post_id=db.Column(db.Integer, db.ForeignKey('post.id'), nullable=False)
class Cover(db.Model): id=db.Column(db.Integer, primary_key=True) data=db.Column(db.LargeBinary, nullable=False) file_id=db.Column(db.Integer, db.ForeignKey('file.id'), nullable=False) extend_existing=True
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='dynamic', cascade='all, delete-orphan') location=db.Column(db.String(30), nullable=True) #ici member_since=db.Column(db.DateTime(), default=datetime.utcnow) notifs=db.relationship('Notif', secondary=notifs, backref=db.backref('user', lazy='dynamic'), lazy='dynamic') nbrnotifs=db.Column(db.Integer, default=0) comment=db.relationship('Comment', backref='author', lazy='dynamic', cascade='all, delete-orphan') reply=db.relationship('Reply', backref='author', lazy='dynamic', cascade='all, delete-orphan') files=db.relationship('File', backref='uploader',lazy='dynamic') aboutme=db.Column(db.Text, nullable=True) admin=db.Column(db.Boolean(), default=False) publisher=db.Column(db.Boolean(), default=False) confirmed=db.Column(db.Boolean(), default=False) liked=db.relationship('PostLike', backref='liker', lazy='dynamic', cascade='all, delete-orphan') recommendations=db.relationship('Ebook', backref='recommender', lazy='dynamic', cascade='all, delete-orphan') books=db.Column(db.Integer, default=0, nullable=True) extend_existing=True def ping(self): self.last_seen=datetime.utcnow() db.session.add(self) db.session.commit() def is_admin(self): return self.admin def is_publisher(self): return self.publisher def get_reset_token(self, expires_sec=1800): s = Serializer(current_app.config['SECRET_KEY'], expires_sec) return s.dumps({'user_id': self.id}).decode('utf-8') def generate_confirmation_token(self, expiration=3600): s=Serializer(current_app.config['SECRET_KEY'], expiration) return s.dumps({'confirm':self.id}).decode('utf-8') def confirm(self, token): s = Serializer(current_app.config['SECRET_KEY']) try: data = s.loads(token.encode('utf-8')) except: return False if data.get('confirm') != self.id: return False self.confirmed = True db.session.add(self) return True @property def getnot(self): return self.nbrnotifs @getnot.setter def getnot(self, n): self.nbrnotifs=n @property def getbooks(self): return self.books @getbooks.setter def getbooks(self, n): self.books=n def has_liked_post(self, post): return PostLike.query.filter( PostLike.user_id == self.id, PostLike.post_id == post.id).count() > 0 def like_post(self, post): if not self.has_liked_post(post): like = PostLike(user_id=self.id, post_id=post.id) db.session.add(like) def unlike_post(self, post): if self.has_liked_post(post): PostLike.query.filter_by( user_id=self.id, post_id=post.id).delete() @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 File(db.Model): __searchable__= ['title'] id=db.Column(db.Integer, primary_key=True) title=db.Column(db.String(150), nullable=False) date_posted=db.Column(db.DateTime, nullable=False, default=datetime.utcnow) data=db.Column(db.String(220), nullable=False) user_id=db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True) description=db.Column(db.String(500), nullable=False) category=db.Column(db.String(20), nullable=False) cover=db.relationship('Cover', backref='file', lazy='dynamic', cascade='all, delete-orphan') downloaded=db.Column(db.Integer, default=0) _img_id=db.Column(db.Integer, default=0) img_id=_img_id file_size=db.Column(db.String(10), nullable=True) auteur=db.Column(db.String(100), nullable=True) extend_existing=True def _set_img(self, id): self._img_id=id def downloads(self): self.downloaded+=1 def __repr__(self): return f"File('{self.title}', '{self.date_posted}')" _img_id=property(_set_img)
class Genre(db.Model): id=db.Column(db.Integer, primary_key=True) title=db.Column(db.String(20), nullable=False) def __repr__(self): return f"Genre ('{self.title}')"
from flask_login import UserMixin, current_user, login_required from myapp.factory import admin from flask_admin.contrib.sqla import ModelView from flask_admin import AdminIndexView, expose from flask_admin.form import rules from wtforms import widgets,TextAreaField, PasswordField import json @login_manager.user_loader def load_user(user_id): return User.query.get(int(user_id)) notifs=db.Table('user_notifs', db.Column('user_id', db.Integer, db.ForeignKey('user.id'), nullable=True), db.Column('notif_id', db.Integer, db.ForeignKey('notif.id'), nullable=True) ) 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='dynamic', cascade='all, delete-orphan') location=db.Column(db.String(30), nullable=True) #ici member_since=db.Column(db.DateTime(), default=datetime.utcnow) notifs=db.relationship('Notif', secondary=notifs, backref=db.backref('user', lazy='dynamic'), lazy='dynamic') nbrnotifs=db.Column(db.Integer, default=0)