class OAuth(db.Model): id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id")) oauth_token = db.Column(db.String(1024), nullable=False) oauth_token_secret = db.Column(db.String(1024), nullable=True) oauth_type = db.Column(db.SmallInteger, default=OAUTH_FACEBOOK) def __init__(self, user_id, oauth_token, oauth_token_secret=None, oauth_type=OAUTH_FACEBOOK): self.user_id = user_id self.oauth_token = oauth_token self.oauth_token_secret = oauth_token_secret self.oauth_type = oauth_type def __repr__(self): return u"<OAuth %s : %s : %s>" % (self.user_id, self.oauth_type, self.oauth_token)
class User(db.Model, UserMixin): """ The User object adding columns """ __tablename__ = "user" id = db.Column(db.Integer, primary_key=True) firstname = db.Column(db.String(256), nullable=False) lastname = db.Column(db.String(256), nullable=False, index=True) emailaddress = db.Column(db.String(1024), nullable=False, index=True, unique=True) password = db.Column(db.String(1024), nullable=True) role = db.Column(db.SmallInteger, default=ROLE_USER) active = db.Column(db.Boolean, default=True) created = db.Column(db.DateTime, default=datetime.utcnow) # relations diaries = db.relationship("Diary", secondary=dairy_user_table, lazy="dynamic", backref="users") posts = db.relationship("Post", lazy="dynamic") def __init__(self, firstname, lastname, emailaddress, password=None): self.firstname = firstname self.lastname = lastname self.emailaddress = emailaddress if password is not None: self.password = bcrypt.generate_password_hash(password) def is_password_correct(self, password): return bcrypt.check_password_hash(self.password, password) def has_access(self, diary_id): return len(self.diaries.filter(Diary.id == diary_id).all()) is 1 def get_diary(self, slug): if self.role == ROLE_USER: return self.diaries.filter(Diary.slug == slug) else: return Diary.query.filter(Diary.slug == slug) def sorted_diaries(self): return self.diaries.order_by(Diary.title) def last_post(self): return self.posts.order_by(Post.created.desc()).first() def __repr__(self): return u"<User %d : %s>" % (self.id, self.emailaddress)
class User(db.Model): id = db.Column(db.String(), primary_key=True, default=uuid.uuid4().hex) username = db.Column(db.String(80), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) password = db.Column(db.String(60), nullable=False) notes = db.relationship('Note', backref='user', lazy=True) def __repr__(self): return f"User('{self.id}','{self.username}','{self.email}', '{self.notes}')" @classmethod def find_user_by_email(cls, email): return User.query.filter(User.email == email).first() @classmethod def find_user_by_id(cls, user_id): return User.query.filter(User.id == user_id).first()
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) firstname = db.Column(db.String(50), nullable=False) lastname = db.Column(db.String(50), nullable=False) username = db.Column(db.String(25), unique=True, nullable=False) dob = db.Column(db.String(20), nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) kin_email = db.Column(db.String(120), nullable=False) password = db.Column(db.String(60), nullable=False) join_date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) entries = db.relationship('Entry', 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.firstname}', '{self.lastname}', '{self.username}', '{self.email}')"
class Entry(db.Model): id = db.Column(db.Integer, primary_key=True) date = db.Column(db.DateTime, nullable=False, default=datetime.now()) title = db.Column(db.String(100), nullable=True) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"User('{self.date}', '{self.content[:10]}...')"
class Picture(db.Model): """ The Picture object """ __tablename__ = "picture" id = db.Column(db.Integer, primary_key=True) post_id = db.Column(db.Integer, db.ForeignKey("post.id")) title = db.Column(db.String(1024), nullable=False, index=True) file_url = db.Column(db.String(1024), nullable=False) thumb_url = db.Column(db.String(1024), nullable=True) slug = db.Column(db.String(256), nullable=False) def __init__(self, title): self.title = title self.slug = utils.slugify(title) def __repr__(self): return u"<Picture %s>" % (self.title)
class Note(db.Model): id = db.Column(db.String(), primary_key=True, default=uuid.uuid4().hex) title = db.Column(db.String(80), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) note = db.Column(db.Text, nullable=False) user_id = db.Column(db.String(), db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Note('{self.title}','{self.user_id}')" @classmethod def find_note_by_id(cls, note_id): return Note.query.filter(Note.id == note_id).first() @classmethod def find_notes_by_user(cls, user_id): return Note.query.filter(Note.user_id == user_id).all()
class Diary(db.Model): """ The Diary object """ __tablename__ = "diary" id = db.Column(db.Integer, primary_key=True) owner_id = db.Column(db.Integer, db.ForeignKey("user.id")) title = db.Column(db.String(1024), nullable=False, index=True) slug = db.Column(db.String(256), nullable=False, unique=True) created = db.Column(db.DateTime, default=datetime.utcnow) # relations posts = db.relationship("Post", lazy="dynamic") def __init__(self, title): self.title = title self.create_slug() def create_slug(self): self.slug = utils.slugify(self.title) counter = 0 new = self.slug while self.query.filter(Diary.slug == new).first() is not None: counter += 1 new = "{0}-{1}".format(self.slug, counter) self.slug = new def get_post(self, slug): return self.posts.filter(Post.slug == slug) def sorted_posts(self): return self.posts.order_by(Post.date.desc()) def last_post(self): return self.posts.order_by(Post.created.desc()).first() def __repr__(self): return u"<Diary %d : %s>" % (self.id, self.title)
class Post(db.Model): """ The Post object """ __tablename__ = "post" id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey("user.id")) diary_id = db.Column(db.Integer, db.ForeignKey("diary.id")) title = db.Column(db.String(1024), nullable=False, index=True) body = db.Column(db.Text, nullable=False) date = db.Column(db.Date, default=datetime.utcnow) slug = db.Column(db.String(256), nullable=False) created = db.Column(db.DateTime, default=datetime.utcnow) modified = db.Column(db.DateTime, default=datetime.utcnow) # relations pictures = db.relationship("Picture", lazy="dynamic") def __init__(self, diary_id, title): self.diary_id = diary_id self.title = title self.create_slug(diary_id) def create_slug(self, diary_id): self.slug = utils.slugify(self.title) counter = 0 new = self.slug while self.query.filter(Post.slug == new, Post.diary_id == diary_id).first() is not None: counter += 1 new = "{0}-{1}".format(self.slug, counter) self.slug = new def __repr__(self): return u"<Post: %s> %s" % (self.id, self.title)