diff --git a/.DS_Store b/.DS_Store index 643cb1c..fe71f83 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/app/forms.py b/app/forms.py index 62cb3fe..211872c 100755 --- a/app/forms.py +++ b/app/forms.py @@ -11,13 +11,18 @@ class AuthorForm(NoCsrfForm): # this forms is never exposed so we can user the non CSRF version author_name = StringField('Author Name', validators=[DataRequired()]) -class UserForm(FlaskForm): +class UploadForm(FlaskForm): title = StringField('title', validators=[InputRequired()]) author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1) category = StringField('category', validators=[InputRequired()]) file = FileField() + upload = SubmitField(label='Upload') + wish = SubmitField(label='''I don't have the file, but wish I did.''') -class UserForm_Edit(FlaskForm): +class EditForm(FlaskForm): title = StringField('title', validators=[InputRequired()]) author = FieldList(FormField(AuthorForm, default=lambda: Author()), min_entries=1) category = StringField('category', validators=[InputRequired()]) + +class SearchForm(FlaskForm): + search = StringField('', validators=[InputRequired()]) diff --git a/app/models.py b/app/models.py index 5c63322..ea199b3 100755 --- a/app/models.py +++ b/app/models.py @@ -27,13 +27,12 @@ class Book(db.Model): scapeX = db.Column(db.Numeric(10,2)) scapeY = db.Column(db.Numeric(10,2)) - def __init__(self, title, file, cover, fileformat, category, stack): + def __init__(self, title, file, cover, fileformat, category): self.title = title self.file = file self.cover = cover self.fileformat = fileformat self.category = category - self.stack = stack self.scapeX = 0 self.scapeY = 0 diff --git a/app/static/css/style.css b/app/static/css/style.css index f2d1914..f442e51 100755 --- a/app/static/css/style.css +++ b/app/static/css/style.css @@ -71,6 +71,34 @@ width: 500px; font-size: 16px; } +.search input{ +margin: 0; +float: left; +width: 400px; +height: 36px; +font-size: 20px; +font-weight: regular; +padding: 2px; +background:rgba(50, 50, 50, 0.2); +border:0px; +} + +.button { +height:40px; +font-size: 18px; +padding:6px 15px; +left:0px; +border:0px solid #dbdbdb; +background-color: grey; +color:#fafafa; +} + +.button:hover { +background-color:red; +color: #fafafa; +} + + .footer{ width: 100%; diff --git a/app/templates/_formhelpers.html b/app/templates/_formhelpers.html new file mode 100644 index 0000000..d4ad8c2 --- /dev/null +++ b/app/templates/_formhelpers.html @@ -0,0 +1,12 @@ +{% macro render_field(field) %} +
{{ field.label }} +
{{ field(**kwargs)|safe }} + {% if field.errors %} + + {% endif %} +
+{% endmacro %} \ No newline at end of file diff --git a/app/templates/add_book.html b/app/templates/add_book.html index 1fbb362..2121566 100755 --- a/app/templates/add_book.html +++ b/app/templates/add_book.html @@ -37,7 +37,8 @@
{{ form.category.label }} {{ form.category(size=20, class="form-control") }}
{{ form.file }} - + {{ form.upload }} + {{ form.wish }} diff --git a/app/templates/red_link.html b/app/templates/red_link.html index 95b940d..133e954 100755 --- a/app/templates/red_link.html +++ b/app/templates/red_link.html @@ -24,7 +24,48 @@ {% block header %}{% endblock %} {% block main %} -

ID: {{ id }}

-

red link page

+

We don't have any results for: {{ title }}

+

upload?

go back home?

+ + +
+

Add Book

+ {% with messages = get_flashed_messages() %} + {% if messages %} +
+ +
+ {% endif %} + {% endwith %} +
+ {{ form.csrf_token }} +
{{ form.title.label }} {{ form.title(size=20, class="form-control") }}
+
+
+ {{ form.author.label }} + + + + + + {% for author in form.author %} + + + + + {% endfor %} +
{{ author.author_name }}
+
+
+
{{ form.tag.label }} {{ form.tag(size=20, class="form-control") }}
+ {{ form.file }} + +
+
{% endblock %} diff --git a/app/templates/results.html b/app/templates/results.html new file mode 100644 index 0000000..9262d35 --- /dev/null +++ b/app/templates/results.html @@ -0,0 +1,91 @@ +{% extends 'base.html' %} + +{% block main %} + +
+ {% from "_formhelpers.html" import render_field %} +
+ + +
+ +
+ +
+

Search Results for: {{ query }}

