diff --git a/apps/calendar/app.py b/apps/calendar/app.py index aaccae7..7a958fa 100644 --- a/apps/calendar/app.py +++ b/apps/calendar/app.py @@ -1,6 +1,7 @@ from aiohttp import web import aiohttp_security import datetime +import aiomysql import security @@ -16,9 +17,10 @@ def datetime_format(postdata): def init_app(app): async def db_query(query, params=()): - await app['cur'].execute(query, params) - q = app['cur'].mogrify(query, params) - r = await app['cur'].fetchall() + cursor = await app['con'].cursor(aiomysql.DictCursor) + await cursor.execute(query, params) + q = cursor.mogrify(query, params) + r = await cursor.fetchall() result = [{k: (str(v) if isinstance(v, datetime.date) or isinstance(v, datetime.timedelta) else v) for k, v in row.items()} for row in r] return (q, result) diff --git a/apps/calendar/config/secret.toml.example b/apps/calendar/config/secret.toml.example new file mode 100644 index 0000000..9ce5b75 --- /dev/null +++ b/apps/calendar/config/secret.toml.example @@ -0,0 +1,4 @@ +[database] +db = "calendar" +user = "calendaruser" +password = "Cal@dm1n" \ No newline at end of file diff --git a/apps/reporter/queries.py b/apps/reporter/queries.py index bbabd43..ec6315c 100644 --- a/apps/reporter/queries.py +++ b/apps/reporter/queries.py @@ -16,24 +16,24 @@ async def orders_by_employee(cursor, options): query = """\ SELECT - CONCAT(E.firstname, ' ', E.lastname) AS name, - O.orderid, - O.orderdate, - C.customername, + CONCAT(E.FirstName, ' ', E.LastName) AS name, + O.OrderID, + O.OrderDate, + C.CustomerName, inter.value - FROM employees AS E - INNER JOIN orders AS O ON E.employeeid = O.employeeid - LEFT JOIN customers AS C on O.customerid = C.customerid + FROM Employees AS E + INNER JOIN Orders AS O ON E.EmployeeID = O.EmployeeID + LEFT JOIN Customers AS C on O.CustomerID = C.CustomerID INNER JOIN ( SELECT - OD.orderid, - SUM(OD.quantity * P.price) AS value - FROM orderdetails AS OD - LEFT JOIN products AS P ON OD.productid = P.productid - WHERE P.categoryid IN ({}) - GROUP BY OD.orderid - ) AS inter ON o.orderid = inter.orderid - WHERE O.orderdate BETWEEN %s AND %s;""".format(",".join(['%s'] * len(options["category"]))) + OD.OrderID, + SUM(OD.Quantity * P.Price) AS value + FROM OrderDetails AS OD + LEFT JOIN Products AS P ON OD.ProductID = P.ProductID + WHERE P.CategoryID IN ({}) + GROUP BY OD.OrderID + ) AS inter ON O.OrderID = inter.OrderID + WHERE O.OrderDate BETWEEN %s AND %s;""".format(",".join(['%s'] * len(options["category"]))) await cursor.execute(query, [*options["category"].keys(), options["date"]["from"], options["date"]["to"]]) rows = await cursor.fetchall() @@ -68,22 +68,22 @@ async def orders_by_location(cursor, options): query = """\ SELECT {}, - C.customername, - O.orderid, - O.orderdate, + C.CustomerName, + O.OrderID, + O.OrderDate, inter.value - FROM orders AS O - LEFT JOIN customers AS C on O.customerid = C.customerid + FROM Orders AS O + LEFT JOIN Customers AS C ON O.CustomerID = C.CustomerID INNER JOIN ( SELECT - OD.orderid, - SUM(OD.quantity * P.price) AS value - FROM orderdetails AS OD - LEFT JOIN products AS P ON OD.productid = P.productid - WHERE P.categoryid IN ({}) - GROUP BY OD.orderid - ) AS inter ON o.orderid = inter.orderid - WHERE O.orderdate BETWEEN %s AND %s;\ + OD.OrderID, + SUM(OD.quantity * P.Price) AS value + FROM OrderDetails AS OD + LEFT JOIN Products AS P ON OD.ProductID = P.ProductID + WHERE P.CategoryID IN ({}) + GROUP BY OD.OrderID + ) AS inter ON O.OrderID = inter.OrderID + WHERE O.OrderDate BETWEEN %s AND %s;\ """.format(location_query[options["location"]], ",".join(['%s'] * len(options["category"]))) await cursor.execute(query, [*options["category"].keys(), options["date"]["from"], options["date"]["to"]]) @@ -111,18 +111,18 @@ async def orders_by_product(cursor, options): query = """\ SELECT - P.productname, - C.customername, - O.orderid, - O.orderdate, - OD.quantity, - OD.quantity * P.price AS value - FROM orderdetails AS OD - LEFT JOIN orders AS O ON OD.orderid = O.orderid - LEFT JOIN products AS P ON OD.productid = P.productid - LEFT JOIN customers AS C ON O.customerid = C.customerid - WHERE P.categoryid IN ({}) - AND O.orderdate BETWEEN %s AND %s;\ + P.ProductName, + C.CustomerName, + O.OrderID, + O.OrderDate, + OD.Quantity, + OD.Quantity * P.Price AS value + FROM OrderDetails AS OD + LEFT JOIN Orders AS O ON OD.OrderID = O.OrderID + LEFT JOIN Products AS P ON OD.ProductID = P.ProductID + LEFT JOIN Customers AS C ON O.CustomerID = C.CustomerID + WHERE P.CategoryID IN ({}) + AND O.OrderDate BETWEEN %s AND %s;\ """.format(",".join(['%s'] * len(options["category"]))) await cursor.execute(query, [*options["category"].keys(), options["date"]["from"], options["date"]["to"]]) @@ -151,19 +151,19 @@ async def orders_by_supplier(cursor, options): query = """\ SELECT S.suppliername, - P.productname, - C.customername, - O.orderid, - O.orderdate, - OD.quantity, - OD.quantity * P.price AS value - FROM orderdetails AS OD - LEFT JOIN orders AS O ON OD.orderid = O.orderid - LEFT JOIN products AS P ON OD.productid = P.productid - LEFT JOIN customers AS C ON O.customerid = C.customerid - LEFT JOIN suppliers AS S ON P.supplierid = S.supplierid - WHERE P.categoryid IN ({}) - AND O.orderdate BETWEEN %s AND %s;\ + P.ProductName, + C.CustomerName, + O.OrderID, + O.OrderDate, + OD.Quantity, + OD.Quantity * P.Price AS value + FROM OrderDetails AS OD + LEFT JOIN Orders AS O ON OD.OrderID = O.OrderID + LEFT JOIN Products AS P ON OD.ProductID = P.ProductID + LEFT JOIN Customers AS C ON O.CustomerID = C.CustomerID + LEFT JOIN Suppliers AS S ON P.SupplierID = S.SupplierID + WHERE P.CategoryID IN ({}) + AND O.OrderDate BETWEEN %s AND %s;\ """.format(",".join(['%s'] * len(options["category"]))) await cursor.execute(query, [*options["category"].keys(), options["date"]["from"], options["date"]["to"]]) @@ -207,40 +207,40 @@ async def pivot_table(cursor, options): # SELECT, GROUP BY, JOIN parts = { 'none' : ("", "", ""), - 'month' : ("CONCAT(YEAR(o.orderdate), '-', LPAD(MONTH(o.orderdate), 2, '0')) AS month", "month", ""), - 'year' : ("YEAR(o.orderdate) AS year", "year", ""), - 'employee': ("CONCAT(E.firstname, ' ', E.lastname) AS employeename", + 'month' : ("CONCAT(YEAR(O.OrderDate), '-', LPAD(MONTH(O.OrderDate), 2, '0')) AS month", "month", ""), + 'year' : ("YEAR(O.OrderDate) AS year", "year", ""), + 'employee': ("CONCAT(E.FirstName, ' ', E.LastName) AS employeename", "employeename", - "LEFT JOIN employees AS E ON O.employeeid = E.employeeid"), - 'shipper' : ("SH.shippername", - "SH.shippername", - "LEFT JOIN shippers AS SH ON O.shipperid = SH.shipperid"), - 'customer': ("C.customername", - "C.customername", - "LEFT JOIN customers AS C ON O.customerid = C.customerid"), - 'supplier': ("S.suppliername", - "S.suppliername", - "LEFT JOIN suppliers AS S ON P.supplierid = S.supplierid"), - 'category': ("CA.categoryname", - "CA.categoryname", - "LEFT JOIN categories AS CA ON P.categoryid = CA.categoryid"), - 'product' : ("P.productname", "P.productname", "") + "LEFT JOIN Employees AS E ON O.EmployeeID = E.EmployeeID"), + 'shipper' : ("SH.ShipperName", + "SH.ShipperName", + "LEFT JOIN Shippers AS SH ON O.ShipperID = SH.ShipperID"), + 'customer': ("C.CustomerName", + "C.CustomerName", + "LEFT JOIN Customers AS C ON O.CustomerID = C.CustomerID"), + 'supplier': ("S.SupplierName", + "S.SupplierName", + "LEFT JOIN Suppliers AS S ON P.SupplierID = S.SupplierID"), + 'category': ("CA.CategoryName", + "CA.CategoryName", + "LEFT JOIN Categories AS CA ON P.CategoryID = CA.CategoryID"), + 'product' : ("P.ProductName", "P.ProductName", "") } values = { - 'sales' : 'SUM(OD.quantity * P.price)', - 'qty' : 'SUM(OD.quantity)' + 'sales' : 'SUM(OD.Quantity * P.Price)', + 'qty' : 'SUM(OD.Quantity)' } query = """ SELECT {rows} {columns} {values} - FROM orderdetails AS OD - LEFT JOIN orders AS O ON OD.orderid = O.orderid - LEFT JOIN products AS P ON OD.productid = P.productid + FROM OrderDetails AS OD + LEFT JOIN Orders AS O ON OD.OrderID = O.OrderID + LEFT JOIN Products AS P ON OD.ProductID = P.ProductID {joins} - WHERE P.categoryid IN ({categories}) - AND O.orderdate BETWEEN %s AND %s + WHERE P.CategoryID IN ({categories}) + AND O.OrderDate BETWEEN %s AND %s GROUP BY {grouprows}{groupcols} """.format( rows=f"{parts[options['rows']][0]},", diff --git a/templates/home.html b/templates/home.html index fea4384..70be85e 100644 --- a/templates/home.html +++ b/templates/home.html @@ -146,7 +146,7 @@ - +