class User(db.Model): __tablename__ = 'users' id = db.Column('id', db.Integer, primary_key=True) username = db.Column('username', db.String(20), unique=True, index=True, nullable=False) password = db.Column('password', db.String(30), nullable=False) books = db.relationship('Book', secondary=userBooks, backref='users') def __init__(self, username, password): self.username = username self.password = password def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return self.id def __repr__(self): return '<User {}>'.format(self.username) def __eq__(self, user): return self.name == user.name
class OfficeModel(db.Model): __tablename__ = 'offices' id = db.Column('id', db.Integer, primary_key=True) name = db.Column('name', db.String(20), nullable=False) def __init__(self, name): self.name = name def __repr__(self): return "<Office: {0}>".format(self.name)
class CategoryModel(db.Model): __tablename__ = 'categories' id = db.Column('id', db.Integer, primary_key=True) name = db.Column('name', db.String(20), nullable=False) def __init__(self, name): self.name = name def __repr__(self): return "<Category ID: {0}, Name: {1}>".format(self.id, self.name)
class UserModel(UserMixin, db.Model): __tablename__ = 'users' id = db.Column('id', db.Integer, primary_key=True, unique=True, autoincrement=True) login = db.Column('login', db.String(30), nullable=False, unique=True) password_hash = db.Column(db.String(100), nullable=False) email = db.Column('email', db.String(60), nullable=False, unique=True) first_name = db.Column('first_name', db.String(30)) last_name = db.Column('last_name', db.String(30)) office = db.Column('office', db.String(20), nullable=False) phone_number = db.Column('phone_number', db.String(20)) limit = db.Column('limit', db.Integer, default=0) points = db.Column('points', db.Integer, default=0) # office_id = db.Column('office_id', db.Integer, db.ForeignKey('offices.id')) def __init__(self, login, password, email, first_name, last_name, office, phone_number): self.login = login self.password = password self.email = email self.first_name = first_name self.last_name = last_name self.office = office # self.office = office.name self.phone_number = phone_number @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify_password(self, password): return check_password_hash(self.password_hash, password) def is_owner(self, book_user_id): return self.id == book_user_id def __repr__(self): return "<User ID: {0}, Login: {1}, Office: {2}, Email: {3}, Password: {4}>".format( self.id, self.login, self.office, self.email, self.password_hash)
class Book(db.Model): __tablename__ = 'books' id = db.Column('id', db.Integer, primary_key=True) title = db.Column('title', db.String, nullable=False) author = db.Column('author', db.String, nullable=False) publisher = db.Column('publisher', db.String, nullable=False) category = db.Column('category', db.String, nullable=False) def __init__(self, title, author, publisher, category): self.title = title self.author = author self.publisher = publisher self.category = category
class RequestModel(db.Model): __tablename__ = 'requests' id = db.Column('id', db.Integer, primary_key=True) request_date = db.Column('request_date', db.DateTime, nullable=False) accept_date = db.Column('accept_date', db.DateTime, default=None) notification_counter = db.Column('notification_counter', db.Integer, default=0) book_id = db.Column('book_id', db.Integer, db.ForeignKey('books.id')) req_user_id = db.Column('req_user_id', db.Integer, db.ForeignKey('users.id')) owner_user_id = db.Column('owner_user_id', db.Integer, db.ForeignKey('users.id')) def __init__(self, book_id, req_user_id, owner_user_id, request_date=datetime.datetime.now(), accept_date=None): self.book_id = book_id self.req_user_id = req_user_id self.owner_user_id = owner_user_id self.request_date = request_date self.accept_date = accept_date def __repr__(self): return "<ID: {0}, REQ_DATE: {1}, ACPT_DATE: {2}, BOOK: {3}, USER: {4}, OWNER: {5}>".format( self.id, self.request_date, self.accept_date, self.book_id, self.req_user_id, self.owner_user_id)
class BookModel(db.Model): __tablename__ = 'books' id = db.Column('id', db.Integer, primary_key=True) title = db.Column('title', db.String(60), nullable=False) author = db.Column('author', db.String(80), nullable=False) publisher = db.Column('publisher', db.String(40), nullable=False) visible = db.Column('visible', db.Boolean, default=True) category_id = db.Column('category_id', db.Integer, db.ForeignKey('categories.id')) user_id = db.Column('user_id', db.Integer, db.ForeignKey('users.id')) def __init__(self, title, author, publisher): self.title = title self.author = author self.publisher = publisher def __repr__(self): return "<Book ID: {0}, Title: {1}, Author: {2}>".format( self.id, self.title, self.author)
class RequestHistoryModel(db.Model): __tablename__ = 'request_history' id = db.Column('id', db.Integer, primary_key=True) request_date = db.Column('request_date', db.DateTime, nullable=False) accept_date = db.Column('accept_date', db.DateTime, default=None) notification_counter = db.Column('notification_counter', db.Integer, default=0) book_title = db.Column('book_title', db.String(60)) book_author = db.Column('book_author', db.String(80)) book_publisher = db.Column('book_publisher', db.String(40)) book_category = db.Column('book_category', db.String(20)) req_user_login = db.Column('req_user_login', db.String(30)) req_user_email = db.Column('req_user_email', db.String(60)) req_user_first_name = db.Column('req_user_first_name', db.String(30)) req_user_last_name = db.Column('req_user_last_name', db.String(30)) req_user_office = db.Column('req_user_office', db.String(20)) req_user_phone_number = db.Column('req_user_phone_number', db.String(20)) owner_user_login = db.Column('owner_user_login', db.String(30)) owner_user_email = db.Column('owner_user_email', db.String(60)) owner_user_first_name = db.Column('owner_user_first_name', db.String(30)) owner_user_last_name = db.Column('owner_user_last_name', db.String(30)) owner_user_office = db.Column('owner_user_office', db.String(20)) owner_user_phone_number = db.Column('owner_user_phone_number', db.String(20)) def __init__(self, book_id, req_user_id, owner_user_id, request_date=None, accept_date=None): book = BookModel.query.get(book_id) category = CategoryModel.query.get(book.category_id) self.book_title = book.title self.book_author = book.author self.book_publisher = book.publisher self.book_category = category.name req_user = UserModel.query.get(req_user_id) self.req_user_login = req_user.login self.req_user_email = req_user.email self.req_user_first_name = req_user.first_name self.req_user_last_name = req_user.last_name self.req_user_office = req_user.office self.req_user_phone_number = req_user.phone_number owner_user = UserModel.query.get(owner_user_id) self.owner_user_login = owner_user.login self.owner_user_email = owner_user.email self.owner_user_first_name = owner_user.first_name self.owner_user_last_name = owner_user.last_name self.owner_user_office = owner_user.office self.owner_user_phone_number = owner_user.phone_number self.request_date = request_date self.accept_date = accept_date def __repr__(self): return "<ID: {0}, REQ_DATE: {1}, ACPT_DATE: {2}, BOOK: {3}, USER: {4}, OWNER: {5}>".format( self.id, self.request_date, self.accept_date, self.book_title, self.req_user_login, self.owner_user_login)
from bookcrossing import db userBooks = db.Table('userBooks', db.Model.metadata, db.Column('user_id', db.ForeignKey('users.id'), nullable=False), db.Column('book_id', db.ForeignKey('books.id'), nullable=False) ) class User(db.Model): __tablename__ = 'users' id = db.Column('id', db.Integer, primary_key=True) username = db.Column('username', db.String(20), unique=True, index=True, nullable=False) password = db.Column('password', db.String(30), nullable=False) books = db.relationship('Book', secondary=userBooks, backref='users') def __init__(self, username, password): self.username = username self.password = password def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return self.id