|
@@ -149,6 +149,18 @@ class QueryManager(object):
|
|
|
def insert_new_product(self, product, category, group, unit):
|
|
|
self.cursor.execute(get_insert_product_statement(product, category, group, unit))
|
|
|
|
|
|
+ def convert_unit(self, product, _from, _to):
|
|
|
+ self.cursor.execute(SQL(
|
|
|
+ "SELECT convert_unit({_from}, {_to}, {product}) AS factor"
|
|
|
+ ).format(
|
|
|
+ product=Literal(product), _from=Literal(_from), _to=Literal(_to)
|
|
|
+ ))
|
|
|
+ res = list(cursor_as_dict(self.cursor))
|
|
|
+ if len(res) == 0:
|
|
|
+ return None
|
|
|
+ assert len(res) == 1
|
|
|
+ return res[0]['factor']
|
|
|
+
|
|
|
def get_preferred_unit(self, product):
|
|
|
self.cursor.execute(SQL("""SELECT
|
|
|
units.name AS preferred_unit
|
|
@@ -159,4 +171,4 @@ WHERE products.name = {product}""").format(product=Literal(product)))
|
|
|
if len(res) == 0:
|
|
|
return None
|
|
|
assert len(res) == 1
|
|
|
- return res[0]['preferred_unit']
|
|
|
+ return res[0]['preferred_unit']
|