class User(db.Model, UserMixin): #This class defines several fields as class variables. """ User of the Dating website.""" __tablename__ = 'users' id = db.Column(db.Integer, autoincrement=True, primary_key=True) firstname = db.Column(db.String(100), nullable=False) lastname = db.Column(db.String(100), nullable=False) email = db.Column(db.String(100), nullable=False, unique=True) username = db.Column(db.String(100), nullable=False) password = db.Column(db.String(100), nullable=False) date_of_birth = db.Column(db.String(100), nullable=False) zipcode = db.Column(db.String(15), nullable=False) phone = db.Column(db.String(100), nullable=False) image_file = db.Column(db.String(20), nullable=True, default='default.jpg') messages_sent = db.relationship('Message', foreign_keys='Message.sender_id', backref='sender', lazy='dynamic') messages_received = db.relationship('Message', foreign_keys='Message.recipient_id', backref='recipient', lazy='dynamic') last_message_read_time = db.Column(db.DateTime) notifications = db.relationship('Notification', backref='user', lazy='dynamic') interest = db.relationship('Interest', backref=db.backref('User')) def _repr_( self ): #The __repr__ method tells Python how to print objects of this class return f"User('{self.username}', '{self.email}', '{self.image_file}')" def new_messages(self): last_read_time = self.last_message_read_time or datetime(1900, 1, 1) return Message.query.filter_by(recipient=self).filter( Message.timestamp > last_read_time).count() def add_notification(self, name, data): self.notifications.filter_by(name=name).delete() n = Notification(name=name, payload_json=json.dumps(data), user=self) db.session.add(n) return n
class Outdoor(db.Model): """Holds the outdoor_activities and their corresponding ids""" __tablename__ = 'outdoors' outdoor_id = db.Column(db.Integer, autoincrement=True, primary_key=True) outdoor_activity = db.Column(db.String(40), nullable=False) interest = db.relationship('Interest', backref=db.backref('outdoor')) def __repr__(self): """displays the ids of oa, and oa Can be cross-referenced with the interests table""" return '<outdoor_id={}, outdoor_activity={}>'.format( self.outdoor_id, self.outdoor_activity)
class Religion(db.Model): """Holds the religious views and their corresponding ids""" __tablename__ = 'religions' religion_id = db.Column(db.Integer, autoincrement=True, primary_key=True) religion_name = db.Column(db.String(40), nullable=False) interest = db.relationship('Interest', backref=db.backref('religion')) def __repr__(self): """displays the ids of religion and religion names Can be cross-referenced with the interests table""" return '<religion_id={}, religion_name={}>'.format( self.religion_id, self.religion_name)
class Hobby(db.Model): """Holds the list of hobbies and thier corresponding ids""" __tablename__ = 'hobbies' hobby_id = db.Column(db.Integer, autoincrement=True, primary_key=True) hobby_name = db.Column(db.String(40), nullable=False) interest = db.relationship('Interest', backref=db.backref('hobby')) def __repr__(self): """displays the ids of hobbies and hobby names Can be cross-referenced with the interests table""" return '<hobby_id={}, hobby_name={}>'.format(self.hobby_id, self.hobby_name)
class BookGenre(db.Model): """Holds the Music_genres and their corresponding ids""" __tablename__ = 'book_genres' book_genre_id = db.Column(db.Integer, autoincrement=True, primary_key=True) book_genre_name = db.Column(db.String(40), nullable=False) interest = db.relationship('Interest', backref=db.backref('book_genre')) def __repr__(self): """displays the ids of Book genres and book genres Can be cross-referenced with the interests table""" return '<book_genre_id={}, book_genre_name={}>'.format( self.book_genre_id, self.book_genre_name)
class FavCuisine(db.Model): """Holds the types of cuisines and thier corresponding ids""" __tablename__ = 'fav_cuisines' fav_cuisine_id = db.Column(db.Integer, autoincrement=True, primary_key=True) fav_cuisine_name= db.Column(db.String(40), nullable=False) interest = db.relationship('Interest', backref=db.backref('fav_cuisine')) def __repr__ (self): """displays the ids of cuisines and cuisine names Can be cross-referenced with the interests table""" return'<fav_cuisine_id={}, fav_cuisine_name={}>'.format(self.fav_cuisine_id, self.fav_cuisine_name)
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=True) dp = db.Column(db.String(20), nullable=False, default='default.jpg') dp2 = db.Column(db.String(20), nullable=False, default='flip.png') dp3 = db.Column(db.String(20), nullable=False, default='plane.png') password = db.Column(db.String(60), nullable=False) last_seen = db.Column(db.DateTime, default=datetime.utcnow) date_joined = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) department = db.Column(db.String(20), nullable=False) student_number = db.Column(db.Integer(), unique=True, nullable=True) age = db.Column(db.DateTime, nullable=False) gender = db.Column(db.String(20), nullable=False) country = db.Column(db.String(20), default="Cyprus", nullable=True) bio = db.Column(db.String(120), nullable=True) private = db.Column(db.Boolean, default=False, nullable=False) snapchat = db.Column(db.String(20), default="snapchat", nullable=True) instagram = db.Column(db.String(20), default="instagram", nullable=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') messages_sent = db.relationship('Message', foreign_keys='Message.sender_id', backref='author', lazy='dynamic', cascade="all, delete-orphan") messages_received = db.relationship('Message', foreign_keys='Message.recipient_id', backref='recipient', lazy='dynamic', cascade="all, delete-orphan") last_message_read_time = 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') @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.dp}')" def birthday(self): if self.age.month == datetime.today().month: if self.age.day == datetime.today().day: return True 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 def new_messages(self): last_read_time = self.last_message_read_time or datetime(1900, 1, 1) return Message.query.filter_by(recipient=self).filter( Message.timestamp > last_read_time).count() @staticmethod def on_changed_body(target, value, oldvalue, initiator): allowed_tags = ['a', 'abbr', 'acronym', 'b', 'blockquote', 'code', 'em', 'i', 'li', 'ol', 'pre', 'strong', 'ul', 'h3', 'p', 'iframe'] target.content = bleach.linkify(bleach.clean(markdown(value, output_format='html'), tags=allowed_tags, strip=True))