使用Django / Python在

时间:2017-04-22 22:31:31

标签: django python-3.x csv

我和这个人度过了最糟糕的时光。在视图中,我创建了一个保存到内存的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')

我试图发送到一个类中的函数并使用适当的数据填充变量,但因为数据来自2个不同的视图,我自己就如何实现这一点。我尝试过这么多荒谬的事情。我删除了任何专有位。

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(请求)函数中为变量分配变量,并且必须this SO question helped我理解使用类变量并分配给它们等等。这帮助我解决了问题我'我已经有很长时间了,所以我在周六晚上更多地开始工作。

0 个答案:

没有答案