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) zip_code = db.Column(db.Integer, nullable=False) notes = db.relationship('Note', backref='author', lazy=True) links = db.relationship('Link', backref='author', lazy=True) sent_messages = db.relationship('Message', backref="sender", lazy=True) def get_reset_token(self, expieres_sec=9000): s = Serializer(current_app.config['SECRET_KEY'], expieres_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 Link(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.String(80)) url = db.Column(db.String(255)) project_id = db.Column(db.Integer, db.ForeignKey('project.id')) def __repr__(self): return '{} [{}]'.format(self.text, self.url)
class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(10), nullable=False, unique=True) def __repr__(self): return f"Role('{self.id}', '{self.name}' )"
class Project(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) url = db.Column(db.String(255)) description = db.Column(db.Text) date = db.Column(db.Date, default=date.today()) display = db.Column(db.Boolean, default=True) links = db.relationship('Link', backref='project', lazy='select') tags = db.relationship('Tag', secondary=tags, backref=db.backref('projects', lazy='dynamic')) tag_id = db.Column(db.Integer, db.ForeignKey('tag.id')) def __repr__(self): return '{}'.format(self.name)
class Link(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) address = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Link('{self.title}', '{self.address}'')"
class Note(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), unique=True, nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Note('{self.title}', '{self.date_posted}'')"
class Message(db.Model): id = db.Column(db.Integer, primary_key=True) sender_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) recipient_id = db.Column(db.Integer, nullable=False) subject = db.Column(db.String(120), nullable=False, default="Subject") body = db.Column(db.Text, nullable=False) time_sent = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) unread = db.Column(db.Boolean, nullable=False, default=True) def __repr__(self): return f"""Message(Sender: {User.query.get(self.sender_id).username}
class Newsletter(db.Model): __tablename__ = 'newsletters' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) content = db.Column(db.Text, nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) sendOut = db.Column(db.Boolean(), nullable=False, default=False) def __repr__(self): return f"Comment('{self.id}')"
class User(db.Model, UserMixin): __tablename__ = 'users' 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') date_joined = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) password = db.Column(db.String(60), nullable=False) roles = db.relationship('Role', secondary='user_roles') posts = db.relationship('Post', backref='author', lazy=True) portfolios = db.relationship('Portfolio', backref='creator', lazy=True) comments_ = db.relationship('Comment', backref='author_comment', lazy=True) discussions_ = db.relationship('Discussion', backref='author_discussion', lazy=True) newsletter = db.Column(db.Boolean(), nullable=False, default=False) activated = db.Column(db.Boolean(), nullable=False, default=False) banned = db.Column(db.Boolean(), nullable=False, default=False) 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') @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.id}', '{self.username}', '{self.email}', '{self.image_file}', '{self.roles}' )"
class Post(SearchableMixin, db.Model): __tablename__ = 'posts' __searchable__ = ['content'] id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) abstract = db.Column(db.Text, nullable=False) content = db.Column(db.Text, nullable=False) region = db.Column(db.Integer, default=0) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) comments = db.relationship('Comment', backref='post_comment', lazy=True) who_liked = db.relationship('User', backref='post_liked', lazy=True) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}' )"
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50), unique=True) def __repr__(self): return '<{}>'.format(self.name)