25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

52 satır
1.6KB

  1. import json
  2. from flask_classy import FlaskView, route
  3. from modules import cbpi
  4. class NotificationView(FlaskView):
  5. @route('/', methods=['GET'])
  6. def getMessages(self):
  7. """
  8. Get all messages
  9. :return: current messages
  10. """
  11. return json.dumps(cbpi.cache["messages"])
  12. @route('/<id>', methods=['DELETE'])
  13. def dismiss(self, id):
  14. """
  15. Delete message from cache by id
  16. :param id: message id to be deleted
  17. :return: empty response HTTP 204
  18. """
  19. for idx, m in enumerate(cbpi.cache.get("messages", [])):
  20. if (m.get("id") == id):
  21. cbpi.cache["messages"].pop(idx)
  22. return ('', 204)
  23. @cbpi.event("MESSAGE", async=True)
  24. def messageEvent(message, **kwargs):
  25. """
  26. React on message event. add the message to the cache and push the message to the clients
  27. :param message: the message
  28. :param kwargs: other parameter
  29. :return: None
  30. """
  31. msg = {"id": len(cbpi.cache["messages"]), "type": "info", "message": message, "read": False}
  32. cbpi.cache["messages"].append(msg)
  33. cbpi.emit('MESSAGE', msg,)
  34. @cbpi.initalizer(order=2)
  35. def init(cbpi):
  36. """
  37. Initializer for the message module
  38. :param app: the flask app
  39. :return: None
  40. """
  41. if cbpi.get_config_parameter("donation_notification", "YES") == "YES":
  42. msg = {"id": len(cbpi.cache["messages"]), "type": "info", "headline": "Support CraftBeerPi with your donation", "message": "You will find the PayPay Donation button in the system menu" , "read": False}
  43. cbpi.cache["messages"].append(msg)
  44. NotificationView.register(cbpi.app, route_base='/api/notification')