xppl/app/models.py

52 lines
1.3 KiB
Python
Raw Normal View History

2018-05-20 00:10:01 +02:00
from app import db
from marshmallow import Schema, fields, ValidationError, pre_load
class Book(db.Model):
2018-05-23 14:12:24 +02:00
__tablename__ = 'books'
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))
author = db.relationship('Author')
2018-05-23 11:16:25 +02:00
tag = db.Column(db.String(255))
2018-05-20 00:10:01 +02:00
2018-05-23 14:29:56 +02:00
def __init__(self, title, file, cover, fileformat, tag):
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-23 11:16:25 +02:00
self.tag = tag
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'
id = db.Column(db.Integer(), primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('books.id'))
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
class BookSchema(Schema):
id = fields.Int(dump_only=True)
title = fields.Str()
author = fields.Str()
file = fields.Str()
2018-05-23 14:12:24 +02:00
cover = fields.Str()
fileformat = fields.Str()
2018-05-23 11:16:25 +02:00
tag = 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.')