我和这个人度过了最糟糕的时光。在视图中,我创建了一个保存到内存的 csv 文件。我需要将该 csv 文件发送到 utils.py 函数并发布到外部 api。我一辈子都不知道该怎么做,这真的让我发疯。
我最初只是尝试在下面的 run_test_suite_in_hatit 函数中创建它,然后以某种方式在下面的 run_all_modal 中打开它,但这不起作用。下面发生的事情是文件 (hatit_csv_filename) 现在是一个消息对象。我不想将其作为临时保存到模型中,并且创建纯粹是为了在 HATScript() 内的 api 帖子中直接发送,该帖子位于我的项目中同一应用程序内的 utils.py 文件中。我不知道如何将文件发送到 HATScript(),这让我抓狂。
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})
编辑:已解决
我一直在为此绞尽脑汁,很明显我需要做什么!
更新的视图功能:
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')
我试图发送到类中的函数并用适当的数据填充变量,但由于数据来自两个不同的视图,我对如何实现这一点感到非常困惑。我尝试过很多可笑的事情。我删除了所有专有位。
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
本质上,我能够在 run_test_suite_in_hatit(request) 函数中将变量分配给文件,并且必须这个问题有帮助我了解如何使用类变量并为其赋值等。这帮助我解决了我长期以来一直遇到的问题,以至于我在周六晚上开始更多地研究它。