blob: 58e326171eca9c7bcac9e40e14c96ef24393c101 [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
11
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020012
Dirk Vogt62ff7f22017-05-04 16:07:21 +020013def is_fairphone_staff(user):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020014 """Check if the user is part of the FairphoneSoftwareTeam group."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020015 return user.groups.filter(name="FairphoneSoftwareTeam").exists()
Dirk Vogt62ff7f22017-05-04 16:07:21 +020016
Dirk Vogt57a615d2017-05-04 22:29:54 +020017
18class DeviceUUIDForm(forms.Form):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020019 """Form for searching devices by UUID."""
20
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020021 uuid = forms.CharField(label="Device UUID:", max_length=100)
22
Dirk Vogt57a615d2017-05-04 22:29:54 +020023
Dirk Vogt62ff7f22017-05-04 16:07:21 +020024@user_passes_test(is_fairphone_staff)
25def device_stats(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020026 """Respond with statistics for a specific device."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020027 template = loader.get_template("crashreport_stats/device.html")
28 uuid = request.GET.get("uuid", "NO_UUID")
Dirk Vogt57a615d2017-05-04 22:29:54 +020029 if not Device.objects.filter(uuid=uuid).exists():
30 raise Http404("Device doesn't exist.")
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020031 return HttpResponse(template.render({"uuid": uuid}, request))
32
Dirk Vogt1accb672017-05-10 14:07:42 +020033
34@user_passes_test(is_fairphone_staff)
35def versions_all_overview(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020036 """Respond with the distribution of official release versions."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020037 template = loader.get_template("crashreport_stats/versions.html")
38 return HttpResponse(template.render({"is_official_release": "1"}, request))
39
Dirk Vogt1accb672017-05-10 14:07:42 +020040
41@user_passes_test(is_fairphone_staff)
42def versions_overview(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020043 """Respond with the distribution of non-official release versions."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020044 template = loader.get_template("crashreport_stats/versions.html")
45 return HttpResponse(template.render({"is_official_release": "2"}, request))
46
Dirk Vogt1accb672017-05-10 14:07:42 +020047
Dirk Vogt57a615d2017-05-04 22:29:54 +020048@user_passes_test(is_fairphone_staff)
49def home(request):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020050 """Respond with a form for searching devices by UUID.
51
52 When using a HTTP GET method, the search device form view is returned.
53 The response additionally includes possible results if a HTTP POST message
54 was sent. If a single device was found, a redirect to the device
55 statistics of that device is sent.
56 """
Dirk Vogtb0b8b512017-12-05 14:46:03 +010057 devices = None
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020058 if request.method == "POST":
Dirk Vogt57a615d2017-05-04 22:29:54 +020059 # create a form instance and populate it with data from the request:
60 form = DeviceUUIDForm(request.POST)
61 if form.is_valid():
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020062 uuid = form.cleaned_data["uuid"]
Dirk Vogt57a615d2017-05-04 22:29:54 +020063 if not Device.objects.filter(uuid=uuid).exists():
Dirk Vogtb0b8b512017-12-05 14:46:03 +010064 devices = Device.objects.filter(uuid__startswith=uuid)
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020065 if len(devices) == 1:
Dirk Vogtb0b8b512017-12-05 14:46:03 +010066 return HttpResponseRedirect(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020067 reverse("hiccup_stats_device")
68 + "?uuid="
69 + devices[0].uuid
70 )
Mitja Nikolausd9fbfd62018-08-30 17:38:51 +020071 if not devices:
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020072 messages.warning(request, "No devices found.")
Dirk Vogt57a615d2017-05-04 22:29:54 +020073 else:
Dirk Vogtb0b8b512017-12-05 14:46:03 +010074 return HttpResponseRedirect(
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020075 reverse("hiccup_stats_device") + "?uuid=" + uuid
76 )
Dirk Vogt57a615d2017-05-04 22:29:54 +020077 else:
78 form = DeviceUUIDForm()
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020079 template = loader.get_template("crashreport_stats/home.html")
80 return HttpResponse(
81 template.render({"form": form, "devices": devices}, request)
82 )