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