class Borrowed(db.Model): ''' This model describes the relationship between a book and the user who borrowed it ''' __tablename__ = 'borrows' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id')) book_id = db.Column(db.Integer, db.ForeignKey('books.id')) _from = db.Column(db.Date, nullable=False) to = db.Column(db.Date, nullable=False, index=True) @property def book(): book = db.session.query(Book).get(self.book_id) return book @property def user(): user = db.session.query(User).get(self.user_id) return user def __repr__(self): book = db.session.query(Book).get(self.book_id) user = db.session.query(User).get(self.user_id) return f'<Book [{book.title}] borrowed by user [{user.name}]>' def to_json(self): result = {} result['id'] = self.id result['user'] = self.user.to_json() result['book'] = self.book.to_json() result['from'] = self._from.isoformat() result['to'] = self.to.isoformat() return result
class User(db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(255), nullable=False, unique=True, index=True) email = db.Column(db.String(255), nullable=False, unique=True) password = db.Column(db.String(255), nullable=False) created_date = db.Column(db.DateTime, default=datetime.utcnow) @staticmethod def generate_hashed_password(password: str) -> str: return generate_password_hash(password) def generate_jwt(self) -> bytes: payload = { 'user_id': self.id, 'exp': datetime.utcnow() + timedelta( minutes=current_app.config.get('JWT_EXPIRED_MINUTES', 30)) } return jwt.encode(payload, current_app.config.get('SECRET_KEY')) def is_password_valid(self, password: str) -> bool: return check_password_hash(self.password, password)
class Book(db.Model, Serializer): # See http://flask-sqlalchemy.pocoo.org/2.0/models/#simple-example # for details on the column types. id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), nullable=False) author_id = db.Column(db.Integer, db.ForeignKey('author.id')) def __repr__(self): return f'Book title:{self.name} author: {self.author.name}'
class Author(db.Model, Serializer): # See http://flask-sqlalchemy.pocoo.org/2.0/models/#simple-example # for details on the column types. id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), nullable=False) country = db.Column(db.String(80)) year_of_birth = db.Column(db.Integer) def __repr__(self): return f'Author name:{self.name} country:{self.country} year: {self.year_of_birth}'
class Author(db.Model): ''' This model describes an author ''' __tablename__ = 'authors' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) email = db.Column(db.String, nullable=False, index=True) books = db.relationship('Book', backref='author') def __repr__(self): return '<Author {} {}>'.format(self.name, self.email) def to_json(self): result = {} result['id'] = self.id result['name'] = self.name result['email'] = self.email # result['books'] = [book.to_json() for book in self.books] return result
class Author(db.Model): __tablename__ = 'authors' id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(50), nullable=False) last_name = db.Column(db.String(50), nullable=False) birth_date = db.Column(db.Date, nullable=False) books = db.relationship('Book', back_populates='author', cascade='all, delete-orphan') def __repr__(self): return f'<{self.__class__.__name__}>: {self.first_name} {self.last_name}' @staticmethod def additional_validation(param: str, value: str) -> date: if param == 'birth_date': try: value = datetime.strptime(value, '%d-%m-%Y').date() except ValueError: value = None return value
class Book(db.Model): ''' This model describes a book. ''' __tablename__ = 'books' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String, nullable=False) edition = db.Column(db.String, nullable=False, index=True) year = db.Column(db.String, nullable=False, index=True) price = db.Column(db.String) isbn = db.Column(db.String, nullable=False, unique=True) pages = db.Column(db.Integer) copies = db.Column(db.Integer, nullable=False) description = db.Column(db.String) author_id = db.Column(db.Integer, db.ForeignKey('authors.id')) # Current users with the book borrowed = db.relationship('Borrowed', backref='book') def __repr__(self): return '<Book {} {}>'.format(self.title, self.isbn) def to_json(self): result = {} result['id'] = self.id result['title'] = self.title result['edition'] = self.edition result['year'] = self.year result['price'] = self.price result['isbn'] = self.isbn result['pages'] = self.pages result['copies'] = self.copies result['description'] = self.description result['author'] = {} author = self.author and self.author.to_json() or None result['author'] = author return result
class Book(db.Model): __tablename__ = 'books' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(50), nullable=False) isbn = db.Column(db.BigInteger, nullable=False, unique=True) number_of_pages = db.Column(db.Integer, nullable=False) description = db.Column(db.Text) author_id = db.Column(db.Integer, db.ForeignKey('authors.id'), nullable=False) author = db.relationship('Author', back_populates='books') def __repr__(self): return f'{self.title} - {self.author.first_name} {self.author.last_name}' @staticmethod def additional_validation(param: str, value: str) -> str: return value
class User(UserMixin, db.Model): ''' This model describes a user. ''' __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) phone = db.Column(db.String, nullable=False, index=True, unique=True) username = db.Column(db.String, nullable=False, unique=True, index=True) email = db.Column(db.String, nullable=False, index=True, unique=True) password_hash = db.Column(db.String(128)) # role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) role = db.Column(db.String, nullable=False, default='member') borrowed = db.relationship('Borrowed', backref='user') @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 __repr__(self): return '<User {}, {}>'.format(self.name, self.email) def to_json(self): result = {} result['id'] = self.id result['name'] = self.name result['phone'] = self.phone result['username'] = self.username result['email'] = self.email result['role'] = self.role return result