You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
XPPL/app/models.py

98 lines
2.9 KiB
Python

7 years ago
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)
)
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)
)
7 years ago
class Book(db.Model):
__tablename__ = 'books'
7 years ago
7 years ago
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))
7 years ago
category = db.Column(db.String(255))
7 years ago
year_published = db.Column(db.Numeric(4,0))
html = db.Column(db.String(255))
7 years ago
authors = db.relationship('Author', secondary=authors,cascade="delete", lazy='subquery',
backref=db.backref('books', lazy=True),passive_deletes=True)
stacks = db.relationship('Stack', secondary=stacks, lazy='subquery',
backref=db.backref('books', lazy=True))
7 years ago
scapeX = db.Column(db.Numeric(10,2))
scapeY = db.Column(db.Numeric(10,2))
7 years ago
7 years ago
def __init__(self, title, file, cover, fileformat, category, year_published):
7 years ago
self.title = title
self.file = file
self.cover = cover
self.fileformat = fileformat
7 years ago
self.category = category
7 years ago
self.year_published = year_published
7 years ago
self.scapeX = 0
self.scapeY = 0
7 years ago
def __repr__(self):
return '<Title %r>' % self.title
def get_id(self):
return self.id
class Author(db.Model):
__tablename__ = 'authors'
7 years ago
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
7 years ago
7 years ago
class AuthorSchema(Schema):
id = fields.Int(dump_only=True)
author_name = fields.Str()
7 years ago
class BookSchema(Schema):
id = fields.Int(dump_only=True)
title = fields.Str()
file = fields.Str()
7 years ago
authors = fields.Nested(AuthorSchema, many=True)
cover = fields.Str()
fileformat = fields.Str()
7 years ago
category = fields.Str()
stack = fields.Str()
7 years ago
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
7 years ago
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()
7 years ago
def must_not_be_blank(data):
if not data:
7 years ago
raise ValidationError('You forgot to write stuff.')