1 |
8792cad8
|
Marc NICOLAS
|
|
2 |
|
|
|
3 |
|
|
from django.shortcuts import render, get_object_or_404
|
4 |
|
|
from webstat.forms import webstatForm
|
5 |
|
|
import models
|
6 |
|
|
from django.http import HttpResponseRedirect
|
7 |
|
|
from django.core.urlresolvers import reverse
|
8 |
|
|
from webstat.libStatSitools import *
|
9 |
|
|
|
10 |
|
|
'''
|
11 |
|
|
def home(request):
|
12 |
|
|
text = "Welcome on SiTools2 Webstat Interface"
|
13 |
|
|
return render(request, 'home.html', locals())
|
14 |
|
|
'''
|
15 |
|
|
|
16 |
|
|
def statview(request):
|
17 |
|
|
|
18 |
|
|
if request.method == 'POST':
|
19 |
|
|
form = webstatForm(request.POST)
|
20 |
|
|
if form.is_valid():
|
21 |
|
|
try:
|
22 |
|
|
form.save()
|
23 |
|
|
form_id = models.webstatModel.objects.filter().order_by('-id')[0].id
|
24 |
|
|
request.session['form_id'] = form_id
|
25 |
|
|
except BaseException, e:
|
26 |
|
|
print "Save FAILED : ", str(e)
|
27 |
|
|
return render(request, '500.html', locals())
|
28 |
|
|
return HttpResponseRedirect(reverse('webstat:results'))
|
29 |
|
|
else:
|
30 |
|
|
form = webstatForm()
|
31 |
|
|
return render(request, 'formWebStat.html', locals())
|
32 |
|
|
|
33 |
|
|
|
34 |
|
|
def result_view(request):
|
35 |
|
|
|
36 |
|
|
form_id = request.session.get('form_id')
|
37 |
|
|
form = get_object_or_404(models.webstatModel, pk=form_id)
|
38 |
|
|
|
39 |
|
|
if form.start_date is None:
|
40 |
|
|
start_tmp = "2012-03-01 00:00:00"
|
41 |
|
|
form.start_date = datetime(int(start_tmp[0:4]), int(start_tmp[5:7]), int(start_tmp[8:10]),
|
42 |
|
|
int(start_tmp[11:13]), int(start_tmp[14:16])).date()
|
43 |
|
|
if form.end_date is None:
|
44 |
|
|
form.end_date = datetime.now().date()
|
45 |
|
|
|
46 |
|
|
form_dico = instance_dict(form)
|
47 |
|
|
data = stat_log(form.application, form_dico)
|
48 |
|
|
return render(request, 'webStatResults.html', locals())
|
49 |
|
|
|
50 |
|
|
|
51 |
|
|
def instance_dict(instance, key_format=None):
|
52 |
|
|
"Returns a dictionary containing field names and values for the given instance"
|
53 |
|
|
from django.db.models.fields.related import ForeignKey
|
54 |
|
|
if key_format:
|
55 |
|
|
assert '%s' in key_format, 'key_format must contain a %s'
|
56 |
|
|
key = lambda key: key_format and key_format % key or key
|
57 |
|
|
|
58 |
|
|
d = {}
|
59 |
|
|
for field in instance._meta.fields:
|
60 |
|
|
attr = field.name
|
61 |
|
|
value = getattr(instance, attr)
|
62 |
|
|
if value is not None and isinstance(field, ForeignKey):
|
63 |
|
|
value = value._get_pk_val()
|
64 |
|
|
d[key(attr)] = value
|
65 |
|
|
for field in instance._meta.many_to_many:
|
66 |
|
|
d[key(field.name)] = [obj._get_pk_val() for obj in getattr(instance, field.attname).all()]
|
67 |
|
|
return d |