|
| 1 | +# routes.py |
| 2 | + |
| 3 | +from app import app, collection |
| 4 | +from flask import render_template, redirect, url_for, flash, request, jsonify |
| 5 | +from datetime import datetime |
| 6 | +from forms import AddBookForm, EditBookForm, DeleteBookForm |
| 7 | +from bson import ObjectId # Import ObjectId |
| 8 | + |
| 9 | +@app.route('/') |
| 10 | +@app.route('/index') |
| 11 | +def index(): |
| 12 | + books = list(collection.find()) |
| 13 | + return render_template('index.html', books=books) |
| 14 | + |
| 15 | +@app.route('/add', methods=['GET', 'POST']) |
| 16 | +def add(): |
| 17 | + form = AddBookForm() |
| 18 | + |
| 19 | + if form.validate_on_submit(): |
| 20 | + isbn = form.isbn.data |
| 21 | + title = form.title.data |
| 22 | + year = form.year.data |
| 23 | + price = form.price.data |
| 24 | + page = form.page.data |
| 25 | + category = form.category.data |
| 26 | + coverPhoto = form.coverPhoto.data |
| 27 | + publisher = form.publisher.data |
| 28 | + author = form.author.data |
| 29 | + |
| 30 | + date = datetime.utcnow() |
| 31 | + collection.insert_one({ |
| 32 | + "isbn": isbn, |
| 33 | + "title": title, |
| 34 | + "year": year, |
| 35 | + "price": price, |
| 36 | + "page": page, |
| 37 | + "category": category, |
| 38 | + "coverPhoto": coverPhoto, |
| 39 | + "publisher": publisher, |
| 40 | + "author": author, |
| 41 | + "date": date |
| 42 | + }) |
| 43 | + |
| 44 | + flash('The book was added to the database.') |
| 45 | + return redirect(url_for('index')) |
| 46 | + |
| 47 | + return render_template('add.html', form=form) |
| 48 | + |
| 49 | +@app.route('/edit/<string:book_id>', methods=['GET', 'POST']) |
| 50 | +def edit(book_id): |
| 51 | + book = collection.find_one({"_id": ObjectId(book_id)}) |
| 52 | + form = EditBookForm() |
| 53 | + |
| 54 | + if book: |
| 55 | + if form.validate_on_submit(): |
| 56 | + title = form.title.data |
| 57 | + date = datetime.utcnow() |
| 58 | + collection.update_one({"_id": ObjectId(book_id)}, {"$set": {"title": title, "date": date}}) |
| 59 | + flash('The book in the database was updated.') |
| 60 | + return redirect(url_for('index')) |
| 61 | + form.title.data = book['title'] |
| 62 | + return render_template('edit.html', form=form, book_id=book_id, book=book) # Pass 'book' variable to the template |
| 63 | + else: |
| 64 | + flash('The book ID is not in the database.') |
| 65 | + return redirect(url_for('index')) |
| 66 | + |
| 67 | + |
| 68 | +@app.route('/delete/<string:book_id>', methods=['GET', 'POST']) |
| 69 | +def delete(book_id): |
| 70 | + book = collection.find_one({"_id": ObjectId(book_id)}) # Use ObjectId |
| 71 | + form = DeleteBookForm() |
| 72 | + |
| 73 | + if book: |
| 74 | + if form.validate_on_submit(): |
| 75 | + collection.delete_one({"_id": ObjectId(book_id)}) # Use ObjectId |
| 76 | + flash('The book in the database was deleted.') |
| 77 | + return redirect(url_for('index')) |
| 78 | + return render_template('delete.html', form=form, book_id=book_id, title=book['title']) |
| 79 | + else: |
| 80 | + flash('The book ID is not in the database.') |
| 81 | + return redirect(url_for('index')) |
| 82 | + |
| 83 | +# Updated route for CRUD operations |
| 84 | +@app.route('/crud/<string:operation>/<string:book_id>', methods=['GET', 'POST', 'PUT', 'DELETE']) |
| 85 | +def crud(operation, book_id): |
| 86 | + book = collection.find_one({"_id": ObjectId(book_id)}) |
| 87 | + |
| 88 | + if operation == 'view': |
| 89 | + return render_template('view.html', book=book) |
| 90 | + |
| 91 | + form = EditBookForm() |
| 92 | + |
| 93 | + if request.method == 'GET': |
| 94 | + # Handle GET request |
| 95 | + if operation == 'edit': |
| 96 | + form.title.data = book['title'] |
| 97 | + return render_template('edit.html', form=form, book_id=book_id) |
| 98 | + elif operation == 'delete': |
| 99 | + return render_template('delete.html', form=form, book_id=book_id, title=book['title']) |
| 100 | + else: |
| 101 | + flash('Invalid operation.') |
| 102 | + return redirect(url_for('index')) |
| 103 | + |
| 104 | + elif request.method == 'POST': |
| 105 | + # Handle POST request |
| 106 | + if operation == 'edit' and form.validate_on_submit(): |
| 107 | + title = form.title.data |
| 108 | + date = datetime.utcnow() |
| 109 | + collection.update_one({"_id": ObjectId(book_id)}, {"$set": {"title": title, "date": date}}) |
| 110 | + flash('The book in the database was updated.') |
| 111 | + return redirect(url_for('index')) |
| 112 | + elif operation == 'delete' and form.validate_on_submit(): |
| 113 | + collection.delete_one({"_id": ObjectId(book_id)}) |
| 114 | + flash('The book in the database was deleted.') |
| 115 | + return redirect(url_for('index')) |
| 116 | + else: |
| 117 | + flash('Invalid operation.') |
| 118 | + return redirect(url_for('index')) |
| 119 | + |
| 120 | + elif request.method == 'PUT': |
| 121 | + # Handle PUT request (using JSON for simplicity) |
| 122 | + data = request.get_json() |
| 123 | + title = data.get('title') |
| 124 | + date = datetime.utcnow() |
| 125 | + collection.update_one({"_id": ObjectId(book_id)}, {"$set": {"title": title, "date": date}}) |
| 126 | + return jsonify({"message": "Book updated successfully"}) |
| 127 | + |
| 128 | + elif request.method == 'DELETE': |
| 129 | + # Handle DELETE request (using JSON for simplicity) |
| 130 | + collection.delete_one({"_id": ObjectId(book_id)}) |
| 131 | + return jsonify({"message": "Book deleted successfully"}) |
| 132 | + |
| 133 | + else: |
| 134 | + flash('Invalid method.') |
| 135 | + return redirect(url_for('index')) |
0 commit comments