aboutsummaryrefslogtreecommitdiffstats
path: root/lib/yp/views.py
blob: 6f722479b06e6b70a615b6e9f3978a68f9256fea (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#
# 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.

import os

#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 orm.models import SrtSetting
from srtgui.views import MimeTypeFinder
from yp.reports import YPReportManager

#from orm.models import SrtSetting

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

SRT_BASE_DIR = os.environ.get('SRT_BASE_DIR', '.')
SRT_MAIN_APP = os.environ.get('SRT_MAIN_APP', '.')

# determine in which mode we are running in, and redirect appropriately
def landing(request):

    # Django sometimes has a race condition with this view executing
    # for the master app's landing page HTML which can lead to context
    # errors, so hard enforce the default re-direction
    if SRT_MAIN_APP and (SRT_MAIN_APP != "yp"):
        return redirect(f"/{SRT_MAIN_APP}/landing/")

    # Append the list of landing page extensions
    landing_extensions_table = []
    for landing_extension in SrtSetting.objects.filter(name__startswith='LANDING_LINK').order_by('name'):
        landing_extensions_table.append(landing_extension.value.split('|'))

    context = {
        'landing_extensions_table' : landing_extensions_table,
        'this_landing' : 'yp',
    }

    return render(request, 'landing.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')