2018-05-20 00:10:01 +02:00
|
|
|
from app import db
|
|
|
|
from marshmallow import Schema, fields, ValidationError, pre_load
|
|
|
|
|
2018-05-23 17:02:06 +02:00
|
|
|
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)
|
|
|
|
)
|
|
|
|
|
2018-05-31 18:04:05 +02:00
|
|
|
stacks = db.Table('books_stacks',
|
|
|
|
db.Column('book_id', db.Integer, db.ForeignKey('books.id'), primary_key=True),
|
|
|
|
db.Column('stack_id', db.Integer, db.ForeignKey('stacks.id'), primary_key=True)
|
|
|
|
)
|
|
|
|
|
2018-05-20 00:10:01 +02:00
|
|
|
class Book(db.Model):
|
2018-05-23 14:12:24 +02:00
|
|
|
__tablename__ = 'books'
|
2018-05-31 17:30:16 +02:00
|
|
|
|
2018-05-20 00:10:01 +02:00
|
|
|
id = db.Column(db.Integer, primary_key = True)
|
|
|
|
title = db.Column(db.String(255))
|
|
|
|
file = db.Column(db.String(255))
|
2018-05-23 14:12:24 +02:00
|
|
|
cover = db.Column(db.String(255))
|
|
|
|
fileformat = db.Column(db.String(255))
|
2018-05-27 22:11:35 +02:00
|
|
|
category = db.Column(db.String(255))
|
2018-05-31 20:06:13 +02:00
|
|
|
year_published = db.Column(db.Numeric(4,0))
|
|
|
|
html = db.Column(db.String(255))
|
2018-05-31 17:30:16 +02:00
|
|
|
authors = db.relationship('Author', secondary=authors,cascade="delete", lazy='subquery',
|
|
|
|
backref=db.backref('books', lazy=True),passive_deletes=True)
|
2018-05-31 18:04:05 +02:00
|
|
|
stacks = db.relationship('Stack', secondary=stacks, lazy='subquery',
|
|
|
|
backref=db.backref('books', lazy=True))
|
2018-05-29 16:18:00 +02:00
|
|
|
scapeX = db.Column(db.Numeric(10,2))
|
|
|
|
scapeY = db.Column(db.Numeric(10,2))
|
2018-05-20 00:10:01 +02:00
|
|
|
|
2018-05-31 20:06:13 +02:00
|
|
|
def __init__(self, title, file, cover, fileformat, category, year_published):
|
2018-05-20 00:10:01 +02:00
|
|
|
self.title = title
|
|
|
|
self.file = file
|
2018-05-23 14:12:24 +02:00
|
|
|
self.cover = cover
|
|
|
|
self.fileformat = fileformat
|
2018-05-27 22:11:35 +02:00
|
|
|
self.category = category
|
2018-05-31 20:06:13 +02:00
|
|
|
self.year_published = year_published
|
2018-05-29 16:18:00 +02:00
|
|
|
self.scapeX = 0
|
|
|
|
self.scapeY = 0
|
2018-05-20 00:10:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return '<Title %r>' % self.title
|
|
|
|
|
2018-05-23 14:12:24 +02:00
|
|
|
def get_id(self):
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
|
|
|
|
class Author(db.Model):
|
|
|
|
__tablename__ = 'authors'
|
2018-05-31 17:30:16 +02:00
|
|
|
|
2018-05-23 14:12:24 +02:00
|
|
|
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
|
2018-05-20 00:10:01 +02:00
|
|
|
|
2018-05-27 22:11:35 +02:00
|
|
|
|
|
|
|
class AuthorSchema(Schema):
|
|
|
|
id = fields.Int(dump_only=True)
|
|
|
|
author_name = fields.Str()
|
|
|
|
|
2018-05-20 00:10:01 +02:00
|
|
|
class BookSchema(Schema):
|
|
|
|
id = fields.Int(dump_only=True)
|
|
|
|
title = fields.Str()
|
|
|
|
file = fields.Str()
|
2018-05-27 22:11:35 +02:00
|
|
|
authors = fields.Nested(AuthorSchema, many=True)
|
2018-05-23 14:12:24 +02:00
|
|
|
cover = fields.Str()
|
|
|
|
fileformat = fields.Str()
|
2018-05-27 22:11:35 +02:00
|
|
|
category = fields.Str()
|
2018-05-31 18:04:05 +02:00
|
|
|
stack = fields.Str()
|
2018-05-27 22:11:35 +02:00
|
|
|
|
2018-05-31 18:04:05 +02:00
|
|
|
class Stack(db.Model):
|
|
|
|
__tablename__ = 'stacks'
|
|
|
|
id = db.Column(db.Integer, primary_key = True)
|
|
|
|
stack_name = db.Column(db.String(50))
|
|
|
|
stack_description = db.Column(db.String(500))
|
|
|
|
|
|
|
|
def __init__(self, stack_name, stack_description):
|
|
|
|
self.stack_name = stack_name
|
|
|
|
self.stack_description = stack_description
|
2018-05-27 22:11:35 +02:00
|
|
|
|
2018-05-31 18:04:05 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return '<Stack %r>' % self.stack
|
|
|
|
|
|
|
|
def get_id(self):
|
|
|
|
return self.id
|
|
|
|
|
|
|
|
class StackSchema(Schema):
|
|
|
|
id = fields.Int(dump_only=True)
|
|
|
|
stack_name = fields.Str()
|
|
|
|
stack_description = fields.Str()
|
2018-05-20 00:10:01 +02:00
|
|
|
|
|
|
|
def must_not_be_blank(data):
|
|
|
|
if not data:
|
2018-05-23 11:16:25 +02:00
|
|
|
raise ValidationError('You forgot to write stuff.')
|