+ {% with messages = get_flashed_messages() %} + {% if messages %} +
+ +
+ {% endif %} + {% endwith %} + + + + + + + + + + {% for book in books %} + + + + + + + + + {% endfor %} +
CoverTitleAuthorFiletypeTag
{{ book.title }} {% for author in book.authors %} + +
  • {{ author.author_name }}
  • + + {% endfor %}
    {{ book.fileformat }}{{ book.tag}}
    + +
    + +
    +
    +

    Other books

    + + + + + + + + + + {% for book in books_all %} + + + + + + + + + {% endfor %} +
    CoverTitleAuthorFiletypeTag
    {{ book.title }} {% for author in book.authors %} + +
  • {{ author.author_name }}
  • + + + {% endfor %}
    {{ book.fileformat }}{{ book.tag}}
    +

    + See all books +

    + +
    + +{% endblock %} + + diff --git a/app/templates/show_books.html b/app/templates/show_books.html index 420b911..7cfe4b1 100755 --- a/app/templates/show_books.html +++ b/app/templates/show_books.html @@ -2,6 +2,14 @@ {% block main %}
    + + {% from "_formhelpers.html" import render_field %} +
    + + +
    +

    All Books

    {% with messages = get_flashed_messages() %} {% if messages %} diff --git a/app/views.py b/app/views.py index 98bf49d..7909b86 100755 --- a/app/views.py +++ b/app/views.py @@ -8,7 +8,8 @@ This file creates your application. from app import app, db from flask import Flask, render_template, request, redirect, url_for, flash, send_from_directory, jsonify, abort import json -from app.forms import UserForm, UserForm_Edit +from sqlalchemy.sql.expression import func, select +from app.forms import UploadForm, EditForm, SearchForm from app.models import Book, BookSchema, Author, AuthorSchema, Stack, StackSchema from app.cover import get_cover @@ -54,10 +55,13 @@ def uploaded_file_cover(filename): return send_from_directory(app.config['UPLOAD_FOLDER_COVER'], filename) -@app.route('/books') +@app.route('/books', methods= ['POST','GET']) def show_books(): - books = db.session.query(Book).all() # or you could have used User.query.all() - return render_template('show_books.html', books=books) + books = db.session.query(Book).all() + search = SearchForm(request.form) + if request.method == 'POST': + return redirect((url_for('search_results', query=search.search.data))) + return render_template('show_books.html', books=books, form=search) @app.route('/scape', methods=['POST', 'GET']) def scape(): @@ -100,7 +104,7 @@ def remove_book_by_id(id): @app.route('/books//edit', methods=['POST', 'GET']) def edit_book_by_id(id): book_to_edit = Book.query.filter_by(id=id).first() - user_form = UserForm_Edit(title = book_to_edit.title, author =book_to_edit.authors, category = book_to_edit.category ) + user_form = EditForm(title = book_to_edit.title, author =book_to_edit.authors, category = book_to_edit.category ) if request.method == 'POST': if user_form.validate_on_submit(): @@ -148,30 +152,61 @@ def edit_book_by_id(id): @app.route('/add-book', methods=['POST', 'GET']) def add_book(): - user_form = UserForm() + upload_form = UploadForm() if request.method == 'POST': - if user_form.validate_on_submit(): - # check if the post request has the file part - if 'file' not in request.files: - flash('No file part') - return redirect(request.url) - file = request.files['file'] - # if user does not select file, browser also - # submit a empty part without filename - if file.filename == '': - flash('No selected file') - return redirect(request.url) - if file and allowed_file(file.filename): - filename = secure_filename(file.filename) - fullpath = os.path.join(app.config['UPLOAD_FOLDER'], filename) - name, file_extension = os.path.splitext(filename) - file.save(fullpath) - cover = get_cover(fullpath, name) - title = user_form.title.data # You could also have used request.form['name'] - authors = user_form.author.data # You could also have used - category = user_form.category.data + if upload_form.validate_on_submit(): + if upload_form.upload.data: + # check if the post request has the file part + if 'file' not in request.files: + flash('No file part') + return redirect(request.url) + file = request.files['file'] + # if user does not select file, browser also + # submit a empty part without filename + if file.filename == '': + flash('No selected file') + return redirect(request.url) + if file and allowed_file(file.filename): + filename = secure_filename(file.filename) + fullpath = os.path.join(app.config['UPLOAD_FOLDER'], filename) + name, file_extension = os.path.splitext(filename) + file.save(fullpath) + cover = get_cover(fullpath, name) + title = upload_form.title.data # You could also have used request.form['name'] + authors = upload_form.author.data # You could also have used + category = upload_form.category.data + #print(author) + #print(len(author)) + book = Book(title, filename, cover, file_extension, category) + db.session.add(book) + 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) + db.session.commit() + # save user to database + + + flash("%s added to the library" % (title)) + return redirect(url_for('show_books')) + else: + flash('allowed file formats: %s' % ALLOWED_EXTENSIONS) + + if upload_form.wish.data: + file = open('app/uploads/potential.pdf') + filename = 'potential.pdf' + file_extension = '.pdf' + cover = get_cover('app/uploads/potential.pdf', 'default') + + title = upload_form.title.data # You could also have used request.form['name'] + authors = upload_form.author.data # You could also have used + category = upload_form.category.data #print(author) #print(len(author)) book = Book(title, filename, cover, file_extension, category) @@ -185,16 +220,14 @@ def add_book(): db.session.add(a) book.authors.append(a) db.session.commit() - # save user to database + flash("%s added to the library" % (title)) + return redirect(url_for('show_books')) - flash("%s added to the library" % (title)) - return redirect(url_for('show_books')) - else: - flash('allowed file formats: %s' % ALLOWED_EXTENSIONS) - flash_errors(user_form) - return render_template('add_book.html', form=user_form) + flash_errors(upload_form) + return render_template('add_book.html', form=upload_form) + # Flash errors from the form if validation fails def flash_errors(form): @@ -235,6 +268,27 @@ def show_stack_by_id(id): else: return render_template('show_stack_detail.html', stack=stack) +## search + +@app.route('/search//', methods=['POST', 'GET']) +def search_results(query): + search = SearchForm(request.form) + books_all=Book.query.all() + random_order=Book.query.order_by(func.random()).limit(10) + results=Book.query.filter(Book.title.contains(query)).all() + + if not results: + upload_form = UploadForm(title= query, author='') + return render_template('red_link.html', form=upload_form, title=query) + + if request.method == 'POST': + query = search.search.data + results = [] + results=Book.query.filter(Book.title.contains(query)).all() + return redirect((url_for('search_results', query=search.search.data))) + + return render_template('results.html', form=search, books=results, books_all=random_order, query=query) + ### # The API