Skip to content

Commit

Permalink
Small improvements in user shift account log.
Browse files Browse the repository at this point in the history
  • Loading branch information
Theophile-Madet committed Oct 20, 2023
1 parent 5fe4ee2 commit da62f7f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
10 changes: 5 additions & 5 deletions tapir/shifts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,12 +918,12 @@ def get_upcoming_shift_attendances(self):
slot__shift__start_time__gt=timezone.localtime()
).with_valid_state()

def get_account_balance(self):
def get_account_balance(self, at_date: datetime.datetime | None = None):
entries = self.user.shift_account_entries
if at_date:
entries = entries.filter(date__lte=at_date)
# Might return None if no objects, so "or 0"
return (
self.user.shift_account_entries.aggregate(balance=Sum("value"))["balance"]
or 0
)
return entries.aggregate(balance=Sum("value"))["balance"] or 0

def is_balance_ok(self):
balance = self.get_account_balance()
Expand Down
10 changes: 6 additions & 4 deletions tapir/shifts/templates/shifts/user_shift_account_log.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ <h5 class="card-header d-flex justify-content-between align-items-center">
<tr>
<th>{% translate 'Date' %}</th>
<th>{% translate 'Value' %}</th>
<th>{% translate 'Balance at date' %}</th>
<th>{% translate 'Description' %}</th>
</tr>
</thead>
<tbody>
{% for entry in entries %}
{% for entry_data in entries_data %}
<tr>
<td>{{ entry.date }}</td>
<td>{{ entry.value }}</td>
<td class="description">{{ entry.description }}</td>
<td>{{ entry_data.entry.date|date:"d.m.Y H:i" }}</td>
<td>{{ entry_data.entry.value }}</td>
<td>{{ entry_data.balance_at_date }}</td>
<td class="description">{{ entry_data.entry.description }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
17 changes: 12 additions & 5 deletions tapir/shifts/views/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def dictionary_get(dic, key):
class UserShiftAccountLog(LoginRequiredMixin, PermissionRequiredMixin, TemplateView):
template_name = "shifts/user_shift_account_log.html"

def get_target_user(self):
def get_target_user(self) -> TapirUser:
return TapirUser.objects.get(pk=self.kwargs["user_pk"])

def get_permission_required(self):
Expand All @@ -138,10 +138,17 @@ def get_permission_required(self):

def get_context_data(self, **kwargs):
context = super().get_context_data()
context["user"] = self.get_target_user()
context["entries"] = ShiftAccountEntry.objects.filter(
user=self.get_target_user()
).order_by("-date")
user = self.get_target_user()
context["user"] = user
context["entries_data"] = [
{
"entry": entry,
"balance_at_date": user.shift_user_data.get_account_balance(
at_date=entry.date
),
}
for entry in ShiftAccountEntry.objects.filter(user=user).order_by("-date")
]
return context


Expand Down

0 comments on commit da62f7f

Please sign in to comment.