~cypheon/trakka

ref: e30fe514a2dd8f4e81d8cf3910e604f8faffa1a0 trakka/activities/views.py -rw-r--r-- 2.4 KiB
e30fe514 — Johann Rudloff Take climbed meters into account when estimating energy expenditure 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
from django.http import HttpResponse
from django.shortcuts import 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
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()

@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 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')