bia-fitness/lifting/views.py

90 lines
3.0 KiB
Python
Raw Normal View History

2015-04-03 20:46:59 +00:00
import datetime
import os
2015-04-02 21:58:26 +00:00
import tempfile
2015-04-03 20:46:59 +00:00
import calendar
from collections import defaultdict
2015-04-02 21:58:26 +00:00
2015-03-31 21:25:19 +00:00
from django.shortcuts import render
2015-04-01 22:19:20 +00:00
from django import forms
2015-04-02 21:58:26 +00:00
from django.contrib.auth.decorators import login_required
2015-04-03 20:46:59 +00:00
from django.views.generic import dates
2015-04-07 21:33:05 +00:00
from django.db.models import Count, Max
2015-04-02 21:58:26 +00:00
2015-04-10 18:19:12 +00:00
from .models import Set
from inventory.models import Lift
2015-04-03 20:46:59 +00:00
@login_required
2015-04-06 17:51:33 +00:00
def month_lifts(request, year, month):
2015-04-03 20:46:59 +00:00
year, month = int(year), int(month)
2015-04-03 21:09:38 +00:00
sets_by_day = defaultdict(set)
2015-04-03 20:46:59 +00:00
for workset in Set.objects.filter(user=request.user, date__year=year, date__month=month):
2015-04-10 18:19:12 +00:00
sets_by_day[workset.date.day].add(workset.lift)
2015-04-03 20:46:59 +00:00
date = datetime.date(year, month, 1)
2015-04-06 16:00:32 +00:00
# build calendar
2015-04-03 20:46:59 +00:00
first_day, max_days = calendar.monthrange(year, month)
2015-04-06 16:00:32 +00:00
# alter first_day to use 0 for sunday
2015-04-03 20:46:59 +00:00
first_day = (first_day + 1) % 7
2015-04-06 16:00:32 +00:00
# start calendar with a few blank days, then put days into array
2015-04-03 20:46:59 +00:00
days = [None]*first_day
2015-04-03 21:19:11 +00:00
for day in range(1, max_days+1):
2015-04-07 21:40:30 +00:00
days.append({'number': day, 'lifts': sets_by_day[day]})
2015-04-03 20:46:59 +00:00
2015-04-06 16:00:32 +00:00
# split days up into weeks
2015-04-03 21:19:11 +00:00
days_by_week = [days[0:7], days[7:14], days[14:21], days[21:28], days[28:35], days[35:42]]
2015-04-03 20:46:59 +00:00
2015-04-06 15:43:36 +00:00
# prev and next month
if date.month == 1:
prev_date = datetime.date(year-1, 12, 1)
next_date = datetime.date(year, 2, 1)
elif date.month == 12:
prev_date = datetime.date(year, 11, 1)
next_date = datetime.date(year+1, 1, 1)
else:
prev_date = datetime.date(year, month-1, 1)
next_date = datetime.date(year, month+1, 1)
2015-04-06 16:00:32 +00:00
return render(request, 'lifting/month.html', {'date': date, 'days': days_by_week,
'prev_date': prev_date, 'next_date': next_date
})
2015-03-31 21:25:19 +00:00
2015-04-06 17:51:33 +00:00
@login_required
def day_lifts(request, year, month, day):
year, month, day = int(year), int(month), int(day)
sets = list(Set.objects.filter(user=request.user, date__year=year, date__month=month,
date__day=day))
date = datetime.date(year, month, day)
prev_date = date - datetime.timedelta(days=1)
next_date = date + datetime.timedelta(days=1)
return render(request, 'lifting/day.html', {'date': date, 'sets': sets,
'prev_date': prev_date, 'next_date': next_date
})
2015-04-01 22:19:20 +00:00
2015-04-07 21:33:05 +00:00
@login_required
def lift_list(request):
2015-04-10 18:19:12 +00:00
lifts = Lift.objects.filter(sets__user=request.user).annotate(
2015-04-07 21:33:05 +00:00
total=Count('sets'), max_kg=Max('sets__weight_kg'),
last_date=Max('sets__date'),
2015-04-07 21:40:30 +00:00
).order_by('-last_date')
2015-04-07 21:33:05 +00:00
return render(request, 'lifting/lift_list.html', {'lifts': lifts})
@login_required
def by_lift(request, lift_id):
2015-04-10 18:19:12 +00:00
lift = Lift.objects.get(pk=lift_id)
sets = Set.objects.filter(user=request.user, lift=lift).order_by('-date')
2015-04-08 15:17:37 +00:00
return render(request, 'lifting/by_lift.html', {'lift': lift, 'sets': sets})
2015-04-07 21:33:05 +00:00
2015-04-02 21:58:26 +00:00
@login_required
2015-04-10 18:19:12 +00:00
def edit_profile(request):
form = request.user.profile
return render(request, 'profiles/edit.html', {'form': form})