app.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. from flask import (
  2. Flask, redirect, render_template, request, flash, jsonify, send_file
  3. )
  4. from contacts_model import Contact, Archiver
  5. import time
  6. Contact.load_db()
  7. # ========================================================
  8. # Flask App
  9. # ========================================================
  10. app = Flask(__name__)
  11. app.secret_key = b'hypermedia rocks'
  12. @app.route("/")
  13. def index():
  14. return redirect("/contacts")
  15. @app.route("/contacts")
  16. def contacts():
  17. search = request.args.get("q")
  18. page = int(request.args.get("page", 1))
  19. if search is not None:
  20. contacts_set = Contact.search(search)
  21. if request.headers.get('HX-Trigger') == 'search':
  22. return render_template("rows.html", contacts=contacts_set)
  23. else:
  24. contacts_set = Contact.all()
  25. return render_template("index.html", contacts=contacts_set, archiver=Archiver.get())
  26. @app.route("/contacts/archive", methods=["POST"])
  27. def start_archive():
  28. archiver = Archiver.get()
  29. archiver.run()
  30. return render_template("archive_ui.html", archiver=archiver)
  31. @app.route("/contacts/archive", methods=["GET"])
  32. def archive_status():
  33. archiver = Archiver.get()
  34. return render_template("archive_ui.html", archiver=archiver)
  35. @app.route("/contacts/archive/file", methods=["GET"])
  36. def archive_content():
  37. archiver = Archiver.get()
  38. return send_file(archiver.archive_file(), "archive.json", as_attachment=True)
  39. @app.route("/contacts/archive", methods=["DELETE"])
  40. def reset_archive():
  41. archiver = Archiver.get()
  42. archiver.reset()
  43. return render_template("archive_ui.html", archiver=archiver)
  44. @app.route("/contacts/count")
  45. def contacts_count():
  46. count = Contact.count()
  47. return "(" + str(count) + " total Contacts)"
  48. @app.route("/contacts/new", methods=['GET'])
  49. def contacts_new_get():
  50. return render_template("new.html", contact=Contact())
  51. @app.route("/contacts/new", methods=['POST'])
  52. def contacts_new():
  53. c = Contact(None, request.form['first_name'], request.form['last_name'], request.form['phone'],
  54. request.form['email'])
  55. if c.save():
  56. flash("Created New Contact!")
  57. return redirect("/contacts")
  58. else:
  59. return render_template("new.html", contact=c)
  60. @app.route("/contacts/<contact_id>")
  61. def contacts_view(contact_id=0):
  62. contact = Contact.find(contact_id)
  63. return render_template("show.html", contact=contact)
  64. @app.route("/contacts/<contact_id>/edit", methods=["GET"])
  65. def contacts_edit_get(contact_id=0):
  66. contact = Contact.find(contact_id)
  67. return render_template("edit.html", contact=contact)
  68. @app.route("/contacts/<contact_id>/edit", methods=["POST"])
  69. def contacts_edit_post(contact_id=0):
  70. c = Contact.find(contact_id)
  71. c.update(request.form['first_name'], request.form['last_name'], request.form['phone'], request.form['email'])
  72. if c.save():
  73. flash("Updated Contact!")
  74. return redirect("/contacts/" + str(contact_id))
  75. else:
  76. return render_template("edit.html", contact=c)
  77. @app.route("/contacts/<contact_id>/email", methods=["GET"])
  78. def contacts_email_get(contact_id=0):
  79. c = Contact.find(contact_id)
  80. c.email = request.args.get('email')
  81. c.validate()
  82. return c.errors.get('email') or ""
  83. @app.route("/contacts/<contact_id>", methods=["DELETE"])
  84. def contacts_delete(contact_id=0):
  85. contact = Contact.find(contact_id)
  86. contact.delete()
  87. if request.headers.get('HX-Trigger') == 'delete-btn':
  88. flash("Deleted Contact!")
  89. return redirect("/contacts", 303)
  90. else:
  91. return ""
  92. @app.route("/contacts/", methods=["DELETE"])
  93. def contacts_delete_all():
  94. contact_ids = list(map(int, request.form.getlist("selected_contact_ids")))
  95. for contact_id in contact_ids:
  96. contact = Contact.find(contact_id)
  97. contact.delete()
  98. flash("Deleted Contacts!")
  99. contacts_set = Contact.all(1)
  100. return render_template("index.html", contacts=contacts_set)
  101. # ===========================================================
  102. # JSON Data API
  103. # ===========================================================
  104. @app.route("/api/v1/contacts", methods=["GET"])
  105. def json_contacts():
  106. contacts_set = Contact.all()
  107. return {"contacts": [c.__dict__ for c in contacts_set]}
  108. @app.route("/api/v1/contacts", methods=["POST"])
  109. def json_contacts_new():
  110. c = Contact(None, request.form.get('first_name'), request.form.get('last_name'), request.form.get('phone'),
  111. request.form.get('email'))
  112. if c.save():
  113. return c.__dict__
  114. else:
  115. return {"errors": c.errors}, 400
  116. @app.route("/api/v1/contacts/<contact_id>", methods=["GET"])
  117. def json_contacts_view(contact_id=0):
  118. contact = Contact.find(contact_id)
  119. return contact.__dict__
  120. @app.route("/api/v1/contacts/<contact_id>", methods=["PUT"])
  121. def json_contacts_edit(contact_id):
  122. c = Contact.find(contact_id)
  123. c.update(request.form['first_name'], request.form['last_name'], request.form['phone'], request.form['email'])
  124. if c.save():
  125. return c.__dict__
  126. else:
  127. return {"errors": c.errors}, 400
  128. @app.route("/api/v1/contacts/<contact_id>", methods=["DELETE"])
  129. def json_contacts_delete(contact_id=0):
  130. contact = Contact.find(contact_id)
  131. contact.delete()
  132. return jsonify({"success": True})
  133. if __name__ == "__main__":
  134. app.run()