class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) email = db.Column(db.String(120), index=True, unique=True) password = db.Column(db.String(128)) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') posts = db.relationship('Post', backref='author', lazy='dynamic') comment = db.relationship('Comment', backref='commentator', lazy='dynamic') def get_reset_token(self, expires_sec=1800): # IN how many sec, token expires s = Serializer(current_app.config['SECRET_KEY'], expires_sec) return s.dumps({ 'user_id': self.id }).decode('UTF-8') # creates token, user_id is payload @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 'User {}'.format(self.username)
class User(db.Model, UserMixin): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) profile_image = db.Column(db.String(64), nullable=False, default='default_profile.jpg') #what is index - speed up search in tables email = db.Column(db.String(64), unique=True, index=True) username = db.Column(db.String(64), unique=True, index=True) password_hash = db.Column(db.String(128)) posts = db.relationship('BlogPost', backref='author', lazy=True) def __init__(self, email, username, password): self.email = email self.username = username self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) def __repr__(self): return f"Username {self.username}, email {self.email}"
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50), unique=True, nullable=True) password = db.Column(db.Text, nullable=False) createdtime = db.Column(db.TIMESTAMP, nullable=False, default=datetime.datetime.now) posts = db.relationship('Post', backref='user')
class Comment(db.Model): c_id = db.Column(db.Integer, primary_key=True) c_body = db.Column(db.Text, nullable=False) c_timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) c_author_id = db.Column(db.Integer, db.ForeignKey('user.id')) comment_id = db.Column(db.Integer, db.ForeignKey('post.id')) def __repr__(self): return 'Comment{}'.format(self.comment_id)
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) body = db.Column(db.Text, nullable=False) title = db.Column(db.String(140)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) def __repr__(self): return 'Post {}'.format(self.body)
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.Text, nullable=False) date = db.Column(db.DateTime, default=datetime.utcnow) content = db.Column(db.String(300)) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) @staticmethod def newest(num): return Bookmark.query.order_by(desc(Bookmark.date)).limit(num)
class Blog(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) admin = db.Column(db.Integer, db.ForeignKey('author.id')) def __init__(self, name, admin): self.name = name self.admin = admin def __repr__(self): return '<Blog %r>' % self.name
class Post(db.Model): 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) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}','{self.date_posted}')"
class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(100), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(50), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}','{self.email}','{self.image_file}')"
class BlogPost(db.Model): users = db.relationship(User) id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False) date = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) title = db.Column(db.String(140), nullable=False) text = db.Column(db.Text, nullable=False) def __init(self, title, text, user_id): self.title = title self.text = text self.user_id = user_id def __repr__(self): return f"Post id: {self.id}, date: {self.date}, {self.title}"
class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) posts = db.relationship('Post', backref='user', lazy='dynamic') password_hash = db.Column(db.String) @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def check_password(self, password): return check_password_hash(self.password_hash, password) @staticmethod def get_by_username(username): return User.query.filter_by(username=username).first() def __repr__(self): return "<User '{}'>".format(self.username)
class Author(db.Model): id = db.Column(db.Integer, primary_key=True) fullname = db.Column(db.String(settings.AUTHOR_FULLNAME_STRING_LENGHT)) email = db.Column(db.String(settings.AUTHOR_EMAIL_STRING_LENGHT), unique=True) username = db.Column(db.String(settings.AUTHOR_USERNAME_STRING_LENGHT), unique=True) password = db.Column(db.String(settings.AUTHOR_PASSWORD_STRING_LENGHT)) is_author = db.Column(db.Boolean) def __init__(self, fullname, email, username, password, is_author=False): self.fullname = fullname self.email = email self.username = username self.password = password self.is_author = is_author def __repr__(self): return '<Author %r>' % self.username
class Post(db.Model): __tablename__ = 'posts' id = db.Column(db.Integer, primary_key=True, autoincrement=True) title = db.Column(db.String(50), nullable=False, unique=True) content = db.Column(db.Text, nullable=False) committime = db.Column(db.TIMESTAMP, nullable=False, default=datetime.datetime.now) author_id = db.Column(db.Integer, db.ForeignKey('users.id')) # def init_db(): # try: # con = sqlite3.connect('database.db') # cur = con.cursor() # # # #初始化表格,如果存在这些表则删除 # # sql = "drop table if exists users" # # cur.execute(sql) # # # sql = "drop table if exists posts" # # cur.execute(sql) # # # sql = ''' # # create table users( # # id integer primary key autoincrement, # # name varchar(16) unique not null , # # password text not null # # ) # # ''' # # cur.execute(sql) # # # # sql = ''' # # create table posts( # # id integer primary key autoincrement, # # title varchar(50) unique not null , # # content text not null , # # author_id integer not null, # # committime timestamp not null default current_timestamp, # # foreign key (author_id) references users (id) # # ) # # ''' # # cur.execute(sql) # # cur.close() # con.close() # except Exception as e: # print(e) # 如果没有dict_factory这个函数,那么将返回一个list的值,使用起来非常不方便 # def dict_factory(cursor, row): # d = {} # for index, col in enumerate(cursor.description): # d[col[0]] = row[index] # return d # # def search_user_name(username): # try: # con = sqlite3.connect('database.db') # con.row_factory = dict_factory # cur = con.cursor() # sql = " select * from users where name=? " # user = cur.execute(sql, (username,)).fetchone() #这里u返回值是一个tunple(元组) # cur.close() # con.close() # if user: # return user # except Exception as e: # print(e) # # def search_user_id(id): # try: # con = sqlite3.connect('database.db') # con.row_factory = dict_factory # cur = con.cursor() # sql = " select * from users where id=? " # user = cur.execute(sql, (id,)).fetchone() #这里u返回值是一个tunple(元组) # cur.close() # con.close() # if user: # return user # except Exception as e: # print(e) # def show_posts(): # try: # con = sqlite3.connect('database.db') # con.row_factory = dict_factory # 指定工厂方法,官方给出的将list返回为字典的方法 # cur = con.cursor() # sql = " select * from posts " # u = cur.execute(sql).fetchall() # 如果不进行转换,那么这里的u返回值是一个list;现在返回的是一个字典列表 # # # 可以通过打印来验证一下 # # print(u) # # cur.close() # con.close() # # if u: # # print(u) # return u #将返回一个字典列表 # # except Exception as e: # print(e) # def add_user(name, password): # try: # con = sqlite3.connect('database.db') # cur = con.cursor() # sql = ''' # insert into users(name, password) # values # (?, ?) # ''' # cur.execute(sql, (name, generate_password_hash(password), )) # con.commit() # cur.close() # con.close() # except Exception as e: # con.rollback() # print(e) # def add_post(title, content, author_id): # try: # con = sqlite3.connect('database.db') # cur = con.cursor() # sql = ''' # insert into posts ( title, content, author_id ) # values # (?, ?, ?) # ''' # cur.execute(sql, (title, content, author_id, )) # con.commit() # cur.close() # con.close() # except Exception as e: # con.rollback() # print(e) # init_db() # db.drop_all() # db.create_all() # print(show_posts()) # add_post('test_title', 'test_content', 1)