123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- from flask import (
- Flask, redirect, render_template, request, flash, jsonify, send_file
- )
- from contacts_model import Contact, Archiver
- import time
- Contact.load_db()
- # ========================================================
- # Flask App
- # ========================================================
- app = Flask(__name__)
- app.secret_key = b'hypermedia rocks'
- @app.route("/")
- def index():
- return redirect("/contacts")
- @app.route("/contacts")
- def contacts():
- search = request.args.get("q")
- page = int(request.args.get("page", 1))
- if search is not None:
- contacts_set = Contact.search(search)
- if request.headers.get('HX-Trigger') == 'search':
- return render_template("rows.html", contacts=contacts_set)
- else:
- contacts_set = Contact.all()
- return render_template("index.html", contacts=contacts_set, archiver=Archiver.get())
- @app.route("/contacts/archive", methods=["POST"])
- def start_archive():
- archiver = Archiver.get()
- archiver.run()
- return render_template("archive_ui.html", archiver=archiver)
- @app.route("/contacts/archive", methods=["GET"])
- def archive_status():
- archiver = Archiver.get()
- return render_template("archive_ui.html", archiver=archiver)
- @app.route("/contacts/archive/file", methods=["GET"])
- def archive_content():
- archiver = Archiver.get()
- return send_file(archiver.archive_file(), "archive.json", as_attachment=True)
- @app.route("/contacts/archive", methods=["DELETE"])
- def reset_archive():
- archiver = Archiver.get()
- archiver.reset()
- return render_template("archive_ui.html", archiver=archiver)
- @app.route("/contacts/count")
- def contacts_count():
- count = Contact.count()
- return "(" + str(count) + " total Contacts)"
- @app.route("/contacts/new", methods=['GET'])
- def contacts_new_get():
- return render_template("new.html", contact=Contact())
- @app.route("/contacts/new", methods=['POST'])
- def contacts_new():
- c = Contact(None, request.form['first_name'], request.form['last_name'], request.form['phone'],
- request.form['email'])
- if c.save():
- flash("Created New Contact!")
- return redirect("/contacts")
- else:
- return render_template("new.html", contact=c)
- @app.route("/contacts/<contact_id>")
- def contacts_view(contact_id=0):
- contact = Contact.find(contact_id)
- return render_template("show.html", contact=contact)
- @app.route("/contacts/<contact_id>/edit", methods=["GET"])
- def contacts_edit_get(contact_id=0):
- contact = Contact.find(contact_id)
- return render_template("edit.html", contact=contact)
- @app.route("/contacts/<contact_id>/edit", methods=["POST"])
- def contacts_edit_post(contact_id=0):
- c = Contact.find(contact_id)
- c.update(request.form['first_name'], request.form['last_name'], request.form['phone'], request.form['email'])
- if c.save():
- flash("Updated Contact!")
- return redirect("/contacts/" + str(contact_id))
- else:
- return render_template("edit.html", contact=c)
- @app.route("/contacts/<contact_id>/email", methods=["GET"])
- def contacts_email_get(contact_id=0):
- c = Contact.find(contact_id)
- c.email = request.args.get('email')
- c.validate()
- return c.errors.get('email') or ""
- @app.route("/contacts/<contact_id>", methods=["DELETE"])
- def contacts_delete(contact_id=0):
- contact = Contact.find(contact_id)
- contact.delete()
- if request.headers.get('HX-Trigger') == 'delete-btn':
- flash("Deleted Contact!")
- return redirect("/contacts", 303)
- else:
- return ""
- @app.route("/contacts/", methods=["DELETE"])
- def contacts_delete_all():
- contact_ids = list(map(int, request.form.getlist("selected_contact_ids")))
- for contact_id in contact_ids:
- contact = Contact.find(contact_id)
- contact.delete()
- flash("Deleted Contacts!")
- contacts_set = Contact.all(1)
- return render_template("index.html", contacts=contacts_set)
- # ===========================================================
- # JSON Data API
- # ===========================================================
- @app.route("/api/v1/contacts", methods=["GET"])
- def json_contacts():
- contacts_set = Contact.all()
- return {"contacts": [c.__dict__ for c in contacts_set]}
- @app.route("/api/v1/contacts", methods=["POST"])
- def json_contacts_new():
- c = Contact(None, request.form.get('first_name'), request.form.get('last_name'), request.form.get('phone'),
- request.form.get('email'))
- if c.save():
- return c.__dict__
- else:
- return {"errors": c.errors}, 400
- @app.route("/api/v1/contacts/<contact_id>", methods=["GET"])
- def json_contacts_view(contact_id=0):
- contact = Contact.find(contact_id)
- return contact.__dict__
- @app.route("/api/v1/contacts/<contact_id>", methods=["PUT"])
- def json_contacts_edit(contact_id):
- c = Contact.find(contact_id)
- c.update(request.form['first_name'], request.form['last_name'], request.form['phone'], request.form['email'])
- if c.save():
- return c.__dict__
- else:
- return {"errors": c.errors}, 400
- @app.route("/api/v1/contacts/<contact_id>", methods=["DELETE"])
- def json_contacts_delete(contact_id=0):
- contact = Contact.find(contact_id)
- contact.delete()
- return jsonify({"success": True})
- if __name__ == "__main__":
- app.run()
|