From d029e57c724d204f68876b33af7999b6b6099c5d Mon Sep 17 00:00:00 2001 From: Alex Date: Fri, 1 Jun 2018 20:26:37 +0200 Subject: [PATCH] rqlite update --- app/__init__.py | 2 +- app/models.py | 35 ++++++++------ app/templates/base.html | 18 ++++++++ app/templates/home.html | 2 +- app/views.py | 39 ++++++---------- test.py | 100 ++++++++++++++++++++++++---------------- test2.py | 49 ++++++++++++++++++++ 7 files changed, 162 insertions(+), 83 deletions(-) create mode 100644 test2.py diff --git a/app/__init__.py b/app/__init__.py index 4ba42bd..420b1b2 100755 --- a/app/__init__.py +++ b/app/__init__.py @@ -11,7 +11,7 @@ basedir = os.path.abspath(os.path.dirname(__file__)) UPLOAD_FOLDER = os.path.join(basedir, 'uploads') UPLOAD_FOLDER_COVER = os.path.join(basedir, 'uploads/cover') -ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) +#ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) app = Flask(__name__) diff --git a/app/models.py b/app/models.py index c0235a7..1aaf5b0 100755 --- a/app/models.py +++ b/app/models.py @@ -56,21 +56,6 @@ class Author(db.Model): def __init__(self, author_name): self.author_name = author_name - -class AuthorSchema(Schema): - id = fields.Int(dump_only=True) - author_name = fields.Str() - -class BookSchema(Schema): - id = fields.Int(dump_only=True) - title = fields.Str() - file = fields.Str() - authors = fields.Nested(AuthorSchema, many=True) - cover = fields.Str() - fileformat = fields.Str() - category = fields.Str() - stack = fields.Str() - class Stack(db.Model): __tablename__ = 'stacks' id = db.Column(db.Integer, primary_key = True) @@ -87,11 +72,31 @@ class Stack(db.Model): def get_id(self): return self.id +class AuthorSchema(Schema): + id = fields.Int(dump_only=True) + author_name = fields.Str() + class StackSchema(Schema): id = fields.Int(dump_only=True) stack_name = fields.Str() stack_description = fields.Str() +class BookSchema(Schema): + id = fields.Int(dump_only=True) + title = fields.Str() + file = fields.Str() + authors = fields.Nested(AuthorSchema, many=True) + cover = fields.Str() + html = fields.Str() + fileformat = fields.Str() + category = fields.Str() + year_published = fields.Str() + stacks = fields.Nested(StackSchema, many=True) + + + + + def must_not_be_blank(data): if not data: raise ValidationError('You forgot to write stuff.') diff --git a/app/templates/base.html b/app/templates/base.html index b3eab28..77e1d1e 100755 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -103,6 +103,24 @@ $( function() { } }); } ); + +$( "#title" ).click(function() { + generateTitle(this); +}); + +$( document ).ready(function() { + generateTitle("#title"); +}); + +function generateTitle(elem){ +var x = ["XPERIMENTAL"] +var p1 = ["POTENTIAL", "PUBLIC", "POST", "PI", "PLATFORM FOR", "PRETENTIOUS"] +var p2 = ["PIRATE", "PERFORMATIVE", "PUBLIC", "PUBLISHING", "POTENTIAL"] +var l = ["LIBRARY", "LIAISON", "LAB", "LEGALITY", "LABOUR"] + +$(elem).text(x[Math.floor(Math.random()*x.length)]+" "+p1[Math.floor(Math.random()*p1.length)]+" "+p2[Math.floor(Math.random()*p2.length)]+" "+l[Math.floor(Math.random()*l.length)]); + +} diff --git a/app/templates/home.html b/app/templates/home.html index 197b107..b6d2605 100755 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% block main %} -

XPPL

+

XPPL

