|
1
|
|
|
# coding: utf8 |
|
2
|
|
|
|
|
3
|
|
|
""" |
|
4
|
|
|
This software is licensed under the Apache 2 license, quoted below. |
|
5
|
|
|
|
|
6
|
|
|
Copyright 2014 Crystalnix Limited |
|
7
|
|
|
|
|
8
|
|
|
Licensed under the Apache License, Version 2.0 (the "License"); you may not |
|
9
|
|
|
use this file except in compliance with the License. You may obtain a copy of |
|
10
|
|
|
the License at |
|
11
|
|
|
|
|
12
|
|
|
http://www.apache.org/licenses/LICENSE-2.0 |
|
13
|
|
|
|
|
14
|
|
|
Unless required by applicable law or agreed to in writing, software |
|
15
|
|
|
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
16
|
|
|
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
17
|
|
|
License for the specific language governing permissions and limitations under |
|
18
|
|
|
the License. |
|
19
|
|
|
""" |
|
20
|
|
|
|
|
21
|
|
|
import json |
|
22
|
|
|
|
|
23
|
|
|
from django.core.exceptions import ObjectDoesNotExist |
|
24
|
|
|
from django.core.urlresolvers import reverse_lazy |
|
25
|
|
|
from django.views.generic import FormView |
|
26
|
|
|
from django.views.decorators.csrf import csrf_exempt |
|
27
|
|
|
from django.http import HttpResponse, HttpResponseBadRequest |
|
28
|
|
|
from crash.forms import CrashFrom, CrashDescriptionForm |
|
29
|
|
|
from crash.models import Crash |
|
30
|
|
|
from omaha_server.utils import get_client_ip |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
class CrashFormView(FormView): |
|
34
|
|
|
http_method_names = ('post',) |
|
35
|
|
|
form_class = CrashFrom |
|
36
|
|
|
|
|
37
|
|
|
@csrf_exempt |
|
38
|
|
|
def dispatch(self, *args, **kwargs): |
|
39
|
|
|
return super(CrashFormView, self).dispatch(*args, **kwargs) |
|
40
|
|
|
|
|
41
|
|
|
def form_valid(self, form): |
|
42
|
|
|
meta = self.request.POST.dict() |
|
43
|
|
|
meta.pop("appid", None) |
|
44
|
|
|
meta.pop("userid", None) |
|
45
|
|
|
obj = form.save(commit=False) |
|
46
|
|
|
if meta: |
|
47
|
|
|
obj.meta = meta |
|
48
|
|
|
obj.ip = get_client_ip(self.request) |
|
49
|
|
|
obj.save() |
|
50
|
|
|
return HttpResponse(obj.pk, status=200) |
|
51
|
|
|
|
|
52
|
|
|
def form_invalid(self, form): |
|
53
|
|
|
return HttpResponse(json.dumps(form.errors), status=400, content_type='application/json') |
|
54
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
class CrashDescriptionFormView(FormView): |
|
57
|
|
|
form_class = CrashDescriptionForm |
|
58
|
|
|
template_name = 'crash/crash_description.html' |
|
59
|
|
|
success_url = reverse_lazy('crash_description_submitted') |
|
60
|
|
|
|
|
61
|
|
|
def dispatch(self, request, *args, **kwargs): |
|
62
|
|
|
# verify crash_id refers to valid crash object |
|
63
|
|
|
try: |
|
64
|
|
|
self.crash = Crash.objects.select_related('crash_description').get(pk=self.kwargs.get('pk')) |
|
65
|
|
|
except Crash.DoesNotExist: |
|
66
|
|
|
return HttpResponseBadRequest('no such crash') |
|
67
|
|
|
|
|
68
|
|
|
# verify there is no crash description for that object yet |
|
69
|
|
|
try: |
|
70
|
|
|
desc = self.crash.crash_description |
|
71
|
|
|
return HttpResponseBadRequest('already reported as \"%s\"' % desc.summary) |
|
72
|
|
|
except ObjectDoesNotExist: |
|
73
|
|
|
pass |
|
74
|
|
|
|
|
75
|
|
|
return super(CrashDescriptionFormView, self).dispatch(request, *args, **kwargs) |
|
76
|
|
|
|
|
77
|
|
|
def get_initial(self): |
|
78
|
|
|
data = super(CrashDescriptionFormView, self).get_initial() |
|
79
|
|
|
data['description'] = self.request.GET.get('comment') |
|
80
|
|
|
return data |
|
81
|
|
|
|
|
82
|
|
|
def form_valid(self, form): |
|
83
|
|
|
obj = form.save(commit=False) |
|
84
|
|
|
obj.crash = self.crash |
|
85
|
|
|
obj.save() |
|
86
|
|
|
return super(CrashDescriptionFormView, self).form_valid(form) |
|
87
|
|
|
|