Ejemplo n.º 1
0
    def action(self, request, data_dict):
        rm = response_message()
        user_id = 0
        user_name = 'System'
        if request:
            user_id = request.user.id
            user_name = request.user.username

        logging.debug('Action')
        logging.debug(
            '----------------------------------------------------------------------'
        )
        logging.debug(data_dict)
        entity = data_dict.get('entity', None)
        try:
            if entity:
                id = entity['id']
                action = data_dict['action']
                email = data_dict['email']
                tracking_code = data_dict['tracking_code']
                location = data_dict['location']
                transaction_id = data_dict['transaction_id']
                comments = data_dict['comments']
                obj = RaEntity.objects.get(id=id)

                nw = datetime.datetime.now()
                lc = RaLogController()

                ra_type = entity['ra_type']

                if action == 'APPROVE':  # state value = 10
                    if obj.state == '10':
                        rm.code = 10
                        rm.message = 'Ra state is [Processing], nothing to do!'
                        return rm

                    if obj.is_approved:
                        rm.code = 10
                        rm.message = 'Ra status is [is_approved], nothing to do!'
                        return rm

                    obj.state = '10'
                    obj.status = '10'
                    obj.is_approved = True
                    obj.approved_at = nw

                    lc.add(request, obj.id, obj.type, action, action, comments)

                    if ra_type == 'CPN':
                        obj.email_to = email
                        email_subject = '[%s]-Coupon Create Notice' % entity[
                            'label_id']
                        email_text = 'Coupon Amount:[$%s][%s]' % (
                            entity['amount'], entity['created_at'])

                        se = SendEmail()
                        se.send_email(obj.email_to, email_subject, email_text)
                        lc.add(request, obj.id, obj.type, 'EMAIL_TO',
                               'Email TO: %s' % obj.email_to, email_text)

                if action == 'BUY_LABEL':  # status value = 20
                    if obj.state != '10':
                        rm.code = 10
                        rm.message = 'Ra state must be [Processing], nothing to do!'
                        return rm
                    if obj.is_label:
                        rm.code = 20
                        rm.message = 'Ra status is [Label], nothing to do!'
                        return rm

                    obj.status = '20'
                    obj.is_label = True
                    obj.label_at = nw
                    obj.tracking_code = tracking_code
                    lc.add(request, obj.id, obj.type, action, action, comments)

                if action == 'STOCK_IN':  # status value = 20
                    if obj.state != '10':
                        rm.code = 10
                        rm.message = 'Ra state must be [Processing], nothing to do!'
                        return rm
                    if obj.is_stock:
                        rm.code = 30
                        rm.message = 'Ra status is [STOCK_IN], nothing to do!'
                        return rm

                    obj.status = '30'
                    obj.is_stock = True
                    obj.stock_at = nw
                    obj.location = location

                    logging.debug('location: %s' % location)

                    # 调用入库
                    irc = inventory_receipt_control()
                    warehouse_code = 'USRW01'
                    items = obj.get_items()
                    for item in items:
                        doc_number = "%s-%s" % (obj.id, item.id)
                        irc.add(request, doc_number, warehouse_code,
                                item.frame, 0, 'REFUNDS_IN', item.quantity)

                    if ra_type != 'CPN':
                        obj.email_to = email
                        email_subject = '[%s]-Refund Notice' % entity[
                            'label_id']
                        email_text = 'Refund Amount:[$%s][%s]' % (
                            entity['amount'], entity['created_at'])

                        se = SendEmail()
                        se.send_email(obj.email_to, email_subject, email_text)
                        lc.add(request, obj.id, obj.type, 'EMAIL_TO',
                               'Email TO: %s' % obj.email_to, email_text)

                    if ra_type == 'RMK':
                        rm = self.refund_ra(obj)

                if action == 'REFUND':  # status value = 20
                    if obj.state != '10':
                        rm.code = 10
                        rm.message = 'Ra state must be [Processing], nothing to do!'
                        return rm

                    if obj.ra_type == 'CPN':
                        rm.code = 33
                        rm.message = 'Ra Type is [CPN], nothing to do!'
                        return rm

                    if not obj.is_stock:
                        rm.code = 30
                        rm.message = 'Ra status must be [STOCK_IN], nothing to do!'
                        return rm
                    if obj.is_refund:
                        rm.code = 40
                        rm.message = 'Ra status is [REFUND], nothing to do!'
                        return rm

                    rm = self.refund_ra(obj)

                    lc.add(request, obj.id, obj.type, action, action, comments)

                if action == 'COUPON':  # status value = 20
                    if obj.state != '10':
                        rm.code = 10
                        rm.message = 'Ra state must be [Processing], nothing to do!'
                        return rm

                    if obj.ra_type != 'CPN':
                        rm.code = 33
                        rm.message = 'Ra Type must be [CPN], nothing to do!'
                        return rm

                    if obj.is_refund:
                        rm.code = 40
                        rm.message = 'Ra status is [REFUND], nothing to do!'
                        return rm

                    obj.transaction_id = transaction_id

                    rm = self.refund_ra(obj)

                    lc.add(request, obj.id, obj.type, action, action, comments)

                if action == 'CLOSE':  # status value = 20
                    if obj.state != '10':
                        rm.code = 10
                        rm.message = 'Ra state must be [Processing], nothing to do!'
                        return rm

                    obj.state = '901'
                    obj.closed_at = nw

                    lc.add(request, obj.id, obj.type, action, action, comments)

                if action == 'CANCEL':  # status value = 20
                    if obj.state != '0':
                        rm.code = 10
                        rm.message = 'Ra state must be [Processing], nothing to do!'
                        return rm

                    obj.state = '902'
                    obj.canceled_at = nw

                    lc.add(request, obj.id, obj.type, action, action, comments)

                obj.comments += '\r\n' + comments
                obj.save()

        except Exception as ex:
            rm.capture_execption(ex)
            logging.error(str(ex))
        return rm
