|
1
|
|
|
# coding: utf-8 |
|
2
|
|
|
import json |
|
3
|
|
|
|
|
4
|
|
|
from dj_diabetes.forms.base import UserProfileForm |
|
5
|
|
|
from dj_diabetes.models import UserProfile |
|
6
|
|
|
from dj_diabetes.models.glucoses import Glucoses |
|
7
|
|
|
|
|
8
|
|
|
from django.contrib.auth import logout |
|
9
|
|
|
from django.contrib.auth.decorators import login_required |
|
10
|
|
|
from django.urls import reverse_lazy, reverse |
|
11
|
|
|
from django.http import HttpResponseRedirect, HttpResponse |
|
12
|
|
|
from django.views.generic import UpdateView |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
def logout_view(request): |
|
16
|
|
|
""" |
|
17
|
|
|
logout the user then redirect him to the home page |
|
18
|
|
|
""" |
|
19
|
|
|
logout(request) |
|
20
|
|
|
return HttpResponseRedirect(reverse('home')) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
def round_value(value): |
|
24
|
|
|
if value: |
|
25
|
|
|
return round(float(value), 1) |
|
26
|
|
|
else: |
|
27
|
|
|
return 0 |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
@login_required |
|
31
|
|
|
def chart_data_json(request): |
|
32
|
|
|
data = {} |
|
33
|
|
|
data['chart_data'] = ChartData.get_datas() |
|
34
|
|
|
return HttpResponse(json.dumps(data), content_type='application/json') |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
class ChartData(object): |
|
38
|
|
|
|
|
39
|
|
|
@classmethod |
|
40
|
|
|
def get_datas(cls): |
|
41
|
|
|
glucose_data = Glucoses.objects.all().order_by('-date_glucoses')[:14] |
|
42
|
|
|
|
|
43
|
|
|
data = {'date_glucoses': [], 'glucose': []} |
|
44
|
|
|
for g in glucose_data: |
|
45
|
|
|
data['date_glucoses'].append(g.date_glucoses.strftime('%m/%d')) |
|
46
|
|
|
data['glucose'].append(round_value(g.glucose)) |
|
47
|
|
|
|
|
48
|
|
|
return data |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
class UserProfileUpdateView(UpdateView): |
|
52
|
|
|
""" |
|
53
|
|
|
|
|
54
|
|
|
""" |
|
55
|
|
|
model = UserProfile |
|
56
|
|
|
form_class = UserProfileForm |
|
57
|
|
|
template_name = "dj_diabetes/userprofile_form.html" |
|
58
|
|
|
success_url = reverse_lazy('home') |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
class LoginRequiredMixin(object): |
|
62
|
|
|
@classmethod |
|
63
|
|
|
def as_view(cls, **initkwargs): |
|
64
|
|
|
view = super(LoginRequiredMixin, cls).as_view(**initkwargs) |
|
65
|
|
|
return login_required(view) |
|
66
|
|
|
|