From 71891b3040605103397ffa7fb349215eced3a2c3 Mon Sep 17 00:00:00 2001 From: Edan Binshtok Date: Sun, 19 Nov 2017 11:50:50 +0200 Subject: Pep8 another fixes Add more fixes to pep8 Issue-ID: VVP-25 Change-Id: Icc3bd3977ced2b537c858a9801e04a6bf6d1db05 Signed-off-by: Edan Binshtok --- iceci/views.py | 175 +++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 121 insertions(+), 54 deletions(-) (limited to 'iceci/views.py') diff --git a/iceci/views.py b/iceci/views.py index 6a5a9b8..8f33679 100644 --- a/iceci/views.py +++ b/iceci/views.py @@ -1,5 +1,5 @@ - -# ============LICENSE_START========================================== + +# ============LICENSE_START========================================== # org.onap.vvp/test-engine # =================================================================== # Copyright © 2017 AT&T Intellectual Property. All rights reserved. @@ -36,8 +36,7 @@ # ============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 @@ -47,7 +46,7 @@ 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.constants import ServiceProvider from services.logging_service import LoggingServiceFactory from .models import TestResults @@ -59,15 +58,17 @@ LAST_BUILD_REPORT_NUM = None # 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) @@ -80,26 +81,34 @@ def testResult_list(request): # List all tests, or create a new test. 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. +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) + 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) + 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) + 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: @@ -121,125 +130,183 @@ def testResult_detail(request, param): # Retrieve, update or delete a test. 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, -# '', +# #send mail with template +# send_mail(mail.mail_subject, +# '', # mail.mail_from, -# mail.mail_to, +# mail.mail_to, # fail_silently=False, # html_message=html_msg) -#=============================================================================== +# ========================================================================= + + def createHtmlStrReportForTestResults(testResults): total_counter = 0 fail_counter = 0 pass_counter = 0 statisticData = "" - str2 = "" + "@Total" + "@Pass" + "@Fail" + "@Evaultaion" + "" + str2 = "" + "@Total" + \ + "@Pass" + \ + "@Fail" + \ + "@Evaultaion" + "" paramData = "" - str3 = "" + "@Version" + "" + str3 = "" + \ + "@Version" + "" allData = "" - str = "" + "@TestType" + "@TestFeature" + "@TestName" + "@TestResult" + "@Notes" + "@Creation_time" + "" - 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)) + string_temp = "" + \ + "@TestType" +\ + "@TestFeature" + \ + "@TestName" + \ + "@TestResult" + \ + "@Notes" + \ + "@Creation_time" + "" + # testResults + for res in testResults: + allData += string_temp.replace( + "@TestType", string_temp( + res.testType)).replace( + "@TestFeature", string_temp( + res.testFeature)).replace( + "@TestName", string_temp( + res.testName)).replace( + "@TestResult", string_temp( + res.testResult)).replace( + "@Notes", string_temp( + res.notes)).replace( + "@Creation_time", string_temp( + 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 + + 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 + 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'): +# List all tests, or create a new test. +def testResult_list_to_mail(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) + 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) - + 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)) + 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['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) + 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) + 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'): +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) + print("BUILD_REPORT_NUM = " + settings.ICE_BUILD_REPORT_NUM) + + testResults = TestResults.objects.filter( + build_id=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) - 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)) + 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['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" + 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) + 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 "" + return "" + -class JSONResponse(HttpResponse): # An HttpResponse that renders its content into JSON. +# An HttpResponse that renders its content into JSON. +class JSONResponse(HttpResponse): def __init__(self, data, **kwargs): content = JSONRenderer().render(data) kwargs['content_type'] = 'application/json' -- cgit 1.2.3-korg