blob: 07691df0a3db2ed2b9712774ca23a7781373ec3c [file] [log] [blame]
Mitja Nikolaus6a679132018-08-30 14:35:29 +02001"""Views for the Hiccup statistics."""
2
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +02003from django.http import HttpResponse, Http404, HttpResponseRedirect
Dirk Vogt62ff7f22017-05-04 16:07:21 +02004from django.template import loader
Dirk Vogt62ff7f22017-05-04 16:07:21 +02005from django.contrib.auth.decorators import user_passes_test
Dirk Vogt57a615d2017-05-04 22:29:54 +02006from django import forms
7from django.contrib import messages
8from django.urls import reverse
Dirk Vogt62ff7f22017-05-04 16:07:21 +02009
Mitja Nikolausbcaf5022018-08-30 16:40:38 +020010from crashreports.models import Device
Mitja Nikolaus0b1aa8c2018-09-14 15:33:29 +020011from crashreports.permissions import user_is_hiccup_staff
Dirk Vogt62ff7f22017-05-04 16:07:21 +020012
Dirk Vogt57a615d2017-05-04 22:29:54 +020013
14class DeviceUUIDForm(forms.Form):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020015 """Form for searching devices by UUID."""
16
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020017 uuid = forms.CharField(label="Device UUID:", max_length=100)
18
Dirk Vogt57a615d2017-05-04 22:29:54 +020019
Mitja Nikolaus0b1aa8c2018-09-14 15:33:29 +020020@user_passes_test(user_is_hiccup_staff)
Dirk Vogt62ff7f22017-05-04 16:07:21 +020021def device_stats(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020022 """Respond with statistics for a specific device."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020023 template = loader.get_template("crashreport_stats/device.html")
24 uuid = request.GET.get("uuid", "NO_UUID")
Dirk Vogt57a615d2017-05-04 22:29:54 +020025 if not Device.objects.filter(uuid=uuid).exists():
26 raise Http404("Device doesn't exist.")
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020027 return HttpResponse(template.render({"uuid": uuid}, request))
28
Dirk Vogt1accb672017-05-10 14:07:42 +020029
Mitja Nikolaus0b1aa8c2018-09-14 15:33:29 +020030@user_passes_test(user_is_hiccup_staff)
Dirk Vogt1accb672017-05-10 14:07:42 +020031def versions_all_overview(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020032 """Respond with the distribution of official release versions."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020033 template = loader.get_template("crashreport_stats/versions.html")
34 return HttpResponse(template.render({"is_official_release": "1"}, request))
35
Dirk Vogt1accb672017-05-10 14:07:42 +020036
Mitja Nikolaus0b1aa8c2018-09-14 15:33:29 +020037@user_passes_test(user_is_hiccup_staff)
Dirk Vogt1accb672017-05-10 14:07:42 +020038def versions_overview(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020039 """Respond with the distribution of non-official release versions."""
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
Mitja Nikolaus0b1aa8c2018-09-14 15:33:29 +020044@user_passes_test(user_is_hiccup_staff)
Dirk Vogt57a615d2017-05-04 22:29:54 +020045def home(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020046 """Respond with a form for searching devices by UUID.
47
48 When using a HTTP GET method, the search device form view is returned.
49 The response additionally includes possible results if a HTTP POST message
50 was sent. If a single device was found, a redirect to the device
51 statistics of that device is sent.
52 """
Dirk Vogtb0b8b512017-12-05 14:46:03 +010053 devices = None
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020054 if request.method == "POST":
Dirk Vogt57a615d2017-05-04 22:29:54 +020055 # create a form instance and populate it with data from the request:
56 form = DeviceUUIDForm(request.POST)
57 if form.is_valid():
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020058 uuid = form.cleaned_data["uuid"]
Dirk Vogt57a615d2017-05-04 22:29:54 +020059 if not Device.objects.filter(uuid=uuid).exists():
Dirk Vogtb0b8b512017-12-05 14:46:03 +010060 devices = Device.objects.filter(uuid__startswith=uuid)
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020061 if len(devices) == 1:
Dirk Vogtb0b8b512017-12-05 14:46:03 +010062 return HttpResponseRedirect(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020063 reverse("hiccup_stats_device")
64 + "?uuid="
65 + devices[0].uuid
66 )
Mitja Nikolausd9fbfd62018-08-30 17:38:51 +020067 if not devices:
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020068 messages.warning(request, "No devices found.")
Dirk Vogt57a615d2017-05-04 22:29:54 +020069 else:
Dirk Vogtb0b8b512017-12-05 14:46:03 +010070 return HttpResponseRedirect(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020071 reverse("hiccup_stats_device") + "?uuid=" + uuid
72 )
Dirk Vogt57a615d2017-05-04 22:29:54 +020073 else:
74 form = DeviceUUIDForm()
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020075 template = loader.get_template("crashreport_stats/home.html")
76 return HttpResponse(
77 template.render({"form": form, "devices": devices}, request)
78 )