class UserLastUpdate(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) media_name = db.Column(db.String(50), nullable=False) media_type = db.Column(db.Enum(ListType), nullable=False) old_status = db.Column(db.Enum(Status)) new_status = db.Column(db.Enum(Status)) old_season = db.Column(db.Integer) new_season = db.Column(db.Integer) old_episode = db.Column(db.Integer) new_episode = db.Column(db.Integer) date = db.Column(db.DateTime, nullable=False)
class MoviesList(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) movies_id = db.Column(db.Integer, db.ForeignKey('movies.id'), nullable=False) status = db.Column(db.Enum(Status), nullable=False) score = db.Column(db.Float)
class AnimeList(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) anime_id = db.Column(db.Integer, db.ForeignKey('anime.id'), nullable=False) current_season = db.Column(db.Integer, nullable=False) last_episode_watched = db.Column(db.Integer, nullable=False) status = db.Column(db.Enum(Status), nullable=False) score = db.Column(db.Float)
class MoviesList(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) media_id = db.Column(db.Integer, db.ForeignKey('movies.id'), nullable=False) status = db.Column(db.Enum(Status), nullable=False) rewatched = db.Column(db.Integer, nullable=False, default=0) eps_watched = db.Column(db.Integer) favorite = db.Column(db.Boolean) score = db.Column(db.Float) comment = db.Column(db.Text)
class AnimeList(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) media_id = db.Column(db.Integer, db.ForeignKey('anime.id'), nullable=False) current_season = db.Column(db.Integer, nullable=False) last_episode_watched = db.Column(db.Integer, nullable=False) status = db.Column(db.Enum(Status), nullable=False) rewatched = db.Column(db.Integer, nullable=False, default=0) favorite = db.Column(db.Boolean) score = db.Column(db.Float) eps_watched = db.Column(db.Integer) comment = db.Column(db.Text)
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(15), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) registered_on = db.Column(db.DateTime, nullable=False) password = db.Column(db.String(60), nullable=False) homepage = db.Column(db.Enum(HomePage), nullable=False, default=HomePage.MYSERIESLIST) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') time_spent_series = db.Column(db.Integer, nullable=False, default=0) time_spent_movies = db.Column(db.Integer, nullable=False, default=0) time_spent_anime = db.Column(db.Integer, nullable=False, default=0) private = db.Column(db.Boolean, nullable=False, default=False) active = db.Column(db.Boolean, nullable=False, default=False) profile_views = db.Column(db.Integer, nullable=False, default=0) series_views = db.Column(db.Integer, nullable=False, default=0) anime_views = db.Column(db.Integer, nullable=False, default=0) movies_views = db.Column(db.Integer, nullable=False, default=0) biography = db.Column(db.Text) transition_email = db.Column(db.String(120)) activated_on = db.Column(db.DateTime) 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') def get_register_token(self): s = Serializer(app.config['SECRET_KEY']) return s.dumps({'user_id': self.id}).decode('utf-8') def get_email_update_token(self): s = Serializer(app.config['SECRET_KEY']) 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 user = User.query.get(user_id) if user is None: return None else: return user
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(15), unique=True, nullable=False) oauth_id = db.Column(db.String(50), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) registered_on = db.Column(db.DateTime, nullable=False) password = db.Column(db.String(60), nullable=False) homepage = db.Column(db.Enum(HomePage), nullable=False, default=HomePage.ACCOUNT) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') time_spent_series = db.Column(db.Integer, nullable=False, default=0) time_spent_movies = db.Column(db.Integer, nullable=False, default=0) time_spent_anime = db.Column(db.Integer, nullable=False, default=0) private = db.Column(db.Boolean, nullable=False, default=False) active = db.Column(db.Boolean, nullable=False, default=False) profile_views = db.Column(db.Integer, nullable=False, default=0) series_views = db.Column(db.Integer, nullable=False, default=0) anime_views = db.Column(db.Integer, nullable=False, default=0) movies_views = db.Column(db.Integer, nullable=False, default=0) biography = db.Column(db.Text) transition_email = db.Column(db.String(120)) activated_on = db.Column(db.DateTime) last_notif_read_time = db.Column(db.DateTime) role = db.Column(db.Enum(RoleType), nullable=False, default=RoleType.USER) series_list = db.relationship('SeriesList', backref='user', lazy=True) anime_list = db.relationship('AnimeList', backref='user', lazy=True) movies_list = db.relationship('MoviesList', backref='user', lazy=True) 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 add_follow(self, user): if not self.is_following(user): self.followed.append(user) def remove_follow(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 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') def get_register_token(self): s = Serializer(app.config['SECRET_KEY']) return s.dumps({'user_id': self.id}).decode('utf-8') def get_email_update_token(self): s = Serializer(app.config['SECRET_KEY']) return s.dumps({'user_id': self.id}).decode('utf-8') def count_notifications(self): last_notif_time = self.last_notif_read_time or datetime(1900, 1, 1) return Notifications.query.filter_by(user_id=self.id) \ .filter(Notifications.timestamp > last_notif_time).count() def get_notifications(self): return Notifications.query.filter_by(user_id=self.id).order_by(desc(Notifications.timestamp)).limit(8).all() def check_autorization(self, user_name): # retrieve the user user = User.query.filter_by(username=user_name).first() # No account with this username if not user: abort(404) # Protection of the admin account if self.role != RoleType.ADMIN and user.role == RoleType.ADMIN: abort(404) # Check if the current account can see the target's account if self.id == user.id or self.role == RoleType.ADMIN: pass elif user.private and not self.is_following(user): abort(404) return user def add_view_count(self, user, list_type): if self.role != RoleType.ADMIN and user.id != self.id: if list_type == ListType.SERIES: user.series_views += 1 elif list_type == ListType.ANIME: user.anime_views += 1 elif list_type == ListType.MOVIES: user.movies_views += 1 db.session.commit() @staticmethod def verify_reset_token(token): s = Serializer(app.config['SECRET_KEY']) try: user_id = s.loads(token)["user_id"] except: return None user = User.query.get(user_id) if not user: return None else: return user