Skip to content

Commit

Permalink
Show a summary of most worn tshirts on homepage
Browse files Browse the repository at this point in the history
There can be an interesting difference between all-time-most-worn
and recently-most-worn, so show both, where 'recently' is roughly
six months.
  • Loading branch information
norm committed Aug 2, 2021
1 parent 572a2d4 commit 7edafba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
14 changes: 13 additions & 1 deletion hasworn/wearers/models.py
@@ -1,4 +1,4 @@
from datetime import date
from datetime import date, timedelta
from django.contrib.auth.models import AbstractUser
from django.core import validators
from django.db import models
Expand Down Expand Up @@ -74,6 +74,18 @@ def most_worn(self):
num_worn = models.Count('days_worn')
).order_by('-num_worn')

def most_worn_recently(self, cut_off=None):
if not cut_off:
cut_off = date.today() - timedelta(days=180)
# FIXME also order by date first worn
return self.worn_set.filter(
days_worn__day__gte = cut_off
).annotate(
num_worn=models.Count('days_worn')
).filter(
num_worn__gt = 1
).order_by('-num_worn')

def years_active(self):
return [ year.year for year in self.wearings.dates('day', 'year') ]

Expand Down
17 changes: 17 additions & 0 deletions templates/wearers/base.html
Expand Up @@ -3,6 +3,23 @@

</head>
<body>
{% block header %}
<h1>{{wearer}}</h1>

<p>
<a href='/'>{{wearer.get_name}}</a> has worn
<a href='/tshirts/'>{{wearer.has_worn.count}} different tshirts</a>
{{wearer.wearings.count}} times, in
{% for year in wearer.years_active %}
{% if forloop.last %}
{% if not forloop.first %} and {% endif %}
<a href='/{{year}}/'>{{year}}</a>.
{% else %}
<a href='/{{year}}/'>{{year}}</a>,
{% endif %}
{% endfor %}
</p>
{% endblock %}
{% block content %}
{% endblock %}
</body>
Expand Down
14 changes: 9 additions & 5 deletions templates/wearers/wearer_page.html
@@ -1,17 +1,21 @@
{% extends "wearers/base.html" %}

{% block content %}
<h1>{{wearer}}</h1>
<h2>recent</h2>
<h2>most recently worn</h2>
{% for wearing in wearer.wearings.all|slice:':10' %}
<li>
<a href='{{wearing.clothing.static_site_url}}'>{{wearing.clothing}}</a>
on {{wearing.day}}
</li>
{% endfor %}

<h2>has worn</h2>
{% for clothing in wearer.has_worn.all %}
<li><a href='{{clothing.static_site_url}}'>{{clothing}}</a></li>
<h2>most worn in the last six months</h2>
{% for worn in wearer.most_worn_recently|slice:':12' %}
<li><a href='{{worn.clothing.static_site_url}}'>{{worn.clothing}}</a> worn {{worn.num_worn}} times</li>
{% endfor %}

<h2>most worn of all all time</h2>
{% for worn in wearer.most_worn|slice:':12' %}
<li><a href='{{worn.clothing.static_site_url}}'>{{worn.clothing}}</a> worn {{worn.num_worn}} times</li>
{% endfor %}
{% endblock %}

0 comments on commit 7edafba

Please sign in to comment.