messages.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from flask import Flask, render_template
  2. from flask_paginate import Pagination, get_page_parameter, get_page_args
  3. from flask_caching import Cache
  4. from flask import request
  5. import pendulum
  6. app = Flask(__name__)
  7. @app.template_filter("datefmt")
  8. def format_datetime(value):
  9. dt = pendulum.from_timestamp(value, tz=pendulum.tz.local_timezone())
  10. return dt.to_datetime_string()
  11. # Check Configuring Flask-Caching section for more details
  12. cache = Cache(app, config={"CACHE_TYPE": "filesystem", "CACHE_DIR": "cache"})
  13. import jammin
  14. bases = {"FSX_GEN": "fsx_bot", "FSX_BOT": "fsx_bot"}
  15. @cache.cached(timeout=50, key_prefix="messages")
  16. def get_messages(base):
  17. return jammin.get_messages(base)
  18. @app.route("/list")
  19. def list_bases():
  20. return render_template("list.html", bases=bases)
  21. # return 'Here would be a listing of message bases'
  22. @app.route("/messages/<area>")
  23. def display_messages(area):
  24. if area not in bases:
  25. return render_template("missing-area.html")
  26. # messages = jammin.get_messages(bases[area])
  27. messages = get_messages(bases[area])
  28. messages.reverse()
  29. page = request.args.get(get_page_parameter(), type=int, default=1)
  30. # get_page_arg defaults to page 1, per_page of 10
  31. PER_PAGE = 50
  32. total = len(messages)
  33. pagination = Pagination(
  34. page=page,
  35. total=total,
  36. css_framework="foundation",
  37. record_name="messages",
  38. per_page=PER_PAGE,
  39. )
  40. page, per_page, offset = get_page_args()
  41. start = (page - 1) * PER_PAGE
  42. end = start + PER_PAGE
  43. # messages = messages[(page-1) * PER_PAGE:offset+PER_PAGE]
  44. messages = messages[start:end]
  45. return render_template(
  46. "messages.html", messages=messages, area=area, pagination=pagination
  47. )
  48. @app.route("/read/<area>/<int:msgno>")
  49. def display_message(area, msgno):
  50. if area not in bases:
  51. return render_template("missing-area.html")
  52. message = jammin.read_message(bases[area], msgno)
  53. if "text" in message:
  54. message["text"] = message["text"].replace("\r", "\n") # <br >\n")
  55. return render_template("message.html", message=message, area=area, msgnumber=msgno)