This is the awesome library of Experimental Publishing.
This might only be one interface to this library: … diff --git a/app/views.py b/app/views.py index c6cdc91..e6f5bc6 100755 --- a/app/views.py +++ b/app/views.py @@ -23,6 +23,8 @@ author_schema = AuthorSchema() authors_schema = AuthorSchema(many=True) book_schema = BookSchema() books_schema = BookSchema(many=True) +stack_schema = StackSchema() +stacks_schema = StackSchema(many=True) def allowed_file(filename): return '.' in filename and \ @@ -96,7 +98,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(book_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')) @@ -120,29 +122,16 @@ def edit_book_by_id(id): book.title = title book.category = category - 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) + 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')) @@ -304,8 +293,8 @@ def search_results(query): @app.route('/api/books', methods=['GET']) def get_books(): books = Book.query.all() - data, errors = books_schema.dump(books) - print(errors) + data = books_schema.dump(books) + #print(errors) return jsonify({'books': data}) @app.route('/api/books/', methods=['GET']) diff --git a/test.py b/test.py index 64c780b..7897f1e 100644 --- a/test.py +++ b/test.py @@ -1,58 +1,76 @@ -from sqlalchemy import create_engine -from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence +#!/usr/bin/env python + +import argparse +import functools +import logging +import pyrqlite.dbapi2 as dbapi2 +#import sqlite3 as dbapi2 + from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker, relationship - +from sqlalchemy.orm import Session +from sqlalchemy import Table, Column, Integer, String, ForeignKey, Sequence, create_engine +from sqlalchemy.dialects import registry +registry.register("rqlite.pyrqlite", "sqlalchemy_rqlite.pyrqlite", "dialect") Base = declarative_base() - -teachers_lessons = Table( - "teachers_lessons", +books_authors = Table( + "books_authors", Base.metadata, - Column("fk_teacher", Integer, ForeignKey("teachers.id")), - Column("fk_lesson", Integer, ForeignKey("lessons.id")), + Column("book", Integer, ForeignKey("books.id")), + Column("author", Integer, ForeignKey("authors.id")), ) - -class Teacher(Base): - __tablename__ = "teachers" - - id = Column("id", Integer, Sequence("teachers_id_seq"), primary_key=True) +class Book(Base): + __tablename__ = 'books' + id = Column(Integer, primary_key=True) + title = Column("title", String(50), nullable=False) + authors = relationship( + "Author", + backref="books", + secondary=books_authors + ) + + def __init__(self, title): + self.title = title + +class Author(Base): + __tablename__ = "authors" + id = Column("id", Integer, Sequence("authors_id_seq"), primary_key=True) name = Column("name", String(50), nullable=False) - lessons = relationship( - "Lesson", - backref="teachers", - secondary=teachers_lessons - ) + def __init__(self, title): + self.name = name +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('-v', '--verbose', action='store_true', default=False) + args = parser.parse_args() -class Lesson(Base): - __tablename__ = "lessons" - - id = Column("id", Integer, Sequence("lessons_id_seq"), primary_key=True) - name = Column("name", String(50), nullable=False) + if args.verbose: + logging.basicConfig() + logging.getLogger().setLevel(logging.DEBUG) + engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=args.verbose) -engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=True) -Base.metadata.create_all(engine) -Session = sessionmaker(bind=engine) + Base.metadata.create_all(engine) + session = Session(engine) + #insert + # for i in range(1): + # book = Book("book about dbs2") + # author = Author("footurist") + # book.authors.append(author) + # session.add(book) + # session.commit() -if __name__ == "__main__": - s = Session() + #delete + desired_book = session.query(Book).filter_by(id=3).delete() + #desired_author = session.query(Author).filter_by(id=2).first() + #desired_book.authors.clear() + #desired_book.authors.append(desired_author) + session.commit() - 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 +if __name__ == '__main__': + main() diff --git a/test2.py b/test2.py new file mode 100644 index 0000000..e471ed1 --- /dev/null +++ b/test2.py @@ -0,0 +1,49 @@ +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__": + + t2 = s.query(Teacher).filter_by(name='NEWNEW').first() + t2.lessons.clear() + s.commit() + + \ No newline at end of file