55. Manage store page functionalities

Next, we will implement the functionalities available to members of the management team on our website.

The functionalities will include:

  • Sales reports

  • Products sold

  • Total orders

  • Exporting reports (including clients, addresses, and orders) as CSV files for further analysis

To achieve this:

  1. Edit the manage_store.html file to display total sales, products sold, and total orders.

  2. Implement functionality to export reports on orders, clients, and addresses as CSV files for analysis by the management team.

manage_store.html
{% extends 'base.html' %}
{% load static %}

{% block body %}

<h3>
    Manage store
</h3>

<p>Sales: {{ sales }}</p>
<p>Products Sold: {{ products_sold }}</p>
<p>Total Orders: {{ total_orders }}</p>

<hr>
<h3>Export Reports</h3>

<a href="{% url 'export_report' 'orders' %}">Export Orders</a> <!--In '' because we want to pass a text as parameter-->
<a href="{% url 'export_report' 'client' %}">Export Clients</a>
<a href="{% url 'export_report' 'address' %}">Export Addresses</a>

{% endblock %}

Next, update the manage_store view function to retrieve data from completed orders and pass this information to the manage_store.html file using the context variable.

views.py
@login_required
def manage_store(request):
    if request.user.groups.filter(name="Team").exists():
        
        orders_finished = Order.objects.filter(finished=True)
        total_orders = len(orders_finished)
        sales = sum(order.total_cost for order in orders_finished)
        products_sold = sum(order.total_quantity for order in orders_finished)
        context = {"total_orders": total_orders, "products_sold": products_sold, "sales": sales}
        return render(request,"internal/manage_store.html", context=context)
    else:
        redirect('store')

Finally, we will create a new view function called export_report to handle the process of exporting reports to CSV. This export option will direct users to a new page dedicated to report generation.

views.py
@login_required
def export_report(request, report):
    print(report)
    return redirect('manage_store')

Since we are adding a new page, update the urls.py file to include a reference to it.

urls.py
path('exportreport/<str:report>/', export_report, name="export_report"),

On the manage_store page, you will now see all the report information and links to export the reports. Currently, clicking these links will display their names in the console.

Remember to be logged into a account of the Team group

Last updated