def list_styles(): bjcp_styles = {} for cat in list(BJCPCategory.select().orderBy('category_id')): styles_for_cat = list(BJCPStyle.select(BJCPStyle.q.category==cat).orderBy('subcategory')) try: bjcp_styles[cat.category_id] except KeyError: bjcp_styles[cat.category_id] = {'name': cat.name, 'styles': styles_for_cat} return render_template('bjcp_browser.html', styles=bjcp_styles)
def list_styles(): bjcp_styles = {} for cat in list(BJCPCategory.select().orderBy('category_id')): styles_for_cat = list( BJCPStyle.select( BJCPStyle.q.category == cat).orderBy('subcategory')) try: bjcp_styles[cat.category_id] except KeyError: bjcp_styles[cat.category_id] = { 'name': cat.name, 'styles': styles_for_cat } return render_template('bjcp_browser.html', styles=bjcp_styles)
def process_bjcp_styles(): # import the XML styledoc = minidom.parse(os.path.join(data_dir, 'styleguide2008.xml')) # generate categories for beer_class in styledoc.getElementsByTagName('class'): this_class = unicode(beer_class.getAttribute('type')) for category in beer_class.getElementsByTagName('category'): nameN = category.getElementsByTagName('name')[0] name = unicode(nameN.firstChild.data) category_id = int(category.getAttribute('id')) notes = "" if category.getElementsByTagName('notes'): for note in category.getElementsByTagName('notes'): for child in note.childNodes: notes = notes + "\n\n" + child.toxml() try: this_category = BJCPCategory(name=name, category_id=category_id, notes=notes) except DuplicateEntryError: this_category = BJCPCategory.selectBy(category_id=category_id) # generate styles for this category for sc in category.getElementsByTagName('subcategory'): # we're only interested in the last letter -- # the number is duplicative data subcategory_id = unicode(sc.getAttribute('id')[-1:]).upper() # initialize the variables needed name = aroma = flavor = appearance = mouthfeel = None impression = comments = examples = None og_low = og_high = abv_high = fg_low = fg_high = srm_low = 0 srm_high = abv_low = ibu_low = ibu_high = 0 g = sc.getElementsByTagName # loop over the text nodes and set the value of the # node equal to the xml node name try: name = unicode(g('name')[0].firstChild.data) except (IndexError, AttributeError): pass try: aroma = unicode(g('aroma')[0].firstChild.data) except (IndexError, AttributeError): pass try: appearance = unicode(g('appearance')[0].firstChild.data) except (IndexError, AttributeError): pass try: flavor = unicode(g('flavor')[0].firstChild.data) except (IndexError, AttributeError): pass try: mouthfeel = unicode(g('mouthfeel')[0].firstChild.data) except (IndexError, AttributeError): pass try: impression = unicode(g('impression')[0].firstChild.data) except (IndexError, AttributeError): pass try: comments = unicode(g('comments')[0].firstChild.data) except (IndexError, AttributeError): pass try: examples = unicode(g('examples')[0].firstChild.data) except (IndexError, AttributeError): pass s = g('stats')[0].getElementsByTagName if not s('exceptions'): try: ibu_lowN = s('ibu')[0].getElementsByTagName('low')[0] ibu_low = int(ibu_lowN.firstChild.data) except (IndexError, AttributeError): pass try: ibu_highN = s('ibu')[0].getElementsByTagName('high')[0] ibu_high = int(ibu_highN.firstChild.data) except (IndexError, AttributeError): pass try: og_lowN = s('og')[0].getElementsByTagName('low')[0] og_low = float(og_lowN.firstChild.data) except (IndexError, AttributeError): pass try: og_highN = s('og')[0].getElementsByTagName('high')[0] og_high = float(og_highN.firstChild.data) except (IndexError, AttributeError): pass try: fg_lowN = s('fg')[0].getElementsByTagName('low')[0] fg_low = float(fg_lowN.firstChild.data) except (IndexError, AttributeError): pass try: fg_highN = s('fg')[0].getElementsByTagName('high')[0] fg_high = float(fg_highN.firstChild.data) except (IndexError, AttributeError): pass try: srm_lowN = s('srm')[0].getElementsByTagName('low')[0] srm_low = float(srm_lowN.firstChild.data) except (IndexError, AttributeError): pass try: srm_highN = s('srm')[0].getElementsByTagName('high')[0] srm_high = float(srm_highN.firstChild.data) except (IndexError, AttributeError): pass try: abv_lowN = s('abv')[0].getElementsByTagName('low')[0] abv_low = float(abv_lowN.firstChild.data) except (IndexError, AttributeError): pass try: abv_highN = s('abv')[0].getElementsByTagName('high')[0] abv_high = float(abv_highN.firstChild.data) except (IndexError, AttributeError): pass BJCPStyle(name = name, beer_type = this_class, category = this_category, subcategory = subcategory_id, aroma = aroma, appearance = appearance, flavor = flavor, mouthfeel = mouthfeel, impression = impression, comments = comments, examples = examples, og_low = og_low, og_high = og_high, fg_low = fg_low, fg_high = fg_high, srm_low = srm_low, srm_high = srm_high, ibu_low = ibu_low, ibu_high = ibu_high, abv_low = abv_low, abv_high = abv_high)
def process_bjcp_styles(): # import the XML styledoc = minidom.parse(os.path.join(data_dir, 'styleguide2008.xml')) # generate categories for beer_class in styledoc.getElementsByTagName('class'): this_class = unicode(beer_class.getAttribute('type')) for category in beer_class.getElementsByTagName('category'): nameN = category.getElementsByTagName('name')[0] name = unicode(nameN.firstChild.data) category_id = int(category.getAttribute('id')) notes = "" if category.getElementsByTagName('notes'): for note in category.getElementsByTagName('notes'): for child in note.childNodes: notes = notes + "\n\n" + child.toxml() try: this_category = BJCPCategory(name=name, category_id=category_id, notes=notes) except DuplicateEntryError: this_category = BJCPCategory.selectBy(category_id=category_id) # generate styles for this category for sc in category.getElementsByTagName('subcategory'): # we're only interested in the last letter -- # the number is duplicative data subcategory_id = unicode(sc.getAttribute('id')[-1:]).upper() # initialize the variables needed name = aroma = flavor = appearance = mouthfeel = None impression = comments = examples = None og_low = og_high = abv_high = fg_low = fg_high = srm_low = 0 srm_high = abv_low = ibu_low = ibu_high = 0 g = sc.getElementsByTagName # loop over the text nodes and set the value of the # node equal to the xml node name try: name = unicode(g('name')[0].firstChild.data) except (IndexError, AttributeError): pass try: aroma = unicode(g('aroma')[0].firstChild.data) except (IndexError, AttributeError): pass try: appearance = unicode(g('appearance')[0].firstChild.data) except (IndexError, AttributeError): pass try: flavor = unicode(g('flavor')[0].firstChild.data) except (IndexError, AttributeError): pass try: mouthfeel = unicode(g('mouthfeel')[0].firstChild.data) except (IndexError, AttributeError): pass try: impression = unicode(g('impression')[0].firstChild.data) except (IndexError, AttributeError): pass try: comments = unicode(g('comments')[0].firstChild.data) except (IndexError, AttributeError): pass try: examples = unicode(g('examples')[0].firstChild.data) except (IndexError, AttributeError): pass s = g('stats')[0].getElementsByTagName if not s('exceptions'): try: ibu_lowN = s('ibu')[0].getElementsByTagName('low')[0] ibu_low = int(ibu_lowN.firstChild.data) except (IndexError, AttributeError): pass try: ibu_highN = s('ibu')[0].getElementsByTagName('high')[0] ibu_high = int(ibu_highN.firstChild.data) except (IndexError, AttributeError): pass try: og_lowN = s('og')[0].getElementsByTagName('low')[0] og_low = float(og_lowN.firstChild.data) except (IndexError, AttributeError): pass try: og_highN = s('og')[0].getElementsByTagName('high')[0] og_high = float(og_highN.firstChild.data) except (IndexError, AttributeError): pass try: fg_lowN = s('fg')[0].getElementsByTagName('low')[0] fg_low = float(fg_lowN.firstChild.data) except (IndexError, AttributeError): pass try: fg_highN = s('fg')[0].getElementsByTagName('high')[0] fg_high = float(fg_highN.firstChild.data) except (IndexError, AttributeError): pass try: srm_lowN = s('srm')[0].getElementsByTagName('low')[0] srm_low = float(srm_lowN.firstChild.data) except (IndexError, AttributeError): pass try: srm_highN = s('srm')[0].getElementsByTagName('high')[0] srm_high = float(srm_highN.firstChild.data) except (IndexError, AttributeError): pass try: abv_lowN = s('abv')[0].getElementsByTagName('low')[0] abv_low = float(abv_lowN.firstChild.data) except (IndexError, AttributeError): pass try: abv_highN = s('abv')[0].getElementsByTagName('high')[0] abv_high = float(abv_highN.firstChild.data) except (IndexError, AttributeError): pass BJCPStyle(name=name, beer_type=this_class, category=this_category, subcategory=subcategory_id, aroma=aroma, appearance=appearance, flavor=flavor, mouthfeel=mouthfeel, impression=impression, comments=comments, examples=examples, og_low=og_low, og_high=og_high, fg_low=fg_low, fg_high=fg_high, srm_low=srm_low, srm_high=srm_high, ibu_low=ibu_low, ibu_high=ibu_high, abv_low=abv_low, abv_high=abv_high)