Ejemplo n.º 1
0
Archivo: views.py Proyecto: AKMFCJ/tms
def version_test(version_id):
    """版本测试结果"""

    # 显示版本信息
    if request.method == 'GET':
        for index, breadcrumb in enumerate(session['breadcrumbs']):
            if u'版本测试' == breadcrumb['text']:
                session['breadcrumbs'] = session['breadcrumbs'][:index+1]
                break
        else:
            session['breadcrumbs'].append({'text': '版本测试',
                                           'url': url_for('qa.version_test', version_id=version_id)})

        version = Version.query.filter_by(id=version_id).first()
        tasks = []

        # 测试权限
        test_permission = False
        # 发布全新
        release_permission = False

        if session['user']['role_name'] == 'os_test' and version.status == '测试':
            test_permission = True

        if session['user']['role_name'] == 'os_test' and version.status == '测试通过':
            release_permission = True

        if version.tasks:
            tasks = json.loads(version.tasks, strict=False)

        if version.documents and version.documents.strip():
            #documents = [(os.path.basename(tmp), os.path.getsize(tmp)/1024, tmp.replace('/', '<>')) for tmp in
            # version.documents.split('#') if tmp.strip()]
            documents = [(os.path.basename(tmp), '', tmp.replace('/', '<>')) for tmp in version.documents.split('#') if tmp.strip()]
        else:
            documents = []
        version_level = ['Recommend', 'Must']

        return render_template('/qa/version_test.html',
                               documents=documents,
                               version=version,
                               test_permission=test_permission,
                               release_permission=release_permission,
                               tasks=tasks,
                               version_level=version_level,
                               current_page='qa.my_version')
    # 版本测试结果
    elif request.method == 'POST':
        version = Version.query.filter_by(id=version_id).first()
        project = version.project
        version.release_note = request.form.get('release_note', '')
        version.version_issues = request.form.get('version_issues', '')
        version.unresolved_issues = request.form.get('unresolved_issues', '')
        version.testing_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        version.test_user = current_user
        smi = request.form.get('smi', '')
        if smi:
            version.smi = int(smi)
        version.level = request.form.get('level', 'Recommend')
        result = request.form.get('result', '')
        version.special_version.release_path = request.form.get('release_path', '')

        # 测试通过后发送邮件地址
        test_pass_to_email = request.form.get('test_pass_to_email', '')
        if test_pass_to_email:
            project.test_pass_to_email = ';'.join([tmp.strip() for tmp in test_pass_to_email.split(';') if tmp.strip()])

        # 测试失败发送的邮件地址
        test_failure_to_email = request.form.get('test_failure_to_email', '')
        if test_failure_to_email:
            project.test_failure_to_email = ';'.join([tmp.strip() for tmp in test_failure_to_email.split(';') if tmp.strip()])

        # 选择发放版本,默认是全部
        need_release_file = request.form.get('need_release_file', '')
        if need_release_file:
            version.release_file = '#'.join([tmp.strip() for tmp in need_release_file.split('#') if tmp.strip()])
        else:
            version.release_file = version.ftp_file

        # 保存上传的附件
        exits_documents = [tmp.strip() for tmp in version.documents.split('#') if tmp.strip()]
        documents = request.files.getlist("file[]")

        if documents:
            upload_path = os.path.join(current_app.config['UPLOAD_FOLDER'], version.name, version.version_number)
            if not os.path.exists(upload_path):
                os.makedirs(upload_path)
            for document in documents:
                if document.filename:
                    current_app.logger.info(document.filename)
                    file_path = os.path.join(upload_path, document.filename)
                    # 删除已存在的
                    if os.path.exists(file_path) and os.path.isfile(file_path):
                        os.remove(file_path)

                    document.save(file_path)
                    if file_path not in exits_documents:
                        exits_documents.append(file_path)

        version.documents = '#'.join(exits_documents)

        # 通知邮件
        to_list = []
        cc_list = []
        # 版本编译工程师
        to_list.append(version.compile_email)
        # 软件开发负责人
        if project.develop_leader and project.develop_leader.email:
            to_list.append(project.develop_leader.email)
        # 软件的邮件记录
        if project.to_email:
            for email in project.to_email.split(';'):
                if email.strip():
                    cc_list.append(email)

        # 版本历史记录
        version_history = VersionHistory()

        # 测试不通过不能发放
        if result == '-1':
            if project.test_failure_to_email:
                for email in project.test_failure_to_email.split(';'):
                    if email.strip():
                        to_list.append(email)

            version.status = '测试不过'
            version.testing_result = False
            version_history.description = "{0}版本测试结果: 测试失败\n 测试人:{1}\n版本缺陷:{2}\n遗留问题{3}".format(
                version.name, current_user.username, version.version_issues, version.unresolved_issues)
            # 去重复
            to_list = list(set(to_list))
            cc_list = list(set(cc_list))
            msg = Message(
                '[TSDS] %s_%s 版本测试不通过' % (version.name, version.version_number),
                sender='*****@*****.**',
                # recipients=[version.project.test_leader.email, '*****@*****.**', '*****@*****.**']
                recipients=to_list,
                cc=cc_list
                # recipients=['*****@*****.**']
            )
            msg.html = TEST_FAILURE_TEMPLATE.format(version.project.name,
                                                    version.name,
                                                    version.version_number,
                                                    version.level,
                                                    version.version_issues,
                                                    current_user.display_name,
                                                    current_user.email).replace('通讯录', '通 讯 录')
            mail.send(msg)

            flash(u'提交成功', 'info')
        # 版本废弃
        elif result == '4':
            version.status = '已废弃'
            version.version_discard_time = datetime.now()
            version.discard_user_id = current_user.id

            db.session.add(version)

        # 研发版本测试结果通知
        elif result == '5':
            version.status = '测试通过'
            version.test_user_id = current_user.id
            version.testing_result = True
            version.testing_time = datetime.now()

            db.session.add(version)

            # 版本测试通过后需要通知的人员
            if project.test_pass_to_email:
                for email in project.test_pass_to_email.split(';'):
                    if email.strip():
                        to_list.append(email)
            # 配置组
            to_list.append('*****@*****.**')
            for scm in User.query.join(Role).filter(Role.name == 'scm').all():
                cc_list.append(scm.email)

            to_list = list(set(to_list + cc_list))
            content = VERSION_TEST_TEMPLATE.format(project.name,
                                              version.name,
                                              version.version_number,
                                              version.smi,
                                              version.version_issues.replace('<', '&lt;').replace('\n', '<br>'),
                                              version.release_note.replace('<', '&lt;').replace('\n', '<br>'),
                                              current_user.display_name,
                                              current_user.email).replace('通讯录', '通 讯 录')
            message = {
                'to_list': ';'.join([tmp for tmp in to_list if tmp]),
                'sub': '[TSDS] {0}_{1}版本,测试结果'.format(project.name, version.version_number),
                'content': content
            }
            send_mail_pubisher(json.dumps(message))

            version_history.description = "{0}版本测试结果: 测试通过,测试通过\n 测试人:{1}\n版本缺陷:{2}\n遗留问题{3}" \
                                          "\n发布说明{4}\n适配信息{5}\n导入注意事项{6}".format(version.name,
                                                                                 current_user.username,
                                                                                 version.version_issues,
                                                                                 version.unresolved_issues,
                                                                                 version.release_note,
                                                                                 version.adaptation_note,
                                                                                 version.project_import_note)
            flash(u'提交成功', 'info')

        # 测试通过进行版本发放
        else:
            # 版本测试通过后需要通知的人员
            if project.test_pass_to_email:
                for email in project.test_pass_to_email.split(';'):
                    if email.strip():
                        to_list.append(email)
            # 配置组
            to_list.append('*****@*****.**')
            for scm in User.query.join(Role).filter(Role.name == 'scm').all():
                cc_list.append(scm.email)

            version.status = '已发布'
            version.testing_result = True
            version.release_time = datetime.now()
            version.release_user_id = current_user.id
            version.release_note = request.form.get('release_note', '')
            version.adaptation_note = request.form.get('adaptation_note', '')
            version.project_import_note = request.form.get('project_import_note', '')

            project.last_release_time = version.release_time
            project.last_version_number = version.version_number

            if result == '1':
                # version.release_type = '特殊发放'
                # msg = Message(
                #     '[TSDS] {0}_{1} 特殊发放'.format(project.name, version.version_number),
                #     sender='*****@*****.**',
                #     recipients=to_list,
                #     cc=cc_list
                #     # recipients=['*****@*****.**']
                # )
                # if version.documents:
                #     for document in version.documents.split('#'):
                #         if document:
                #             file_name = os.path.basename(document)
                #             with current_app.open_resource(document) as fp:
                #                 msg.attach(file_name.encode("utf-8"),
                #                            current_app.config['MIME_TYPE'][os.path.splitext(file_name)[1]], fp.read())
                #
                # msg.html = SPECIAL_VERSION_TEMPLATE.format(project.name,
                #                                            version.name,
                #                                            version.version_number,
                #                                            version.smi,
                #                                            version.level,
                #                                            version.project_import_note,
                #                                            project.apk_name,
                #                                            project.apk_package,
                #                                            version.special_version.release_path,
                #                                            version.release_note.replace('\n', '<br>'),
                #                                            version.unresolved_issues.replace('\n', '<br>'),
                #                                            current_user.display_name,
                #                                            current_user.email)
                # mail.send(msg)
                content = SPECIAL_VERSION_TEMPLATE.format(project.name,
                                                           version.name,
                                                           version.version_number,
                                                           version.smi,
                                                           version.level,
                                                           version.project_import_note.replace('<', '&lt;'),
                                                           project.apk_name,
                                                           project.apk_package,
                                                           version.special_version.release_path,
                                                           version.release_note.replace('<', '&lt;').replace('\n', '<br>'),
                                                           version.unresolved_issues.replace('<', '&lt;').replace('\n', '<br>'),
                                                           current_user.display_name,
                                                           current_user.email).replace('通讯录', '通 讯 录')
                to_list = list(set(to_list + cc_list))
                message = {
                    'to_list': ';'.join([tmp for tmp in to_list if tmp]),
                    'sub': '[TSDS] {0}_{1} 特殊发放'.format(project.name, version.version_number),
                    'content': content
                }
                send_mail_pubisher(json.dumps(message))

            elif result == '2':
                version.release_type = 'DCC发放'
                # msg = Message(
                #     '[TSDS] {0}_{1}版本,DCC发放'.format(project.name, version.version_number),
                #     sender='*****@*****.**',
                #     recipients=to_list,
                #     cc=cc_list
                #     # recipients=['*****@*****.**']
                # )
                # if version.documents:
                #     for document in version.documents.split('#'):
                #         if document:
                #             file_name = os.path.basename(document)
                #             with current_app.open_resource(document) as fp:
                #                 msg.attach(file_name.encode("utf-8"),
                #                            current_app.config['MIME_TYPE'][os.path.splitext(file_name)[1]], fp.read())
                #     msg.html = DCC_RELEASE_TEMPLATE.format(project.name,
                #                                            version.name,
                #                                            version.version_number,
                #                                            version.smi,
                #                                            version.level,
                #                                            project.apk_name,
                #                                            project.apk_package,
                #                                            version.special_version.release_path,
                #                                            version.release_note.replace('\n', '<br>'),
                #                                            project.name,
                #                                            version.adaptation_note,
                #                                            version.unresolved_issues.replace('\n', '<br>'),
                #                                            current_user.display_name,
                #                                            current_user.email)
                #
                #     mail.send(msg)
                to_list = list(set(to_list + cc_list))
                content = DCC_RELEASE_TEMPLATE.format(project.name,
                                                       version.name,
                                                       version.version_number,
                                                       version.smi,
                                                       version.level,
                                                       project.apk_name,
                                                       project.apk_package,
                                                       version.special_version.release_path,
                                                       version.release_note.replace('<', '&lt;').replace('\n', '<br>'),
                                                       project.name,
                                                       version.adaptation_note.replace('<', '&lt;'),
                                                       version.unresolved_issues.replace('<', '&lt;').replace('\n', '<br>'),
                                                       current_user.display_name,
                                                       current_user.email).replace('通讯录', '通 讯 录')
                message = {
                    'to_list': ';'.join([tmp for tmp in to_list if tmp]),
                    'sub': '[TSDS] {0}_{1}版本,DCC发放'.format(project.name, version.version_number),
                    'content': content
                }
                send_mail_pubisher(json.dumps(message))

            elif result == '3':
                version.release_type = '迭代发放'
                # msg = Message(
                #     '[TSDS] {0}_{1}版本,迭代发放'.format(project.name, version.version_number),
                #     sender='*****@*****.**',
                #     recipients=to_list,
                #     cc=cc_list
                #     # recipients=['*****@*****.**']
                # )
                # if version.documents:
                #     for document in version.documents.split('#'):
                #         if document:
                #             file_name = os.path.basename(document)
                #             with current_app.open_resource(document) as fp:
                #                 msg.attach(file_name.encode("utf-8"),
                #                            current_app.config['MIME_TYPE'][os.path.splitext(file_name)[1]], fp.read())
                # msg.html = RELEASE_TEMPLATE.format(project.name,
                #                                    version.name,
                #                                    version.version_number,
                #                                    version.smi,
                #                                    version.level,
                #                                    project.apk_name,
                #                                    project.apk_package,
                #                                    version.special_version.release_path,
                #                                    version.release_note.replace('\n', '<br>'),
                #                                    version.unresolved_issues.replace('\n', '<br>'),
                #                                    current_user.display_name,
                #                                    current_user.email)
                # mail.send(msg)
                to_list = list(set(to_list + cc_list))
                content = RELEASE_TEMPLATE.format(project.name,
                                                   version.name,
                                                   version.version_number,
                                                   version.smi,
                                                   version.level,
                                                   project.apk_name,
                                                   project.apk_package,
                                                   version.special_version.release_path,
                                                   version.release_note.replace('<', '&lt;').replace('\n', '<br>'),
                                                   version.unresolved_issues.replace('<', '&lt;').replace('\n', '<br>'),
                                                   current_user.display_name,
                                                   current_user.email).replace('通讯录', '通 讯 录')
                message = {
                    'to_list': ';'.join([tmp for tmp in to_list if tmp]),
                    'sub': '[TSDS] {0}_{1}版本,迭代发放'.format(project.name, version.version_number),
                    'content': content
                }
                send_mail_pubisher(json.dumps(message))

            version_history.description = "{0}版本测试结果: 测试通过,已发布\n 测试人:{1}\n版本缺陷:{2}\n遗留问题{3}" \
                                          "\n发布说明{4}\n适配信息{5}\n导入注意事项{6}".format(version.name,
                                                                                 current_user.username,
                                                                                 version.version_issues,
                                                                                 version.unresolved_issues,
                                                                                 version.release_note,
                                                                                 version.adaptation_note,
                                                                                 version.project_import_note)
            flash(u'发布成功', 'info')

        version_history.version_id = version_id
        version_history.option_username = session['user']['username']
        version_history.option_email = session['user']['email']
        version_history.option_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')

        db.session.add(version)
        db.session.add(version_history)
        db.session.add(version.special_version)
        db.session.add(project)
        db.session.commit()

        return redirect(url_for('qa.version_test', version_id=version_id))