summaryrefslogtreecommitdiffstats
path: root/iceci/views.py
blob: 6a5a9b860a92c384431f17ec8bbcf8127b798e29 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
 
# ============LICENSE_START========================================== 
# org.onap.vvp/test-engine
# ===================================================================
# Copyright © 2017 AT&T Intellectual Property. All rights reserved.
# ===================================================================
#
# Unless otherwise specified, all software contained herein is licensed
# under the Apache License, Version 2.0 (the “License”);
# you may not use this software except in compliance with the License.
# You may obtain a copy of the License at
#
#             http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#
#
# Unless otherwise specified, all documentation contained herein is licensed
# under the Creative Commons License, Attribution 4.0 Intl. (the “License”);
# you may not use this documentation except in compliance with the License.
# You may obtain a copy of the License at
#
#             https://creativecommons.org/licenses/by/4.0/
#
# Unless required by applicable law or agreed to in writing, documentation
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ============LICENSE_END============================================
#
# ECOMP is a trademark and service mark of AT&T Intellectual Property.
import logging
from string import Template

from django.conf import settings
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from rest_framework.renderers import JSONRenderer

from iceci import mail
from iceci.mail import testsResults_mail_body
from services.constants import Constants, ServiceProvider
from services.logging_service import LoggingServiceFactory

from .models import TestResults
from .serializers import TestResultsModelSerializer


LAST_BUILD_REPORT_NUM = None
# from django.core.mail import send_mail
# from . import mail
logger = LoggingServiceFactory.get_logger()

def index(request):
    return HttpResponse("Hello, world. You're at the "
                        + ServiceProvider.PROGRAM_NAME + " ci index.")

@csrf_exempt
def testResult_list(request):  # List all tests, or create a new test.
    if (request.method == 'DELETE' or request.method == 'PUT'):
        return HttpResponse(status=405)
    
    if request.method == 'GET':
        testResult = TestResults.objects.all()
        serializer = TestResultsModelSerializer(testResult, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = TestResultsModelSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400)
@csrf_exempt
def testResultStr(request,param):  # List all tests, or create a new test.
    try:
        testResults = TestResults.objects.filter(build_id=param)
        pass_counter, total_counter, statisticData, fail_counter = strReportForTestResults(testResults)
        evaultaion = as_percentage_of(pass_counter, total_counter)
    except Exception as e:
        msg = "Something went wrong while trying to send Test Results "  + str(e)
        return HttpResponse(msg, status=500)

    msg = "Total Tests: " + str(total_counter) +" Pass Tests: " + str(pass_counter)+" Fail Tests: "+ str(fail_counter) + " Statistics : " + str(evaultaion) + " BUILD_REPORT_NUM : "+str(param)
    return HttpResponse(msg, status=200)

