Dirk Vogt | f130c75 | 2016-08-23 14:45:01 +0200 | [diff] [blame^] | 1 | # -*- coding: utf-8 -*- |
| 2 | from django.shortcuts import render_to_response |
| 3 | from django.shortcuts import render |
| 4 | from django.http import HttpResponseRedirect |
| 5 | from django.http import HttpResponse |
| 6 | from django.core.urlresolvers import reverse |
| 7 | |
| 8 | from crashreports.models import Crashreport |
| 9 | from crashreports.forms import CrashreportForm |
| 10 | from django.views.decorators.csrf import csrf_exempt |
| 11 | from django.http import Http404 |
| 12 | |
| 13 | from django.contrib.auth.decorators import login_required |
| 14 | from django.db.models import Count |
| 15 | |
| 16 | import datetime |
| 17 | import time |
| 18 | |
| 19 | @csrf_exempt |
| 20 | def index(request): |
| 21 | # Handle file upload` |
| 22 | if request.method == 'POST': |
| 23 | form = CrashreportForm(request.POST, request.FILES) |
| 24 | if form.is_valid(): |
| 25 | new_cr = Crashreport(uuid=form.cleaned_data['uuid'], |
| 26 | aux_data=form.cleaned_data['aux_data'], |
| 27 | uptime=form.cleaned_data['uptime'], |
| 28 | boot_reason=form.cleaned_data['boot_reason'], |
| 29 | power_on_reason=form.cleaned_data['power_on_reason'], |
| 30 | power_off_reason=form.cleaned_data['power_off_reason'], |
| 31 | build_fingerprint=form.cleaned_data['build_fingerprint'], |
| 32 | date=form.cleaned_data['date']) |
| 33 | try: |
| 34 | new_cr.crashreport_file = request.FILES['crashreport'] |
| 35 | except: |
| 36 | new_cr.crashreport_file = None |
| 37 | new_cr.save() |
| 38 | # Redirect to the document list after POST |
| 39 | return HttpResponse(status=204) |
| 40 | else: |
| 41 | return HttpResponse(status=400) |
| 42 | else: |
| 43 | return HttpResponse(status=400) |
| 44 | |
| 45 | |
| 46 | def empty(request): |
| 47 | return HttpResponse(status=204) |
| 48 | |
| 49 | @login_required |
| 50 | def get_crash_statistic(request): |
| 51 | from_date = request.GET.get('from_date', "2016-01-01") |
| 52 | to_date = request.GET.get('to_date', "2017-01-01") |
| 53 | entries = Crashreport.objects.filter(date__range=[from_date, to_date]).extra({'date_created' : "date(date)"}).values('date_created').annotate(created_count=Count('id')) |
| 54 | for entry in entries: |
| 55 | entry['date_created']=time.mktime(datetime.datetime.strptime(entry['date_created'], "%Y-%m-%d").timetuple())*1000 |
| 56 | return render_to_response('crashreports/json/crashreport_by_day.html/', { |
| 57 | 'entries' : entries |
| 58 | }) |