I'm having the worst time with this one. In a view I created a csv file that's saved to memory. I need to get that csv file to a utils.py function and post to an external api. I for the life of me can not figure out how to do this and it's really driving me nuts.
I originally was just trying to create it in the run_test_suite_in_hatit function below and then somehow open it in the run_all_modal below but that wasn't working. What occurred below was the file (hatit_csv_filename) was now a message object. I don't want to save it to a model as its temporary and is being created purely to be sent right in a api post within HATScript() which is in a utils.py file within the same app in the my project. I'm not sure how to get the file to HATScript() which is just making me nuts.
def run_test_suite_in_hatit(request):
testrail_suite_id = int(request.GET['suite'])
print(testrail_suite_id)
testrail_instance = TestRailInstance.objects.first()
project = request.user.humanresource.project
testrail_project_id = project.testrail.project_id
testrail_project = get_testrail_project(testrail_instance, testrail_project_id)
testrail_suites = testrail_project.get_suites()
testrail_suite = [s for s in testrail_suites if s.id == testrail_suite_id][0]
testrail_cases = testrail_suite.get_cases()
hatit_csv_filename = bulk_hatit_file_generator(testrail_cases)
messages.add_message(request, messages.INFO, hatit_csv_filename)
return HttpResponseRedirect('/run_all_modal/')
def run_all_modal(request):
if request.method == 'POST':
form = TestRunnerForm(request.POST)
if form.is_valid():
data = form.cleaned_data
scripts = get_messages(request)
csvfile = ""
for script in scripts:
csvfile = script
hs = HATScript()
hs.apn = data.get('apn')
hs.holly_server = data.get('browser')
hs.basic_connection_test()
response = hs.hatit_execute()
else:
print(form.errors)
return JsonResponse({'success': True})
EDIT: SOLVED I have been bashing my head over this and it was so obvious what I needed to do !
Updated View functions:
def run_test_suite_in_hatit(request):
testrail_suite_id = int(request.GET['suite'])
testrail_instance = TestRailInstance.objects.first()
project = request.user.humanresource.project
testrail_project_id = project.testrail.project_id
testrail_project = get_testrail_project(testrail_instance, testrail_project_id)
testrail_suites = testrail_project.get_suites()
testrail_suite = [s for s in testrail_suites if s.id == testrail_suite_id][0]
testrail_cases = testrail_suite.get_cases()
hatit_csv_filename = bulk_hatit_file_generator(testrail_cases)
**HATScript.csvfile = hatit_csv_filename**
return 'runner/run_all_modal.html'
def run_all_modal(request):
if request.method == 'POST':
form = TestRunnerForm(request.POST)
if form.is_valid():
data = form.cleaned_data
hs = HATScript()
**hs.csvfile = HATScript.csvfile**
hs.apn = data.get('apn')
hs.holly_server = data.get('browser')
response = hs.hatit_execute()
print(response.status_code)
else:
print(form.errors)
return redirect('runner:dashboard')
I was trying to send to a function within a class and populate the variables with the appropriate data but because the data was coming from 2 different Views I was beside myself on how to achieve this. I have tried so many ridiculous things. I removed any proprietary bits.
class HATScript(AutomationScript):
def __init__(self, apn='', body='', dialed_number='',
holly_server='', sonus_server='',
hatit_server='',
remote_server=' ', remote_user='', remote_password=''):
self.csvfile = ''
self.hatit_server = hatit_server
self.apn = apn
self.dialed_number = dialed_number
self.body = body
self.filename = ''
self.holly_server = holly_server
self.sonus_server = sonus_server
self.remote_server = remote_server
self.remote_user = remote_user
self.remote_password = remote_password
def hatit_execute(self):
"""Uses Frank's HAT User Interface to initate a HAT test"""
browser = requests.session()
stuff = browser.get('http://{0}/hatit'.format(self.remote_server))
print(stuff.status_code)
data = {'apn': self.apn,
'browser': self.holly_server,
'port': '5060'}
response = browser.post("http://{0}/".format(self.hatit_server), data=data, files={'csvfile': open(self.csvfile)})
print(response.text)
browser.close()
return response
Essentially I was able to assign the variable the file in the run_test_suite_in_hatit(request) function and had to this SO question helped me understand working with Class variables and assigning to them etc. Which helped me solve the issue I've been having for so long that I came into work on it more on a Saturday night.