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.

112 wiersze
4.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.app_config import cbpi
  6. from werkzeug.utils import secure_filename
  7. import pprint
  8. import time
  9. import os
  10. from modules.steps import Step,StepView
  11. import xml.etree.ElementTree
  12. class BeerXMLImport(FlaskView):
  13. BEER_XML_FILE = "./upload/beer.xml"
  14. @route('/', methods=['GET'])
  15. def get(self):
  16. if not os.path.exists(self.BEER_XML_FILE):
  17. self.api.notify(headline="File Not Found", message="Please upload a Beer.xml File",
  18. type="danger")
  19. return ('', 404)
  20. result = []
  21. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  22. result = []
  23. for idx, val in enumerate(e.findall('RECIPE')):
  24. result.append({"id": idx+1, "name": val.find("NAME").text})
  25. return json.dumps(result)
  26. def allowed_file(self, filename):
  27. return '.' in filename and filename.rsplit('.', 1)[1] in set(['xml'])
  28. @route('/upload', methods=['POST'])
  29. def upload_file(self):
  30. try:
  31. if request.method == 'POST':
  32. file = request.files['file']
  33. if file and self.allowed_file(file.filename):
  34. file.save(os.path.join(self.api.app.config['UPLOAD_FOLDER'], "beer.xml"))
  35. self.api.notify(headline="Upload Successful", message="The Beer XML file was uploaded succesfully")
  36. return ('', 204)
  37. return ('', 404)
  38. except Exception as e:
  39. self.api.notify(headline="Upload Failed", message="Failed to upload Beer xml", type="danger")
  40. return ('', 500)
  41. @route('/<int:id>', methods=['POST'])
  42. def load(self, id):
  43. steps = self.getSteps(id)
  44. name = self.getRecipeName(id)
  45. self.api.set_config_parameter("brew_name", name)
  46. boil_time = self.getBoilTime(id)
  47. mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
  48. mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
  49. boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
  50. boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
  51. boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
  52. # READ KBH DATABASE
  53. Step.delete_all()
  54. StepView().reset()
  55. try:
  56. for row in steps:
  57. Step.insert(**{"name": row.get("name"), "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": float(row.get("temp")), "timer": row.get("timer")}})
  58. ## Add cooking step
  59. Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": boil_time}})
  60. ## Add Whirlpool step
  61. Step.insert(**{"name": "Whirlpool", "type": "ChilStep", "config": {"timer": 15}})
  62. ##reorder chilstep after boil and whirlpool steps
  63. Step.insert(**{"name": "ChilStep", "type": "ChilStep", "config": {"timer": 15}})
  64. self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
  65. self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
  66. except Exception as e:
  67. self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
  68. return ('', 500)
  69. return ('', 204)
  70. def getRecipeName(self, id):
  71. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  72. return e.find('./RECIPE[%s]/NAME' % (str(id))).text
  73. def getBoilTime(self, id):
  74. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  75. return float(e.find('./RECIPE[%s]/BOIL_TIME' % (str(id))).text)
  76. def getSteps(self, id):
  77. e = xml.etree.ElementTree.parse(self.BEER_XML_FILE).getroot()
  78. steps = []
  79. for e in e.findall('./RECIPE[%s]/MASH/MASH_STEPS/MASH_STEP' % (str(id))):
  80. if self.api.get_config_parameter("unit", "C") == "C":
  81. temp = float(e.find("STEP_TEMP").text)
  82. else:
  83. temp = round(9.0 / 5.0 * float(e.find("STEP_TEMP").text) + 32, 2)
  84. steps.append({"name": e.find("NAME").text, "temp": temp, "timer": float(e.find("STEP_TIME").text)})
  85. return steps
  86. @cbpi.initalizer()
  87. def init(cbpi):
  88. BeerXMLImport.api = cbpi
  89. BeerXMLImport.register(cbpi.app, route_base='/api/beerxml')