Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

90 wiersze
2.5KB

  1. import time
  2. from flask import json, request
  3. from flask_classy import route
  4. from modules.core.core import cbpi
  5. from modules.core.db import DBModel
  6. from modules.core.baseview import BaseView
  7. from modules.database.dbmodel import Config
  8. class ConfigView(BaseView):
  9. model = Config
  10. cache_key = "config"
  11. @route('/<name>', methods=["PUT"])
  12. def put(self, name):
  13. """
  14. Set new config value
  15. ---
  16. tags:
  17. - config
  18. responses:
  19. 204:
  20. description: New config value set
  21. """
  22. data = request.json
  23. data["name"] = name
  24. update_data = {"name": data["name"], "value": data["value"]}
  25. if self.api.cache.get(self.cache_key) is not None:
  26. self.api.cache.get(self.cache_key)[name].__dict__.update(**update_data)
  27. m = self.model.update(**self.api.cache.get(self.cache_key)[name].__dict__)
  28. self._post_put_callback(self.api.cache.get(self.cache_key)[name])
  29. self.api.emit("CONFIG_UPDATE", name=name, data=data["value"])
  30. return json.dumps(self.api.cache.get(self.cache_key)[name].__dict__)
  31. @route('/<id>', methods=["GET"])
  32. def getOne(self, id):
  33. """
  34. Get config parameter
  35. ---
  36. tags:
  37. - config
  38. responses:
  39. 400:
  40. description: Get one config parameter via web api is not supported
  41. """
  42. return ('NOT SUPPORTED', 400)
  43. @route('/<id>', methods=["DELETE"])
  44. def delete(self, id):
  45. """
  46. Delete config parameter
  47. ---
  48. tags:
  49. - config
  50. responses:
  51. 400:
  52. description: Deleting config parameter via web api is not supported
  53. """
  54. return ('NOT SUPPORTED', 400)
  55. @route('/', methods=["POST"])
  56. def post(self):
  57. """
  58. Get config parameter
  59. ---
  60. tags:
  61. - config
  62. responses:
  63. 400:
  64. description: Adding new config parameter via web api is not supported
  65. """
  66. return ('NOT SUPPORTED', 400)
  67. @classmethod
  68. def init_cache(cls):
  69. with cls.api._app.app_context():
  70. cls.api.cache[cls.cache_key] = {}
  71. for key, value in cls.model.get_all().iteritems():
  72. cls.post_init_callback(value)
  73. cls.api.cache[cls.cache_key][value.name] = value
  74. @cbpi.addon.core.initializer(order=0)
  75. def init(cbpi):
  76. ConfigView.register(cbpi._app, route_base='/api/config')
  77. ConfigView.init_cache()