blob: 54a67e79707b7f38260c1eb588b67cdbb5dc4361 [file] [log] [blame]
Dirk Vogt62ff7f22017-05-04 16:07:21 +02001from crashreports.models import *
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +02002from django.http import HttpResponse, Http404, HttpResponseRedirect
Dirk Vogt62ff7f22017-05-04 16:07:21 +02003from django.shortcuts import render
4from django.shortcuts import render_to_response
5from django.template import loader
6from itertools import chain
7from django.db import connection
Dirk Vogtb0b8b512017-12-05 14:46:03 +01008from crashreport_stats import raw_querys
Dirk Vogt62ff7f22017-05-04 16:07:21 +02009from django.contrib.auth.decorators import user_passes_test
Dirk Vogt57a615d2017-05-04 22:29:54 +020010from django import forms
11from django.contrib import messages
12from django.urls import reverse
Dirk Vogt62ff7f22017-05-04 16:07:21 +020013
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020014
Dirk Vogt62ff7f22017-05-04 16:07:21 +020015def is_fairphone_staff(user):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020016 return user.groups.filter(name="FairphoneSoftwareTeam").exists()
Dirk Vogt62ff7f22017-05-04 16:07:21 +020017
Dirk Vogt57a615d2017-05-04 22:29:54 +020018
19class DeviceUUIDForm(forms.Form):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020020 uuid = forms.CharField(label="Device UUID:", max_length=100)
21
Dirk Vogt57a615d2017-05-04 22:29:54 +020022
Dirk Vogt62ff7f22017-05-04 16:07:21 +020023@user_passes_test(is_fairphone_staff)
24def device_stats(request):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020025 template = loader.get_template("crashreport_stats/device.html")
26 uuid = request.GET.get("uuid", "NO_UUID")
Dirk Vogt57a615d2017-05-04 22:29:54 +020027 if not Device.objects.filter(uuid=uuid).exists():
28 raise Http404("Device doesn't exist.")
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020029 return HttpResponse(template.render({"uuid": uuid}, request))
30
Dirk Vogt1accb672017-05-10 14:07:42 +020031
32@user_passes_test(is_fairphone_staff)
33def versions_all_overview(request):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020034 template = loader.get_template("crashreport_stats/versions.html")
35 return HttpResponse(template.render({"is_official_release": "1"}, request))
36
Dirk Vogt1accb672017-05-10 14:07:42 +020037
38@user_passes_test(is_fairphone_staff)
39def versions_overview(request):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020040 template = loader.get_template("crashreport_stats/versions.html")
41 return HttpResponse(template.render({"is_official_release": "2"}, request))
42
Dirk Vogt1accb672017-05-10 14:07:42 +020043
Dirk Vogt57a615d2017-05-04 22:29:54 +020044@user_passes_test(is_fairphone_staff)
45def home(request):
Dirk Vogtb0b8b512017-12-05 14:46:03 +010046 """ The home view allows to search for devices. """
47 devices = None
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020048 if request.method == "POST":
Dirk Vogt57a615d2017-05-04 22:29:54 +020049 # create a form instance and populate it with data from the request:
50 form = DeviceUUIDForm(request.POST)
51 if form.is_valid():
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020052 uuid = form.cleaned_data["uuid"]
Dirk Vogt57a615d2017-05-04 22:29:54 +020053 if not Device.objects.filter(uuid=uuid).exists():
Dirk Vogtb0b8b512017-12-05 14:46:03 +010054 devices = Device.objects.filter(uuid__startswith=uuid)
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020055 if len(devices) == 1:
Dirk Vogtb0b8b512017-12-05 14:46:03 +010056 return HttpResponseRedirect(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020057 reverse("hiccup_stats_device")
58 + "?uuid="
59 + devices[0].uuid
60 )
61 elif len(devices) == 0:
62 messages.warning(request, "No devices found.")
Dirk Vogt57a615d2017-05-04 22:29:54 +020063 else:
Dirk Vogtb0b8b512017-12-05 14:46:03 +010064 return HttpResponseRedirect(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020065 reverse("hiccup_stats_device") + "?uuid=" + uuid
66 )
Dirk Vogt57a615d2017-05-04 22:29:54 +020067 else:
68 form = DeviceUUIDForm()
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020069 template = loader.get_template("crashreport_stats/home.html")
70 return HttpResponse(
71 template.render({"form": form, "devices": devices}, request)
72 )