Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 1 | """Views for the Hiccup statistics.""" |
| 2 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 3 | from django.http import HttpResponse, Http404, HttpResponseRedirect |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 4 | from django.template import loader |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 5 | from django.contrib.auth.decorators import user_passes_test |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 6 | from django import forms |
| 7 | from django.contrib import messages |
| 8 | from django.urls import reverse |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 9 | |
Mitja Nikolaus | bcaf502 | 2018-08-30 16:40:38 +0200 | [diff] [blame] | 10 | from crashreports.models import Device |
| 11 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 12 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 13 | def is_fairphone_staff(user): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 14 | """Check if the user is part of the FairphoneSoftwareTeam group.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 15 | return user.groups.filter(name="FairphoneSoftwareTeam").exists() |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 16 | |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 17 | |
| 18 | class DeviceUUIDForm(forms.Form): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 19 | """Form for searching devices by UUID.""" |
| 20 | |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 21 | uuid = forms.CharField(label="Device UUID:", max_length=100) |
| 22 | |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 23 | |
Dirk Vogt | 62ff7f2 | 2017-05-04 16:07:21 +0200 | [diff] [blame] | 24 | @user_passes_test(is_fairphone_staff) |
| 25 | def device_stats(request): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 26 | """Respond with statistics for a specific device.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 27 | template = loader.get_template("crashreport_stats/device.html") |
| 28 | uuid = request.GET.get("uuid", "NO_UUID") |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 29 | if not Device.objects.filter(uuid=uuid).exists(): |
| 30 | raise Http404("Device doesn't exist.") |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 31 | return HttpResponse(template.render({"uuid": uuid}, request)) |
| 32 | |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 33 | |
| 34 | @user_passes_test(is_fairphone_staff) |
| 35 | def versions_all_overview(request): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 36 | """Respond with the distribution of official release versions.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 37 | template = loader.get_template("crashreport_stats/versions.html") |
| 38 | return HttpResponse(template.render({"is_official_release": "1"}, request)) |
| 39 | |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 40 | |
| 41 | @user_passes_test(is_fairphone_staff) |
| 42 | def versions_overview(request): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 43 | """Respond with the distribution of non-official release versions.""" |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 44 | template = loader.get_template("crashreport_stats/versions.html") |
| 45 | return HttpResponse(template.render({"is_official_release": "2"}, request)) |
| 46 | |
Dirk Vogt | 1accb67 | 2017-05-10 14:07:42 +0200 | [diff] [blame] | 47 | |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 48 | @user_passes_test(is_fairphone_staff) |
| 49 | def home(request): |
Mitja Nikolaus | 6a67913 | 2018-08-30 14:35:29 +0200 | [diff] [blame] | 50 | """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 Vogt | b0b8b51 | 2017-12-05 14:46:03 +0100 | [diff] [blame] | 57 | devices = None |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 58 | if request.method == "POST": |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 59 | # create a form instance and populate it with data from the request: |
| 60 | form = DeviceUUIDForm(request.POST) |
| 61 | if form.is_valid(): |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 62 | uuid = form.cleaned_data["uuid"] |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 63 | if not Device.objects.filter(uuid=uuid).exists(): |
Dirk Vogt | b0b8b51 | 2017-12-05 14:46:03 +0100 | [diff] [blame] | 64 | devices = Device.objects.filter(uuid__startswith=uuid) |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 65 | if len(devices) == 1: |
Dirk Vogt | b0b8b51 | 2017-12-05 14:46:03 +0100 | [diff] [blame] | 66 | return HttpResponseRedirect( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 67 | reverse("hiccup_stats_device") |
| 68 | + "?uuid=" |
| 69 | + devices[0].uuid |
| 70 | ) |
Mitja Nikolaus | d9fbfd6 | 2018-08-30 17:38:51 +0200 | [diff] [blame^] | 71 | if not devices: |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 72 | messages.warning(request, "No devices found.") |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 73 | else: |
Dirk Vogt | b0b8b51 | 2017-12-05 14:46:03 +0100 | [diff] [blame] | 74 | return HttpResponseRedirect( |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 75 | reverse("hiccup_stats_device") + "?uuid=" + uuid |
| 76 | ) |
Dirk Vogt | 57a615d | 2017-05-04 22:29:54 +0200 | [diff] [blame] | 77 | else: |
| 78 | form = DeviceUUIDForm() |
Mitja Nikolaus | cb50f2c | 2018-08-24 13:54:48 +0200 | [diff] [blame] | 79 | template = loader.get_template("crashreport_stats/home.html") |
| 80 | return HttpResponse( |
| 81 | template.render({"form": form, "devices": devices}, request) |
| 82 | ) |