rqlite update

This commit is contained in:
Alex 2018-06-01 20:26:37 +02:00
parent d0c8cb968e
commit d029e57c72
7 changed files with 160 additions and 81 deletions

View File

@ -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__)

View File

@ -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.')

View File

@ -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)]);
}
</script>
</body>
</html>

View File

@ -1,7 +1,7 @@
{% extends "base.html" %}
{% block main %}
<h1 class="header">XPPL</h1>
<h1 class="header" id="title">XPPL</h1>
<p class="lead">This is the awesome library of Experimental Publishing. <br>
This might only be one interface to this library:

View File

@ -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/<int:id>', methods=['GET'])

96
test.py
View File

@ -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 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
)
class Teacher(Base):
__tablename__ = "teachers"
def __init__(self, title):
self.title = title
id = Column("id", Integer, Sequence("teachers_id_seq"), primary_key=True)
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"
if args.verbose:
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
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=args.verbose)
Base.metadata.create_all(engine)
session = Session(engine)
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()
#insert
# for i in range(1):
# book = Book("book about dbs2")
# author = Author("footurist")
# book.authors.append(author)
# session.add(book)
# session.commit()
#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()
if __name__ == '__main__':
main()

49
test2.py Normal file
View File

@ -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()