blob: 9e097ec668bd664976794eb1d43f114953d87d9d [file] [log] [blame]
Mitja Nikolaus6a679132018-08-30 14:35:29 +02001"""Views for the Hiccup statistics."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +02002from django.http import HttpResponse, Http404, HttpResponseRedirect
Dirk Vogt62ff7f22017-05-04 16:07:21 +02003from django.template import loader
Dirk Vogt62ff7f22017-05-04 16:07:21 +02004from django.contrib.auth.decorators import user_passes_test
Dirk Vogt57a615d2017-05-04 22:29:54 +02005from django import forms
6from django.contrib import messages
7from django.urls import reverse
Dirk Vogt62ff7f22017-05-04 16:07:21 +02008
Mitja Nikolausa9017982018-11-21 12:14:45 +01009from crashreport_stats.permissions import check_user_is_hiccup_staff
Mitja Nikolausbcaf5022018-08-30 16:40:38 +020010from crashreports.models import Device
Dirk Vogt62ff7f22017-05-04 16:07:21 +020011
Dirk Vogt57a615d2017-05-04 22:29:54 +020012
13class DeviceUUIDForm(forms.Form):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020014 """Form for searching devices by UUID."""
15
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020016 uuid = forms.CharField(label="Device UUID:", max_length=100)
17
Dirk Vogt57a615d2017-05-04 22:29:54 +020018
Mitja Nikolausa9017982018-11-21 12:14:45 +010019@user_passes_test(check_user_is_hiccup_staff)
Dirk Vogt62ff7f22017-05-04 16:07:21 +020020def device_stats(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020021 """Respond with statistics for a specific device."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020022 template = loader.get_template("crashreport_stats/device.html")
23 uuid = request.GET.get("uuid", "NO_UUID")
Dirk Vogt57a615d2017-05-04 22:29:54 +020024 if not Device.objects.filter(uuid=uuid).exists():
25 raise Http404("Device doesn't exist.")
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020026 return HttpResponse(template.render({"uuid": uuid}, request))
27
Dirk Vogt1accb672017-05-10 14:07:42 +020028
Mitja Nikolausa9017982018-11-21 12:14:45 +010029@user_passes_test(check_user_is_hiccup_staff)
Dirk Vogt1accb672017-05-10 14:07:42 +020030def versions_all_overview(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020031 """Respond with the distribution of official release versions."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020032 template = loader.get_template("crashreport_stats/versions.html")
33 return HttpResponse(template.render({"is_official_release": "1"}, request))
34
Dirk Vogt1accb672017-05-10 14:07:42 +020035
Mitja Nikolausa9017982018-11-21 12:14:45 +010036@user_passes_test(check_user_is_hiccup_staff)
Dirk Vogt1accb672017-05-10 14:07:42 +020037def versions_overview(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020038 """Respond with the distribution of non-official release versions."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020039 template = loader.get_template("crashreport_stats/versions.html")
40 return HttpResponse(template.render({"is_official_release": "2"}, request))
41
Dirk Vogt1accb672017-05-10 14:07:42 +020042
Mitja Nikolausa9017982018-11-21 12:14:45 +010043@user_passes_test(check_user_is_hiccup_staff)
Dirk Vogt57a615d2017-05-04 22:29:54 +020044def home(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020045 """Respond with a form for searching devices by UUID.
46
47 When using a HTTP GET method, the search device form view is returned.
48 The response additionally includes possible results if a HTTP POST message
49 was sent. If a single device was found, a redirect to the device
50 statistics of that device is sent.
51 """
Dirk Vogtb0b8b512017-12-05 14:46:03 +010052 devices = None
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020053 if request.method == "POST":
Dirk Vogt57a615d2017-05-04 22:29:54 +020054 # create a form instance and populate it with data from the request:
55 form = DeviceUUIDForm(request.POST)
56 if form.is_valid():
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020057 uuid = form.cleaned_data["uuid"]
Dirk Vogt57a615d2017-05-04 22:29:54 +020058 if not Device.objects.filter(uuid=uuid).exists():
Dirk Vogtb0b8b512017-12-05 14:46:03 +010059 devices = Device.objects.filter(uuid__startswith=uuid)
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020060 if len(devices) == 1:
Dirk Vogtb0b8b512017-12-05 14:46:03 +010061 return HttpResponseRedirect(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020062 reverse("hiccup_stats_device")
63 + "?uuid="
64 + devices[0].uuid
65 )
Mitja Nikolausd9fbfd62018-08-30 17:38:51 +020066 if not devices:
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020067 messages.warning(request, "No devices found.")
Dirk Vogt57a615d2017-05-04 22:29:54 +020068 else:
Dirk Vogtb0b8b512017-12-05 14:46:03 +010069 return HttpResponseRedirect(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020070 reverse("hiccup_stats_device") + "?uuid=" + uuid
71 )
Dirk Vogt57a615d2017-05-04 22:29:54 +020072 else:
73 form = DeviceUUIDForm()
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020074 template = loader.get_template("crashreport_stats/home.html")
75 return HttpResponse(
76 template.render({"form": form, "devices": devices}, request)
77 )