Skip to content

Commit

Permalink
Merge pull request #39 from nss-evening-cohort-12/completed_orders
Browse files Browse the repository at this point in the history
completed orders done
  • Loading branch information
jamalbrowning authored Feb 20, 2021
2 parents b279c8d + a58bd4c commit 97fbb97
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
48 changes: 48 additions & 0 deletions reports/templates/orders/completed_orders.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{% load static %}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Reports</title>
<style>
td {
text-align: center;
padding: 5px;
}

th {
padding: 5px;
}

table,
th,
td {
border: 1px solid black;
border-collapse: collapse;
}
</style>
</head>
<body>
<h1>All completed orders</h1>
<table>
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Payment Type</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
{% for order in completed_orders %}
<tr>
<td>{{ order.order_id }}</td>
<td>{{ order.full_name }}</td>
<td>{{ order.payment_type }}</td>
<td>{{ order.total_price }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</body>
</html>
5 changes: 3 additions & 2 deletions reports/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from django.urls import path
from .views import expensiveproduct_list, inexpensiveproduct_list
from .views import expensiveproduct_list, inexpensiveproduct_list, completedorder_list

urlpatterns = [
path('reports/expensiveproducts', expensiveproduct_list),
path('reports/inexpensiveproducts', inexpensiveproduct_list)
path('reports/inexpensiveproducts', inexpensiveproduct_list),
path('reports/completedorders', completedorder_list)
]
1 change: 1 addition & 0 deletions reports/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .connection import Connection
from .products.expensiveproducts import expensiveproduct_list
from .products.inexpensiveproducts import inexpensiveproduct_list
from .orders.completedorders import completedorder_list
57 changes: 57 additions & 0 deletions reports/views/orders/completedorders.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import sqlite3
from django.shortcuts import render
from bangazonapi.models import Product
from reports.views import Connection


def completedorder_list(request):
"""Function to build an HTML report of products over $1000"""
if request.method == 'GET':
# connect to database
with sqlite3.connect(Connection.db_path) as conn:
conn.row_factory = sqlite3.Row
db_cursor = conn.cursor()

db_cursor.execute("""
SELECT
bangazonapi_order.id AS "order",
auth_user.first_name || ' ' || auth_user.last_name as "full name",
bangazonapi_payment.merchant_name as "payment type",
SUM(price) as "total price"
FROM
bangazonapi_order
JOIN
bangazonapi_customer ON bangazonapi_order.customer_id = bangazonapi_customer.id
JOIN
auth_user ON auth_user.id = bangazonapi_customer.user_id
JOIN
bangazonapi_orderproduct ON bangazonapi_orderproduct.order_id = bangazonapi_order.id
JOIN
bangazonapi_product ON bangazonapi_product.id = bangazonapi_orderproduct.id
JOIN
bangazonapi_payment ON bangazonapi_payment.id = bangazonapi_order.payment_type_id
WHERE
bangazonapi_order.payment_type_id IS NOT NULL
GROUP BY
bangazonapi_order.id
""")

dataset = db_cursor.fetchall()

completed_orders = []

for row in dataset:
order = {}
order["order_id"] = row["order"]
order['full_name'] = row['full name']
order['payment_type'] = row['payment type']
order['total_price'] = row['total price']

completed_orders.append(order)

template = 'orders/completed_orders.html'
context = {
'completed_orders': completed_orders
}

return render(request, template, context)

0 comments on commit 97fbb97

Please sign in to comment.