選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

111 行
4.1KB

  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. for idx, r in enumerate(self.getDict().get("RECIPES").get("RECIPE")):
  22. result.append({"id": idx, "name": r.get("NAME")})
  23. return json.dumps(result)
  24. def allowed_file(self, filename):
  25. return '.' in filename and filename.rsplit('.', 1)[1] in set(['xml'])
  26. @route('/upload', methods=['POST'])
  27. def upload_file(self):
  28. try:
  29. if request.method == 'POST':
  30. file = request.files['file']
  31. if file and self.allowed_file(file.filename):
  32. file.save(os.path.join(self.api.app.config['UPLOAD_FOLDER'], "beer.xml"))
  33. self.api.notify(headline="Upload Successful", message="The Beer XML file was uploaded succesfully")
  34. return ('', 204)
  35. return ('', 404)
  36. except Exception as e:
  37. self.api.notify(headline="Upload Failed", message="Failed to upload Beer xml", type="danger")
  38. return ('', 500)
  39. @route('/<int:id>', methods=['POST'])
  40. def load(self, id):
  41. recipe = self.getDict().get("RECIPES").get("RECIPE")[id]
  42. steps = recipe.get("MASH",{}).get("MASH_STEPS",{}).get("MASH_STEP",[])
  43. name = recipe.get("NAME")
  44. self.api.set_config_parameter("brew_name", name)
  45. boil_time = recipe.get("BOIL_TIME", 90)
  46. mashstep_type = cbpi.get_config_parameter("step_mash", "MashStep")
  47. mash_kettle = cbpi.get_config_parameter("step_mash_kettle", None)
  48. boilstep_type = cbpi.get_config_parameter("step_boil", "BoilStep")
  49. boil_kettle = cbpi.get_config_parameter("step_boil_kettle", None)
  50. boil_temp = 100 if cbpi.get_config_parameter("unit", "C") == "C" else 212
  51. # READ KBH DATABASE
  52. Step.delete_all()
  53. StepView().reset()
  54. conn = None
  55. try:
  56. conn = sqlite3.connect(self.api.app.config['UPLOAD_FOLDER'] + '/kbh.db')
  57. c = conn.cursor()
  58. for row in steps:
  59. Step.insert(**{"name": row.get("NAME"), "type": mashstep_type, "config": {"kettle": mash_kettle, "temp": float(row.get("STEP_TEMP")), "timer": row.get("STEP_TIME")}})
  60. Step.insert(**{"name": "ChilStep", "type": "ChilStep", "config": {"timer": 15}})
  61. ## Add cooking step
  62. Step.insert(**{"name": "Boil", "type": boilstep_type, "config": {"kettle": boil_kettle, "temp": boil_temp, "timer": boil_time}})
  63. ## Add Whirlpool step
  64. Step.insert(**{"name": "Whirlpool", "type": "ChilStep", "config": {"timer": 15}})
  65. # setBrewName(name)
  66. self.api.emit("UPDATE_ALL_STEPS", Step.get_all())
  67. self.api.notify(headline="Recipe %s loaded successfully" % name, message="")
  68. except Exception as e:
  69. self.api.notify(headline="Failed to load Recipe", message=e.message, type="danger")
  70. return ('', 500)
  71. finally:
  72. if conn:
  73. conn.close()
  74. return ('', 204)
  75. def getDict(self):
  76. '''
  77. Beer XML file to dict
  78. :return: beer.xml file as dict
  79. '''
  80. try:
  81. import xmltodict
  82. with open(self.BEER_XML_FILE) as fd:
  83. doc = xmltodict.parse(fd.read())
  84. return doc
  85. except:
  86. self.api.notify(headline="Failed to load Beer.xml", message="Please check if you uploaded an beer.xml", type="danger")
  87. @cbpi.initalizer()
  88. def init(cbpi):
  89. BeerXMLImport.api = cbpi
  90. BeerXMLImport.register(cbpi.app, route_base='/api/beerxml')