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