Skip to content

Commit

Permalink
stregreport: Add all categories to overview (#532)
Browse files Browse the repository at this point in the history
* Add method to convert category into product IDs

* Replace hardcoded values in template and views

Removed product IDs, and removed the wet HTML with templating.

* Pythonic

* Fix black

* Rename report-menu entries

* Reorder sales reporting menu entries by usefulness

* Remove unnecessary check

* Add documentation to fjule_party

* fixup! Add documentation to fjule_party

* Move local variables to clarify use in template

* Revert "Pythonic"

This reverts commit aac3ee0.

* Test stregreport category ranks
  • Loading branch information
krestenlaust authored Jan 15, 2025
1 parent 2524a13 commit acf6b47
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 130 deletions.
32 changes: 32 additions & 0 deletions stregreport/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from stregreport.models import BreadRazzia
from freezegun import freeze_time

from stregsystem.models import Category


class ParseIdStringTests(TestCase):
def test_parse_id_string_success(self):
Expand All @@ -26,6 +28,36 @@ def test_parse_id_string_unicode(self):
self.assertSequenceEqual([11, 13], res)


class CategoryRankReportTests(TestCase):
fixtures = ["initial_data"]

def setUp(self):
Category.objects.all().delete()

def test_no_categories(self):
self.client.login(username="tester", password="treotreo")
response = self.client.get(reverse("report_categoryranks"), {}, follow=True)

self.assertEqual(response.status_code, 200)
self.assertTemplateUsed("admin/stregsystem/report/ranks.html")

self.assertEqual(len(response.context['stat_lists']), 0)

def test_single_category_shown(self):
self.client.login(username="tester", password="treotreo")
catA = Category.objects.create(name="Category A")
catA.save()

response = self.client.get(reverse("report_categoryranks"), {}, follow=True)

self.assertEqual(response.status_code, 200)
self.assertTemplateUsed("admin/stregsystem/report/ranks.html")

stat_lists = response.context['stat_lists']

self.assertEqual(stat_lists[0][0], catA.name)


class SalesReportTests(TestCase):
fixtures = ["initial_data"]

Expand Down
2 changes: 1 addition & 1 deletion stregreport/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
re_path(r'^admin/stregsystem/razzia/wizard_guide/$', views.razzia_wizard),
re_path(r'^admin/stregsystem/razzia/wizard/$', views.razzia_view, name="razzia_view"),
re_path(r'^admin/stregsystem/report/sales/$', views.sales, name="salesreporting"),
re_path(r'^admin/stregsystem/report/ranks/$', views.ranks),
re_path(r'^admin/stregsystem/report/ranks/$', views.ranks, name="report_categoryranks"),
re_path(r'^admin/stregsystem/report/daily/$', views.daily),
re_path(r'^admin/stregsystem/report/ranks/(?P<year>\d+)$', views.ranks),
re_path(r'^admin/stregsystem/report/$', views.reports),
Expand Down
59 changes: 13 additions & 46 deletions stregreport/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,61 +303,24 @@ def sales_product(request, ids, from_time, to_time, error=None):
# both at 10 o'clock
@permission_required("stregsystem.access_sales_reports")
def ranks_for_year(request, year):
if year <= 1900 or year > 9999:
return render(request, 'admin/stregsystem/report/error_ranksnotfound.html', locals())
milk = [2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 17, 18, 19, 20, 24, 25, 43, 44, 45, 1865]
caffeine = [11, 12, 30, 34, 37, 1787, 1790, 1791, 1795, 1799, 1800, 1803, 1804, 1837, 1864]
beer = [
13,
14,
29,
42,
47,
54,
65,
66,
1773,
1776,
1777,
1779,
1780,
1783,
1793,
1794,
1807,
1808,
1809,
1820,
1822,
1840,
1844,
1846,
1847,
1853,
1855,
1856,
1858,
1859,
]
coffee = [32, 35, 36, 39]
vitamin = [1850, 1851, 1852, 1863, 1880]

FORMAT = '%d/%m/%Y kl. %H:%M'
last_year = year - 1
next_year = year + 1
from_time = fjule_party(year - 1)
to_time = fjule_party(year)

kr_stat_list = sale_money_rank(from_time, to_time)
beer_stat_list = sale_product_rank(beer, from_time, to_time)
caffeine_stat_list = sale_product_rank(caffeine, from_time, to_time)
milk_stat_list = sale_product_rank(milk, from_time, to_time)
coffee_stat_list = sale_product_rank(coffee, from_time, to_time)
vitamin_stat_list = sale_product_rank(vitamin, from_time, to_time)

stat_lists = []
for cat in Category.objects.all():
stat_lists.append((cat.name, sale_product_rank(get_product_ids_from_category(cat), from_time, to_time)))

from_time_string = from_time.strftime(FORMAT)
to_time_string = to_time.strftime(FORMAT)
current_date = timezone.now()
show_next_year = year < current_date.year
is_ongoing = current_date > from_time and current_date <= to_time
last_year = year - 1
next_year = year + 1

return render(request, 'admin/stregsystem/report/ranks.html', locals())


Expand All @@ -383,6 +346,10 @@ def sale_money_rank(from_time, to_time, rank_limit=10):
return stat_list


def get_product_ids_from_category(category):
return category.product_set.values_list('id', flat=True)


# year of the last fjuleparty
def last_fjule_party_year():
current_date = timezone.now()
Expand Down
84 changes: 5 additions & 79 deletions stregsystem/templates/admin/stregsystem/report/ranks.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ <h1>Rangeringer for {{year}}</h1>
{% endif %}
<center>
<div id="statscontainer" style="margin-top: 12px; width: 1200px; float: left;">
<div id="stats" style="width: 200px; float: left;">
{% for stat_list in stat_lists %}
<div class="stats" style="width: 200px; float: left;">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th valign="top" colspan="3">Øl</th>
<th valign="top" colspan="3">{{stat_list.0}}</th>
</tr>
<tr>
<th>#</th>
<th>Bruger</th>
<th>Antal</th>
</tr>
{% for stat in beer_stat_list %}
{% for stat in stat_list.1 %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{stat.username}}</td>
Expand All @@ -34,82 +35,7 @@ <h1>Rangeringer for {{year}}</h1>
{% endfor %}
</table>
</div>
<div id="stats2" style="width: 200px; float: left;">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th valign="top" colspan="3">Koffein</th>
</tr>
<tr>
<th>#</th>
<th>Bruger</th>
<th>Antal</th>
</tr>
{% for stat in caffeine_stat_list %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{stat.username}}</td>
<td>{{stat.sale__count}}</td>
</tr>
{% endfor %}
</table>
</div>
<div id="stats3" style="width: 200px; float: left;">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th valign="top" colspan="3">Mælkeprodukter</th>
</tr>
<tr>
<th>#</th>
<th>Bruger</th>
<th>Antal</th>
</tr>
{% for stat in milk_stat_list %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{stat.username}}</td>
<td>{{stat.sale__count}}</td>
</tr>
{% endfor %}
</table>
</div>
<div id="stats4" style="width: 200px; float: left;">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th valign="top" colspan="3">Kaffe</th>
</tr>
<tr>
<th>#</th>
<th>Bruger</th>
<th>Antal</th>
</tr>
{% for stat in coffee_stat_list %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{stat.username}}</td>
<td>{{stat.sale__count}}</td>
</tr>
{% endfor %}
</table>
</div>
<div id="stats5" style="width: 200px; float: left;">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th valign="top" colspan="3">Vitaminvand</th>
</tr>
<tr>
<th>#</th>
<th>Bruger</th>
<th>Antal</th>
</tr>
{% for stat in vitamin_stat_list %}
<tr>
<td>{{forloop.counter}}</td>
<td>{{stat.username}}</td>
<td>{{stat.sale__count}}</td>
</tr>
{% endfor %}
</table>
</div>
{% endfor %}
<div id="stats6" style="width: 200px; float: left;">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
Expand Down
8 changes: 4 additions & 4 deletions stregsystem/templates/shared_report_lists.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@
<table>
<caption><a href="/admin/stregsystem/report/" class="section">{% blocktrans with app.name as name %}Rapporter{% endblocktrans %}</a></caption>
<tr>
<th scope="row"><a href = "/admin/stregsystem/report/ranks/">Købs- og forbrugsrangeringer</a></th>
<th scope="row"><a href = "/admin/stregsystem/report/daily/">Daglig omsætning</a></th>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><a href = "/admin/stregsystem/report/sales/">Salgsrapporteringer</a></th>
<th scope="row"><a href = "/admin/stregsystem/report/ranks/">Kategorirangeringer</a></th>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><a href = "/admin/stregsystem/report/daily/">Daglig rapportering</a></th>
<th scope="row"><a href = "/admin/stregsystem/report/categories/">Bruger køb i udvalgte kategorier</a></th>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<th scope="row"><a href = "/admin/stregsystem/report/categories/">Bruger køb i kategorier</a></th>
<th scope="row"><a href = "/admin/stregsystem/report/sales/">Salgsrapporteringer for udvalgte produkter</a></th>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
Expand Down

0 comments on commit acf6b47

Please sign in to comment.