From fd9d7f1aa9a744b96a5a0369b55340ac1964d025 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 31 May 2018 17:30:16 +0200 Subject: [PATCH] scape and stuff --- .DS_Store | Bin 8196 -> 8196 bytes app/__init__.py | 3 +++ app/models.py | 20 ++++------------- app/views.py | 40 ++++++++++++++++++++++----------- test.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 92 insertions(+), 29 deletions(-) create mode 100644 test.py diff --git a/.DS_Store b/.DS_Store index 23147fac7472315dcce7f2b5e38e9cc7b82f63aa..254df95007af944702c27497806a0982f7b905d7 100644 GIT binary patch delta 959 zcmd_mOGs2v9LMqhPfhQYGkNCebxf|0(lXJp#&Qg8h)78z%c<-EF{9VGDULO2k&ue^ zpjueRq@c2>Xc0v=w$!LLk`_d>C>KGGO$aS*5mbj6Xc_H0yYu_PfAJai5Bo=g$ups@ zi6dia7Y8iSG{vo8M&$E>^vu4#iprAQHS4|N0nfn|G0Sya6$`xB#w=QGU9FvMZ8lrN zolWKmLzG+F><2mTi5NSLj$pUhYitg78xe8S>eLJT9*RHp{?g24c`J&`yuRv9u?%jt z*&U3DIcpu5aN0cES&_C%hkE@OpJmuW>YDhC36ZgNadFW53El3gu8CRg#|q)J9<%qzg1olQc!s^pf7v2l_(a zXpw%Ji-K`n8XXr;T2xv9p2*;7Q~RfK}zaU(!~n@NmBhIDY_h{G@Ex+H!}|Y zL{Vc}mOFcOk*@P3MTvjPJUZjaQZv!K6-?(#BwV8Usu<%bQZ`X*Hp-eR4V74SmRn;v zOoW|Uzxr3YH#ARQX%R_ClUO#mCD%%n!iySgK^^MRfNj`~X6(a$9FSOtVB&BbtTzre zh#{Q9X~{N%OBlsvjA0zNa2t1U7Y|($?{OS&3e$LsXLycT#G=bxpAk*fzNd&qN8W!b H@DaZOPGjS3 delta 1010 zcmeIu%}Z2K7zXh7@uRu7gL>vez0N3iR8o_nW*U_Y)WH-#DN~~;QbxVO&@@_OhDnW> zi^z)NmSGz1qS8&6Mz!mUrdm`EE}}&ev}n;LuxL|lmJxN~IEa%nVe!6G>&#PtF`%9N22U!~vU_WR29d7g(U{RLNXP5;bGb6J|w(AZ#a zYVPLzSX67*!ok7NkX9EQ)S~(a`$2hcsJB1T5jvsiv)0UPS)NPJ&duLYy1A^fsy30q ztv0)ZF@4$E2&U)SygOO3vQg$isWU3xBy8N8!8~k}%%gJWp-`mHNE9FQOT16s9S-(r zVKb^=TP5Bi?-|mLMnnA(GjuS0+SV2;40y>{93)X5d8w9~sfz|^m?mhFX6Y_1(h@zQ zm$X9f=@WgSue3(r=sSQUq#zaP$c6$ZTquDLTi{0x8qtInv|<F`UMP8<%kflbFR#+`=5@@eoh&6wmM)Z}1Ku^)Y*^kW__aR#VYGNkZ0-J3_*2 z)lnsOHPaPiJXyHO2_?trDl2CyPZdOxajo3O7*7#~X1BUVbk_+jDdEa-DlCuH%Q7da z&{QHS43wDVAGyD}#px}r#su0A`YEW^!zQrYD1rwSs73(WQHOf$KpWc8flfiyjUajq zu86@kj1io~DS>tdXK@baaRFB`g=x&-I&KKL+lJgCmT(XE@c@tU%KT#zjB~-8wSonU evX~Z0W&8rC4&lTA diff --git a/app/__init__.py b/app/__init__.py index 981b170..4ba42bd 100755 --- a/app/__init__.py +++ b/app/__init__.py @@ -4,6 +4,8 @@ from marshmallow import Schema, fields, ValidationError, pre_load import os import click 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__)) @@ -17,6 +19,7 @@ app.config['SECRET_KEY'] = 'super secret key' #app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/mydatabase.db' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER app.config['SQLALCHEMY_DATABASE_URI'] = 'rqlite+pyrqlite://localhost:4001/' +#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'mydatabase.db') db = SQLAlchemy(app) app.config.from_object(__name__) diff --git a/app/models.py b/app/models.py index 64061c9..723817b 100755 --- a/app/models.py +++ b/app/models.py @@ -6,23 +6,17 @@ authors = db.Table('books_authors', 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)) + authors = db.relationship('Author', secondary=authors,cascade="delete", lazy='subquery', + backref=db.backref('books', lazy=True),passive_deletes=True) scapeX = 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): __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) diff --git a/app/views.py b/app/views.py index 0681b37..c05bb8d 100755 --- a/app/views.py +++ b/app/views.py @@ -92,7 +92,7 @@ def remove_book_by_id(id): book_to_edit = Book.query.filter_by(id=id).first() title = book_to_edit.title 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() flash("%s deleted from library" % (title)) return redirect(url_for('show_books')) @@ -106,25 +106,39 @@ def edit_book_by_id(id): if user_form.validate_on_submit(): # check if the post request has the file part 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 # save user to database #book = Book(title, author, filename, cover, file_extension) - db.session.commit() book = Book.query.filter_by(id=id).first() + book.title = title book.category = category - book.authors= [] - db.session.commit() - for author in 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) + + if(len(book.authors)==1): + book.authors[0].author_name = input_authors[0].get("author_name") + + #book.authors.clear() + for i, author in enumerate(input_authors): + if i > 0: + 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) + 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() flash("%s updated" % (title)) return redirect(url_for('show_books')) diff --git a/test.py b/test.py new file mode 100644 index 0000000..64c780b --- /dev/null +++ b/test.py @@ -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() + + \ No newline at end of file