# 此文件是项目执行的入口
import unittest
from util.HTMLTestRunner import HTMLTestRunner
import time
from conf.setting import report_dir
from conf.setting import test_dir
from  util.send_email import SendEmail

# 指定测试用例 的路径
discover = unittest.defaultTestLoader.discover(test_dir,pattern='test_case.py')
now=time.strftime('%Y_%m_%d_%H_%M_%S')
# 指定生成的测试报告的文件名
report_name=report_dir+'/'+now+' test_report.html'
# 将测试结果写入测试报告
with open (report_name,'wb') as f:
    runner = HTMLTestRunner(stream=f,title='Vincent Qichacha API Test Report',description=' Qichacha API Test Report By Vincent ',verbosity=2)
    runner.run(discover)
# 将测试报告发送到指定的邮箱
send_email_report = SendEmail()
send_email_report.send_email(report_name)

#     @ddt.data(["admin","Aa1234"],["admin1","Bb1234"])

    @ddt.data(*ex_data)
    @ddt.unpack
    def test_login_case(self, username, password):
        success = self.login_b.login_success(username, password)
        self.assertTrue(success, '登录失败')

if __name__ == "__main__":
    suite = unittest.TestLoader().loadTestsFromTestCase(Ddtcase)
    report_name = datetime.datetime.now().strftime(
        '%Y-%m-%d-%H-%M-%S') + '.html'
    report_file = os.path.join(setting.report_dir, report_name)
    with open(report_file, 'wb') as f:
        runner = HTMLTestRunner(stream=f,
                                title="This is the first ddt_case report1",
                                description="这个是我们第一次测试报告 --数据驱动",
                                verbosity=2)
        runner.run(suite)

    last_report = Get_Latest_Report()
    report_file = last_report.latest_file()
    #     print(report_file)
    send = SendEmail()
    send.send_email(report_file)

# if __name__ == '__main__':
#    unittest.main()