This repository has been archived by the owner on Feb 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
views.py
46 lines (39 loc) · 1.57 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from django.utils import simplejson as json
from django.views.decorators.http import require_POST
from .forms import ReportForm
from .models import Report, STAGES
from .tasks import process_report
@require_POST
def create(request):
form = ReportForm(data=request.POST or None)
if form.is_valid():
report = form.save()
request.session['report_pk'] = report.pk
process_report.delay(report)
result = {'status': 'ok', 'url': report.get_absolute_url()}
else:
result = {'status': 'error'}
return HttpResponse(json.dumps(result), mimetype='application/json')
def get_status(request):
pk = request.session.get('report_pk', None)
if pk is not None:
result = ['waiting', 'waiting', 'waiting', 'waiting']
report = get_object_or_404(Report, pk=pk)
stage = report.stage
stage_index = STAGES.index(stage)
for status in range(stage_index):
result[status] = 'done'
if stage != 'done':
result[stage_index] = 'working'
if report.error:
result[stage_index] = 'error'
data = {'queue': result[0], 'cloning': result[1],
'parsing': result[2], 'analyzing': result[3]}
return HttpResponse(json.dumps(data), mimetype='application/json')
def results(request, hash):
qs = Report.objects.filter(stage='done')
qs = qs.exclude(error='')
report = get_object_or_404(qs, hash=hash)
return render(request, 'lint/results.html', {'report': report})