aboutsummaryrefslogtreecommitdiffstats
path: root/lib/yp/views.py
blob: 3310e7e680a75d63158280fdab0e0b46234bd859 (plain)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# ex:ts=4:sw=4:sts=4:et
# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
#
# Security Response Tool Implementation
#
# Copyright (C) 2017-2020 Wind River Systems
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

#from django.urls import reverse_lazy
#from django.views import generic
from django.http import HttpResponse, HttpResponseNotFound, JsonResponse, HttpResponseRedirect
from django.shortcuts import render, redirect

from users.models import SrtUser, UserSafe
from srtgui.views import MimeTypeFinder
from yp.reports import YPReportManager

#from orm.models import SrtSetting

# quick development/debugging support
from srtgui.api import _log

def yp_hello(request):
    context = {}
    _log("Note:yp_hello")
    return render(request, 'yp_hello.html', context)

def report(request,page_name):
    if request.method == "GET":
        context = YPReportManager.get_context_data(page_name,request=request)
        record_list = request.GET.get('record_list', '')
        _log("EXPORT_GET!:%s|%s|" % (request,record_list))
        context['record_list'] = record_list
        return render(request, 'report.html', context)
    elif request.method == "POST":
        _log("EXPORT_POST!:%s|%s" % (request,request.FILES))
        parent_page = request.POST.get('parent_page', '')
        file_name,response_file_name = YPReportManager.exec_report(parent_page,request=request)

        if file_name.startswith("Error"):
            # Refresh the page with the error message
            context = YPReportManager.get_context_data(page_name,request=request)
            context['error_message'] = file_name
            record_list = request.GET.get('record_list', '')
            _log("EXPORT_GET_WITH_ERROR!:%s|%s|" % (request,record_list))
            context['record_list'] = record_list
            return render(request, 'report.html', context)
        elif file_name and response_file_name:
            fsock = open(file_name, "rb")
            content_type = MimeTypeFinder.get_mimetype(file_name)

            response = HttpResponse(fsock, content_type = content_type)

            disposition = "attachment; filename=" + response_file_name
            response["Content-Disposition"] = disposition

            _log("EXPORT_POST_Q{%s|" % (response))
            return response
        else:
            return render(request, "unavailable_artifact.html", {})

        return redirect('/')
    raise Exception("Invalid HTTP method for this page")

def manage_report(request):
    # does this user have permission to see this record?
    if not UserSafe.is_creator(request.user):
        return redirect('/')

    return redirect(report,'management')