~cypheon/trakka

ref: a14c69be685a87d332f954572143907fe63e2acb trakka/activities/views.py -rw-r--r-- 3.6 KiB
a14c69be — Johann Rudloff Implement saving health measurements 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.core.exceptions import RequestDataTooBig
from django.views.decorators.csrf import csrf_exempt

from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User

from django import forms

from .models import Activity, HealthData
from .services import UPLOAD_MAX_SIZE, create_activity
from .tasks import process_update_activity
from . import graph

import datetime
import hashlib
import logging

logger = logging.getLogger(__name__)

class UploadFileForm(forms.Form):
    title = forms.CharField(max_length=1024, required=False)
    content = forms.FileField()

class HealthMeasurementForm(forms.Form):
    body_weight = forms.FloatField(required=False)
    body_height = forms.FloatField(required=False)
    min_hr = forms.IntegerField(required=False)
    max_hr = forms.IntegerField(required=False)
    measurement_date = forms.DateField()
    measurement_time = forms.TimeField()

@login_required
def index(request):
    return render(request, 'upload.html')

@login_required
def profile(request):
    return render(request, 'profile.html')

@login_required
def health(request):
    return render(request, 'health.html')

def save_healthdata(user, time, typ, value):
    if value is not None:
        m = HealthData(user=user, measurement_ts=time, measurement_type=typ, measurement_value=value)
        m.save()

@login_required
def health_measurement(request):
    if request.method == 'POST':
        form = HealthMeasurementForm(request.POST)
        if form.is_valid():
            t = datetime.datetime.combine(form.cleaned_data['measurement_date'], form.cleaned_data['measurement_time'])
            save_healthdata(request.user, t, 'WEIGHT', form.cleaned_data.get('body_weight'))
            save_healthdata(request.user, t, 'HEIGHT', form.cleaned_data.get('body_height'))
            save_healthdata(request.user, t, 'RESTING_HR', form.cleaned_data.get('min_hr'))
            save_healthdata(request.user, t, 'MAX_HR', form.cleaned_data.get('max_hr'))
    return redirect('activities:health')

def schedule(request):
    unprocessed = Activity.objects.filter(status='PENDING').all()[:1000]
    for act in unprocessed:
        process_update_activity.delay(act.id)

    return HttpResponse('done')

@login_required
def overview(request):
    user_activities = request.user.activity_set.order_by('-start_date')
    context = {'activities': user_activities}
    return render(request, 'overview.html', context)

@login_required
def summary(request, activity_id):
    act = request.user.activity_set.get(id=activity_id)
    context = {'act': act}
    return render(request, 'details.html', context)

@login_required
def graph_hr_zones(request, activity_id):
    act = request.user.activity_set.get(id=activity_id)
    return HttpResponse(graph.hr_zones(act), content_type='image/svg+xml')

@login_required
def upload(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            uploaded = request.FILES['content']
            if uploaded.size > UPLOAD_MAX_SIZE:
                raise RequestDataTooBig()
            filename = uploaded.name
            body = b''
            for chunk in uploaded.chunks():
                body += chunk
            title = request.POST['title']
            act = create_activity(request.user, title, body)
            act.orig_upload.save()
            act.save()
            process_update_activity.delay(act.id)
            return HttpResponse('upload OK')
        else:
            print(f'form invalid: {form.errors}')
    else:
        return render(request, 'upload.html')