コード例 #1
0
def export_config(request):
    """
    导出配置
    :param request:
    :return:
    """
    try:
        file_name = 'export_config_%s.json' % datetime.today().strftime(
            '%Y%m%d')
        json_data = get_export_config_json()
        config_data = json_dumps(json_data, indent=4, sort_keys=True)

        agent = request.META.get('HTTP_USER_AGENT')
        if agent and re.search('MSIE', agent):
            # 解决ie不能下载的问题
            response = HttpResponse(content=config_data,
                                    content_type="text/plain; charset=utf-8")
            # 解决文件名乱码/不显示的问题
            response[
                'Content-Disposition'] = 'attachment; filename=%s' % urlquote(
                    file_name)
        else:
            response = HttpResponse(content=config_data,
                                    content_type="text/plain; charset=utf-8")
            response[
                'Content-Disposition'] = 'attachment; filename=%s' % smart_str(
                    file_name)

        return response
    except Exception as e:
        logger.error(e.message)
        logger.error(traceback.format_exc())
        return HttpResponse('导出配置失败 %s' % e.message)
コード例 #2
0
    def test_order_update_deposit(self):
        csettings = CashBackSettings()
        csettings.threshold = "1"

        minimal = self.minimal_example

        order = self.app.pay.create_order(self.wechat_user, self.request, **minimal)
        result = self.success(self.app.pay, order)

        old_hold = self.fund.hold

        order.update(result)

        # test deposit
        new_fund = Fund.objects.get(id=self.fund.id)
        self.assertEqual(self.fund.cash + to_decimal("1.01"), new_fund.cash)
        self.assertEqual(old_hold + to_decimal("1.01"), new_fund.hold)
        self.assertEqual(self.fund.total + to_decimal("2.02"), new_fund.total)
        transfer = FundTransfer.objects.get(
            to_fund=self.fund, order_id=order.id, type="DEPOSIT"
        )
        self.assertEqual(transfer.note, f"user:{self.shop_user.id} deposit")

        # test cashback
        cashback_transfer = FundTransfer.objects.get(
            to_fund=self.fund, order_id=order.id, type="CASHBACK"
        )
        self.assertEqual(cashback_transfer.amount, to_decimal("1.01"))

        fund_action = FundAction.objects.get(fund=self.fund, transfer=cashback_transfer)
        self.assertDictEqual(
            fund_action.balance, json.loads(json_dumps(new_fund.amount_d))
        )
コード例 #3
0
def export_config(request):
    """
    导出配置
    :param request:
    :return:
    """
    try:
        file_name = 'export_config_%s.json' % datetime.today().strftime('%Y%m%d')
        json_data = get_export_config_json()
        config_data = json_dumps(json_data, indent=4, sort_keys=True)

        agent = request.META.get('HTTP_USER_AGENT')
        if agent and re.search('MSIE', agent):
            # 解决ie不能下载的问题
            response = HttpResponse(content=config_data, content_type="text/plain; charset=utf-8")
            # 解决文件名乱码/不显示的问题
            response['Content-Disposition'] = 'attachment; filename=%s' % urlquote(file_name)
        else:
            response = HttpResponse(content=config_data, content_type="text/plain; charset=utf-8")
            response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)

        return response
    except Exception as e:
        logger.error(e.message)
        logger.error(traceback.format_exc())
        return HttpResponse('导出配置失败 %s' % e.message)
コード例 #4
0
def transfer_to_redis(request):
    """
    将配置数据同步到Redis中
    """
    success, msg = False, ''
    try:
        config_data = get_config_redis_json()
        logger.debug(config_data)
        r = redis.StrictRedis(host=settings.REDIS_HOST,
                              port=settings.REDIS_PORT,
                              db=settings.REDIS_DB,
                              password=settings.REDIS_PASSWORD)

        # 默认transaction=True
        pipe = r.pipeline(transaction=True)
        # 按模式匹配批量删除
        pattern_delete_lua = """
        local keys = redis.call('keys', ARGV[1])
        for i = 1, table.getn(keys) do
            redis.call('del', keys[i])
        end
        """
        pattern_delete = r.register_script(pattern_delete_lua)
        pattern_delete(keys=[''],
                       args=['%s:*' % settings.CLIENT_CONFIG_REDIS_PREFIX],
                       client=pipe)

        for t in config_data:
            logger.debug(t)
            #
            # client = {}
            # for k, v in t.iteritems():
            #     if k != 'endpoints':
            #         client[k] = v
            pipe.set(
                '%s:%s' % (settings.CLIENT_CONFIG_REDIS_PREFIX, t['app_id']),
                json_dumps(t))

            # for s in t['endpoints']:
            #     pipe.set('%s:%s:%s:%s' % (settings.PROXY_CONFIG_REDIS_PREFIX, t['access_key'], s['name'], s['version']),
            #              json_dumps(s))
        # pipe.delete('config:*')

        # the EXECUTE call sends all buffered commands to the server, returning
        # a list of responses, one for each command.
        pipe.execute()
        success = True
    except Exception as e:
        msg = '同步配置数据到 Redis 出现异常'
        logger.error(e.message)
        logger.error(traceback.format_exc())

    return http_response_json({'success': success, 'msg': msg})
コード例 #5
0
def transfer_to_redis(request):
    """
    将配置数据同步到Redis中
    """
    success, msg = False, ''
    try:
        config_data = get_config_redis_json()
        logger.debug(config_data)
        r = redis.StrictRedis(host=settings.REDIS_HOST, port=settings.REDIS_PORT,
                              db=settings.REDIS_DB, password=settings.REDIS_PASSWORD)

        # 默认transaction=True
        pipe = r.pipeline(transaction=True)
        # 按模式匹配批量删除
        pattern_delete_lua = """
        local keys = redis.call('keys', ARGV[1])
        for i = 1, table.getn(keys) do
            redis.call('del', keys[i])
        end
        """
        pattern_delete = r.register_script(pattern_delete_lua)
        pattern_delete(keys=[''], args=['%s:*' % settings.CLIENT_CONFIG_REDIS_PREFIX], client=pipe)

        for t in config_data:
            logger.debug(t)
            #
            # client = {}
            # for k, v in t.iteritems():
            #     if k != 'endpoints':
            #         client[k] = v
            pipe.set('%s:%s' % (settings.CLIENT_CONFIG_REDIS_PREFIX, t['access_key']), json_dumps(t))

            # for s in t['endpoints']:
            #     pipe.set('%s:%s:%s:%s' % (settings.PROXY_CONFIG_REDIS_PREFIX, t['access_key'], s['name'], s['version']),
            #              json_dumps(s))
        # pipe.delete('config:*')

        # the EXECUTE call sends all buffered commands to the server, returning
        # a list of responses, one for each command.
        pipe.execute()
        success = True
    except Exception as e:
        msg = u'同步配置数据到 Redis 出现异常'
        logger.error(e.message)
        logger.error(traceback.format_exc())

    return http_response_json({'success': success, 'msg': msg})