No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

147 líneas
5.3KB

  1. from flask import json, request
  2. from flask_classy import FlaskView, route
  3. from git import Repo, Git
  4. import sqlite3
  5. from modules.core.core import cbpi
  6. from werkzeug.utils import secure_filename
  7. import pprint
  8. import time
  9. import os
  10. from modules.step import Step, StepView
  11. class KBH(FlaskView):
  12. @route('/', methods=['GET'])
  13. def get(self):
  14. """
  15. Get all recipes from uploaded kleinerbrauhelfer database
  16. ---
  17. tags:
  18. - kleinerbrauhelfer
  19. responses:
  20. 200:
  21. description: Recipes from kleinerbrauhelfer database
  22. """
  23. conn = None
  24. try:
  25. if not os.path.exists(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db'):
  26. self.api.notify(headline="File Not Found", message="Please upload a Kleiner Brauhelfer Database", type="danger")
  27. return ('', 404)
  28. conn = sqlite3.connect(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db')
  29. c = conn.cursor()
  30. c.execute('SELECT ID, Sudname, BierWurdeGebraut FROM Sud')
  31. data = c.fetchall()
  32. result = []
  33. for row in data:
  34. result.append({"id": row[0], "name": row[1], "brewed": row[2]})
  35. return json.dumps(result)
  36. except Exception as e:
  37. print e
  38. self.api.notify(headline="Failed to load KHB database", message="ERROR", type="danger")
  39. return ('', 500)
  40. finally:
  41. if conn:
  42. conn.close()
  43. def allowed_file(self, filename):
  44. return '.' in filename and filename.rsplit('.', 1)[1] in set(['sqlite'])
  45. @route('/upload', methods=['POST'])
  46. def upload_file(self):
  47. """
  48. Upload KleinerBrauhelfer Database File
  49. ---
  50. tags:
  51. - kleinerbrauhelfer
  52. responses:
  53. 200:
  54. description: File uploaed
  55. """
  56. try:
  57. if request.method == 'POST':
  58. file = request.files['file']
  59. if file and self.allowed_file(file.filename):
  60. filename = secure_filename(file.filename)
  61. file.save(os.path.join(self.api.app.config['UPLOAD_FOLDER'], "kbh.db"))
  62. self.api.notify(headline="Upload Successful", message="The Kleiner Brauhelfer Database was uploaded succesfully")
  63. return ('', 204)
  64. return ('', 404)
  65. except Exception as e:
  66. self.api.notify(headline="Upload Failed", message="Failed to upload Kleiner Brauhelfer", type="danger")
  67. return ('', 500)
  68. @route('/<int:id>', methods=['POST'])
  69. def load(self, id):
  70. """
  71. Load Recipe from Kleinerbrauhelfer Database
  72. ---
  73. tags:
  74. - kleinerbrauhelfer
  75. parameters:
  76. - in: path
  77. name: id
  78. schema:
  79. type: integer
  80. required: true
  81. description: Numeric ID of recipe
  82. responses:
  83. 200:
  84. description: Recipe loaded
  85. """
  86. mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
  87. mashinstep_type = cbpi.get_config_parameter("step_mashin", "MashInStep")
  88. chilstep_type = cbpi.get_config_parameter("step_chil", "ChilStep")
  89. boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
  90. mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
  91. boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
  92. boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
  93. # READ KBH DATABASE
  94. Step.delete_all()
  95. StepView().reset()
  96. conn = None
  97. try:
  98. conn = sqlite3.connect(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db')
  99. c = conn.cursor()
  100. c.execute('SELECT EinmaischenTemp, Sudname FROM Sud WHERE ID = ?', (id,))
  101. row = c.fetchone()
  102. name = row[1]
  103. self.api.set_config_parameter("brew_name", name)
  104. Step.insert(**{"name": "MashIn", "type": mashinstep_type, "config": {"kettle": mash_kettle, "temp": row[0]}})
  105. ### add rest step
  106. for row in c.execute('SELECT * FROM Rasten WHERE SudID = ?', (id,)):
  107. Step.insert(**{"name": row[5], "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": row[3], "timer": row[4]}})
  108. Step.insert(**{"name": "Chil", "type": chilstep_type, "config": {"timer": 15}})
  109. ## Add cooking step
  110. c.execute('SELECT max(Zeit) FROM Hopfengaben WHERE SudID = ?', (id,))
  111. row = c.fetchone()
  112. Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": row[0]}})
  113. ## Add Whirlpool step
  114. Step.insert(**{"name": "Whirlpool", "type": chilstep_type, "config": {"timer": 15}})
  115. #setBrewName(name)
  116. self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
  117. self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
  118. except Exception as e:
  119. self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
  120. return ('', 500)
  121. finally:
  122. if conn:
  123. conn.close()
  124. return ('', 204)
  125. @cbpi.addon.core.initializer()
  126. def init(cbpi):
  127. KBH.api = cbpi
  128. KBH.register(cbpi._app, route_base='/api/kbh')