scape and stuff
This commit is contained in:
parent
c4583482b5
commit
fd9d7f1aa9
@ -4,6 +4,8 @@ from marshmallow import Schema, fields, ValidationError, pre_load
|
|||||||
import os
|
import os
|
||||||
import click
|
import click
|
||||||
from werkzeug.utils import secure_filename
|
from werkzeug.utils import secure_filename
|
||||||
|
from sqlalchemy.dialects import registry
|
||||||
|
registry.register("rqlite.pyrqlite", "sqlalchemy_rqlite.pyrqlite", "dialect")
|
||||||
|
|
||||||
basedir = os.path.abspath(os.path.dirname(__file__))
|
basedir = os.path.abspath(os.path.dirname(__file__))
|
||||||
|
|
||||||
@ -17,6 +19,7 @@ app.config['SECRET_KEY'] = 'super secret key'
|
|||||||
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/mydatabase.db'
|
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/mydatabase.db'
|
||||||
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
||||||
app.config['SQLALCHEMY_DATABASE_URI'] = 'rqlite+pyrqlite://localhost:4001/'
|
app.config['SQLALCHEMY_DATABASE_URI'] = 'rqlite+pyrqlite://localhost:4001/'
|
||||||
|
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'mydatabase.db')
|
||||||
db = SQLAlchemy(app)
|
db = SQLAlchemy(app)
|
||||||
|
|
||||||
app.config.from_object(__name__)
|
app.config.from_object(__name__)
|
||||||
|
@ -6,23 +6,17 @@ authors = db.Table('books_authors',
|
|||||||
db.Column('author_id', db.Integer, db.ForeignKey('authors.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):
|
class Book(db.Model):
|
||||||
__tablename__ = 'books'
|
__tablename__ = 'books'
|
||||||
|
|
||||||
id = db.Column(db.Integer, primary_key = True)
|
id = db.Column(db.Integer, primary_key = True)
|
||||||
title = db.Column(db.String(255))
|
title = db.Column(db.String(255))
|
||||||
file = db.Column(db.String(255))
|
file = db.Column(db.String(255))
|
||||||
cover = db.Column(db.String(255))
|
cover = db.Column(db.String(255))
|
||||||
fileformat = db.Column(db.String(255))
|
fileformat = db.Column(db.String(255))
|
||||||
category = db.Column(db.String(255))
|
category = db.Column(db.String(255))
|
||||||
authors = db.relationship('Author', secondary=authors, lazy='subquery',
|
authors = db.relationship('Author', secondary=authors,cascade="delete", lazy='subquery',
|
||||||
backref=db.backref('books', lazy=True))
|
backref=db.backref('books', lazy=True),passive_deletes=True)
|
||||||
tags = db.relationship('Tag', secondary=tags, lazy='subquery',
|
|
||||||
backref=db.backref('books', lazy=True))
|
|
||||||
scapeX = db.Column(db.Numeric(10,2))
|
scapeX = db.Column(db.Numeric(10,2))
|
||||||
scapeY = db.Column(db.Numeric(10,2))
|
scapeY = db.Column(db.Numeric(10,2))
|
||||||
|
|
||||||
@ -45,19 +39,13 @@ class Book(db.Model):
|
|||||||
|
|
||||||
class Author(db.Model):
|
class Author(db.Model):
|
||||||
__tablename__ = 'authors'
|
__tablename__ = 'authors'
|
||||||
|
|
||||||
id = db.Column(db.Integer(), primary_key=True)
|
id = db.Column(db.Integer(), primary_key=True)
|
||||||
author_name = db.Column(db.String(50))
|
author_name = db.Column(db.String(50))
|
||||||
|
|
||||||
def __init__(self, author_name):
|
def __init__(self, author_name):
|
||||||
self.author_name = 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):
|
class AuthorSchema(Schema):
|
||||||
id = fields.Int(dump_only=True)
|
id = fields.Int(dump_only=True)
|
||||||
|
40
app/views.py
40
app/views.py
@ -92,7 +92,7 @@ def remove_book_by_id(id):
|
|||||||
book_to_edit = Book.query.filter_by(id=id).first()
|
book_to_edit = Book.query.filter_by(id=id).first()
|
||||||
title = book_to_edit.title
|
title = book_to_edit.title
|
||||||
Book.query.filter_by(id=id).delete()
|
Book.query.filter_by(id=id).delete()
|
||||||
#author_table = Author.query.filter_by(books_id=book_to_edit.id).delete()
|
author_table = Author.query.filter_by(book_id=book_to_edit.id).delete()
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash("%s deleted from library" % (title))
|
flash("%s deleted from library" % (title))
|
||||||
return redirect(url_for('show_books'))
|
return redirect(url_for('show_books'))
|
||||||
@ -106,25 +106,39 @@ def edit_book_by_id(id):
|
|||||||
if user_form.validate_on_submit():
|
if user_form.validate_on_submit():
|
||||||
# check if the post request has the file part
|
# check if the post request has the file part
|
||||||
title = user_form.title.data # You could also have used request.form['name']
|
title = user_form.title.data # You could also have used request.form['name']
|
||||||
authors = user_form.author.data # You could also have used request.form['email']
|
input_authors = user_form.author.data # You could also have used request.form['email']
|
||||||
category = user_form.category.data
|
category = user_form.category.data
|
||||||
# save user to database
|
# save user to database
|
||||||
#book = Book(title, author, filename, cover, file_extension)
|
#book = Book(title, author, filename, cover, file_extension)
|
||||||
db.session.commit()
|
|
||||||
|
|
||||||
book = Book.query.filter_by(id=id).first()
|
book = Book.query.filter_by(id=id).first()
|
||||||
|
|
||||||
book.title = title
|
book.title = title
|
||||||
book.category = category
|
book.category = category
|
||||||
book.authors= []
|
|
||||||
db.session.commit()
|
if(len(book.authors)==1):
|
||||||
for author in authors:
|
book.authors[0].author_name = input_authors[0].get("author_name")
|
||||||
author_name = author.get("author_name")
|
|
||||||
if author_name:
|
#book.authors.clear()
|
||||||
a = db.session.query(Author).filter_by(author_name=author_name).first()
|
for i, author in enumerate(input_authors):
|
||||||
if a == None:
|
if i > 0:
|
||||||
a = Author(author_name=author_name)
|
author_name = author.get("author_name")
|
||||||
db.session.add(a)
|
if author_name:
|
||||||
book.authors.append(a)
|
a = db.session.query(Author).filter_by(author_name=author_name).first()
|
||||||
|
if a == None:
|
||||||
|
a = Author(author_name=author_name)
|
||||||
|
db.session.add(a)
|
||||||
|
book.authors.append(a)
|
||||||
|
else:
|
||||||
|
book.authors.clear()
|
||||||
|
for i, author in enumerate(input_authors):
|
||||||
|
author_name = author.get("author_name")
|
||||||
|
if author_name:
|
||||||
|
a = db.session.query(Author).filter_by(author_name=author_name).first()
|
||||||
|
if a == None:
|
||||||
|
a = Author(author_name=author_name)
|
||||||
|
db.session.add(a)
|
||||||
|
book.authors.append(a)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
flash("%s updated" % (title))
|
flash("%s updated" % (title))
|
||||||
return redirect(url_for('show_books'))
|
return redirect(url_for('show_books'))
|
||||||
|
58
test.py
Normal file
58
test.py
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.orm import sessionmaker, relationship
|
||||||
|
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
|
||||||
|
teachers_lessons = Table(
|
||||||
|
"teachers_lessons",
|
||||||
|
Base.metadata,
|
||||||
|
Column("fk_teacher", Integer, ForeignKey("teachers.id")),
|
||||||
|
Column("fk_lesson", Integer, ForeignKey("lessons.id")),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Teacher(Base):
|
||||||
|
__tablename__ = "teachers"
|
||||||
|
|
||||||
|
id = Column("id", Integer, Sequence("teachers_id_seq"), primary_key=True)
|
||||||
|
name = Column("name", String(50), nullable=False)
|
||||||
|
|
||||||
|
lessons = relationship(
|
||||||
|
"Lesson",
|
||||||
|
backref="teachers",
|
||||||
|
secondary=teachers_lessons
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class Lesson(Base):
|
||||||
|
__tablename__ = "lessons"
|
||||||
|
|
||||||
|
id = Column("id", Integer, Sequence("lessons_id_seq"), primary_key=True)
|
||||||
|
name = Column("name", String(50), nullable=False)
|
||||||
|
|
||||||
|
|
||||||
|
engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=True)
|
||||||
|
Base.metadata.create_all(engine)
|
||||||
|
Session = sessionmaker(bind=engine)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
s = Session()
|
||||||
|
|
||||||
|
t1 = Teacher(name="NEWNEW")
|
||||||
|
t1.lessons = [
|
||||||
|
Lesson(name="fpro"),
|
||||||
|
Lesson(name="math")
|
||||||
|
]
|
||||||
|
s.add(t1)
|
||||||
|
s.commit()
|
||||||
|
|
||||||
|
t2 = s.query(Teacher).filter_by(name='NEWNEW').first()
|
||||||
|
t2.lessons.clear()
|
||||||
|
s.commit()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user