76 lines
2.2 KiB
Python
Executable File
76 lines
2.2 KiB
Python
Executable File
from app import db
|
|
from marshmallow import Schema, fields, ValidationError, pre_load
|
|
|
|
authors = db.Table('books_authors',
|
|
db.Column('book_id', db.Integer, db.ForeignKey('books.id'), primary_key=True),
|
|
db.Column('author_id', db.Integer, db.ForeignKey('authors.id'), primary_key=True)
|
|
)
|
|
|
|
tags = db.Table('books_tags',
|
|
db.Column('book_id', db.Integer, db.ForeignKey('books.id'), primary_key=True),
|
|
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id'), primary_key=True)
|
|
)
|
|
|
|
class Book(db.Model):
|
|
__tablename__ = 'books'
|
|
id = db.Column(db.Integer, primary_key = True)
|
|
title = db.Column(db.String(255))
|
|
file = db.Column(db.String(255))
|
|
cover = db.Column(db.String(255))
|
|
fileformat = db.Column(db.String(255))
|
|
category = db.Column(db.String(255))
|
|
authors = db.relationship('Author', secondary=authors, lazy='subquery',
|
|
backref=db.backref('books', lazy=True))
|
|
tags = db.relationship('Tag', secondary=tags, lazy='subquery',
|
|
backref=db.backref('books', lazy=True))
|
|
|
|
def __init__(self, title, file, cover, fileformat, category):
|
|
self.title = title
|
|
self.file = file
|
|
self.cover = cover
|
|
self.fileformat = fileformat
|
|
self.category = category
|
|
|
|
|
|
def __repr__(self):
|
|
return '<Title %r>' % self.title
|
|
|
|
def get_id(self):
|
|
return self.id
|
|
|
|
|
|
class Author(db.Model):
|
|
__tablename__ = 'authors'
|
|
id = db.Column(db.Integer(), primary_key=True)
|
|
author_name = db.Column(db.String(50))
|
|
|
|
def __init__(self, author_name):
|
|
self.author_name = author_name
|
|
|
|
class Tag(db.Model):
|
|
__tablename__ = 'tags'
|
|
id = db.Column(db.Integer(), primary_key=True)
|
|
tag = db.Column(db.String(50))
|
|
|
|
def __init__(self, tag):
|
|
self.tag = tag
|
|
|
|
class AuthorSchema(Schema):
|
|
id = fields.Int(dump_only=True)
|
|
author_name = fields.Str()
|
|
|
|
class BookSchema(Schema):
|
|
id = fields.Int(dump_only=True)
|
|
title = fields.Str()
|
|
file = fields.Str()
|
|
authors = fields.Nested(AuthorSchema, many=True)
|
|
cover = fields.Str()
|
|
fileformat = fields.Str()
|
|
category = fields.Str()
|
|
|
|
|
|
|
|
def must_not_be_blank(data):
|
|
if not data:
|
|
raise ValidationError('You forgot to write stuff.')
|