diff --git a/flaskapp/__pycache__/__init__.cpython-311.pyc b/flaskapp/__pycache__/__init__.cpython-311.pyc index cdf24bf..7a22727 100644 Binary files a/flaskapp/__pycache__/__init__.cpython-311.pyc and b/flaskapp/__pycache__/__init__.cpython-311.pyc differ diff --git a/flaskapp/__pycache__/forms.cpython-311.pyc b/flaskapp/__pycache__/forms.cpython-311.pyc index d42f9e8..98f4003 100644 Binary files a/flaskapp/__pycache__/forms.cpython-311.pyc and b/flaskapp/__pycache__/forms.cpython-311.pyc differ diff --git a/flaskapp/__pycache__/models.cpython-311.pyc b/flaskapp/__pycache__/models.cpython-311.pyc index c51d86e..61e1b3f 100644 Binary files a/flaskapp/__pycache__/models.cpython-311.pyc and b/flaskapp/__pycache__/models.cpython-311.pyc differ diff --git a/flaskapp/__pycache__/routes.cpython-311.pyc b/flaskapp/__pycache__/routes.cpython-311.pyc index 18791be..8feb981 100644 Binary files a/flaskapp/__pycache__/routes.cpython-311.pyc and b/flaskapp/__pycache__/routes.cpython-311.pyc differ diff --git a/flaskapp/models.py b/flaskapp/models.py index 94deade..2d7cf49 100644 --- a/flaskapp/models.py +++ b/flaskapp/models.py @@ -1,6 +1,6 @@ from flaskapp import db, login_manager from flask_login import UserMixin - +import json @login_manager.user_loader def load_user(user_id): @@ -34,4 +34,11 @@ class Book(db.Model): #cover = db.Column(db.LargeBinary) def __repr__(self): - return f"Book('{self.id}','{self.title}', '{self.subtitle}', '{self.genre}')" \ No newline at end of file + return f"Book('{self.id}','{self.title}', '{self.subtitle}', '{self.genre}')" + + def to_dict(self): + new_dict = self.__dict__.copy() + new_dict["__class__"] = self.__class__.__name__ + if "_sa_instance_state" in new_dict: + del new_dict["_sa_instance_state"] + return new_dict \ No newline at end of file diff --git a/flaskapp/routes.py b/flaskapp/routes.py index 6f97f09..5dfea31 100644 --- a/flaskapp/routes.py +++ b/flaskapp/routes.py @@ -1,4 +1,4 @@ -from flask import render_template, url_for, flash, redirect, request, jsonify +from flask import send_from_directory, render_template, url_for, flash, redirect, request, jsonify from flaskapp.models import User, Book from flaskapp.forms import RegistrationForm, LoginForm, UpdateAccountForm, AddBookForm from flaskapp import app, db, bcrypt @@ -81,9 +81,13 @@ def load_content(tab_name): # form.new_password.data = current_user.password return render_template(f'{tab_name}.html', form=form) if tab_name == 'Publish': - return render_template(f'{tab_name}.html', form=form1) + return render_template(f'{tab_name}.html', form=form1) if tab_name == 'MyBooks': - return render_template(f'{tab_name}.html', form=form2) + books = db.session.query(Book).filter(Book.author_id == "3") + myBooks = [] + for book in books: + myBooks.append(book.to_dict()) + return render_template(f'{tab_name}.html', books=myBooks, form=form2) content = render_template(f'{tab_name}.html', form=form) return content @@ -138,6 +142,13 @@ def publish(): @app.route('/mybooks', methods={'GET'}, strict_slashes=False) @login_required def getBooksByUser(): - books = db.session.query(Book).all() - return jsonify(books) + books = db.session.query(Book).filter(Book.author_id == "3") + myBooks = [] + for book in books: + myBooks.append(book.to_dict()) + return jsonify(myBooks) + +@app.route('/books/') +def get_image(filename): + return send_from_directory(os.path.join(os.getcwd(), 'books'), filename) diff --git a/flaskapp/static/scripts/Dahboard.js b/flaskapp/static/scripts/Dahboard.js index 760ac4f..b3b9269 100644 --- a/flaskapp/static/scripts/Dahboard.js +++ b/flaskapp/static/scripts/Dahboard.js @@ -13,10 +13,7 @@ $(document).ready(function () { $('#publish').click(function(event){ fetchData('Publish'); setActiveTab(this) - $.get("http://127.0.0.1:5000/mybooks", function(data){ - console.log(data) - - }); + }); $('#account').click(function(event){ diff --git a/flaskapp/templates/MyBooks.html b/flaskapp/templates/MyBooks.html index d95172e..a90b967 100644 --- a/flaskapp/templates/MyBooks.html +++ b/flaskapp/templates/MyBooks.html @@ -3,9 +3,8 @@

My BookShelf

{% for book in books %}

{{ book.title }}

- book + book
{% endfor %} - \ No newline at end of file