@csrf_exempt
def testResult_detail(request, param):  # Retrieve, update or delete a test.
    if request.method == 'POST':
        return HttpResponse(status=405)
    
    param = param.strip("/")
    
    try:
        testResult = TestResults.objects.get(name=param)
    except TestResults.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = TestResultsModelSerializer(testResult)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = TestResultsModelSerializer(testResult, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        testResult.delete()
        return HttpResponse(status=204)

#===============================================================================
# def testResult_post_action(request, data):
#     
#     logger.debug("about to send mail to " + data['email'])
#     
#     html_msg = mail.mail_body.substitute(data)
#     #send mail with template           
#     send_mail(mail.mail_subject, 
#               '', 
#               mail.mail_from,
#               mail.mail_to, 
#               fail_silently=False,
#               html_message=html_msg)
#===============================================================================
def createHtmlStrReportForTestResults(testResults):
    total_counter = 0
    fail_counter = 0
    pass_counter = 0
    statisticData = ""
    str2 = "<tr class='row1'>" + "<th class='field-testTotal'>@Total</th>" + "<td class='field-testPass'>@Pass</td>" + "<td class='field-tesFail'>@Fail</td>" + "<td class='field-testEvaultaion'>@Evaultaion</td>" + "</tr>"
    paramData = ""
    str3 = "<tr class='row2'>" + "<th class='field-testTotal'>@Version</th>" + "</tr>"
    allData = ""
    str = "<tr class='row1'>" + "<th class='field-testType'>@TestType</th>" + "<td class='field-testFeature'>@TestFeature</td>" + "<td class='field-testName'>@TestName</td>" + "<td class='field-testResult'>@TestResult</td>" + "<td class='field-notes'>@Notes</td>" + "<td class='field-create_time nowrap'>@Creation_time</td>" + "</tr>"
    for res in testResults: # testResults
        allData += str.replace("@TestType", str(res.testType)).replace("@TestFeature", str(res.testFeature)).replace("@TestName", str(res.testName)).replace("@TestResult", str(res.testResult)).replace("@Notes", str(res.notes)).replace("@Creation_time", str(res.create_time))
        total_counter += 1
        if (res.testResult == "PASS"):
            pass_counter += 1
        else:
            fail_counter += 1
    
    return pass_counter, total_counter, statisticData, str2, fail_counter, paramData, str3, allData

def strReportForTestResults(testResults):
    total_counter = 0
    fail_counter = 0
    pass_counter = 0
    statisticData = ""
    for res in testResults: # testResults
        total_counter += 1
        if (res.testResult == "PASS"):
            pass_counter += 1
        else:
            fail_counter += 1
    
    return pass_counter, total_counter, statisticData, fail_counter

@csrf_exempt
def testResult_list_to_mail(request,param):   # List all tests, or create a new test.
    if (request.method == 'DELETE' or request.method == 'PUT' or request.method == 'POST'):
        return HttpResponse(status=405)

    data = dict()

    print("BUILD_REPORT_NUM = "+settings.ICE_BUILD_REPORT_NUM)

    testResults = TestResults.objects.filter(build_id=param)

    pass_counter, total_counter, statisticData, str2, fail_counter, paramData, str3, allData = createHtmlStrReportForTestResults(testResults)
    
    evaultaion = as_percentage_of(pass_counter, total_counter)
    statisticData += str2.replace("@Total", str(total_counter)).replace("@Pass", str(pass_counter)).replace("@Fail", str(fail_counter)).replace("@Evaultaion", str(evaultaion))
    paramData += str3.replace("@Version", str(param))
    data['email'] = "rgafiulin@interwise.com"
    data['allData'] = str(allData) 
    data['statisticData'] = str(statisticData) 
    data['paramData'] = str(paramData)
    
    mail.testsResults_mail_to = data['email']
    try:
        mail.sendMail(param,data['email'], data, mail.testsResults_mail_body, mail.testsResults_mail_subject) 
    except Exception as e:
        msg = "Something went wrong while trying to send Test Report mail to " + data['email'] + str(e)
        return HttpResponse(msg, status=500)

    serializer = TestResultsModelSerializer(testResults, many=True)
    return JSONResponse(serializer.data)

@csrf_exempt
def testResult_list_to_html_file(request,param):
    if (request.method == 'DELETE' or request.method == 'PUT' or request.method == 'POST'):
        return HttpResponse(status=405)

    data = dict()

    print("BUILD_REPORT_NUM = "+settings.ICE_BUILD_REPORT_NUM)

    testResults = TestResults.objects.filter(build_id=settings.ICE_BUILD_REPORT_NUM)

    pass_counter, total_counter, statisticData, str2, fail_counter, paramData, str3, allData = createHtmlStrReportForTestResults(testResults)
    
    evaultaion = as_percentage_of(pass_counter, total_counter)
    statisticData += str2.replace("@Total", str(total_counter)).replace("@Pass", str(pass_counter)).replace("@Fail", str(fail_counter)).replace("@Evaultaion", str(evaultaion))
    paramData += str3.replace("@Version", str(param))

    data['allData'] = str(allData) 
    data['statisticData'] = str(statisticData) 
    data['paramData'] = str(paramData)
    
    html_msg = testsResults_mail_body.substitute(data)
    fileName = settings.LOGS_PATH+"Test_Results_"+settings.ICE_BUILD_REPORT_NUM+".html"
    try:
        with open(fileName, "w") as text_file:
            text_file.write(html_msg)
    except Exception as e:
        msg = "Something went wrong while trying to write the tet results to html file " +fileName +" "+ str(e)
        return HttpResponse(msg, status=500)

    serializer = TestResultsModelSerializer(testResults, many=True)
    return JSONResponse(serializer.data)

def as_percentage_of(part, whole):
    try:
        return "%d%%" % (float(part) / whole * 100)
    except (ValueError, ZeroDivisionError):
        return "" 

class JSONResponse(HttpResponse):  # An HttpResponse that renders its content into JSON.
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)