Ejemplo n.º 1
0
    def test_json(self):
        a = User()
        a.name = "'\;sd"
        print(a.optimistic_save())
        print(a.to_json())
        print(User.objects.all())
        from framework.utils import json_dumps

        query_set = User.objects.all()
        d = {"s": query_set, "b": "1"}
        jd = json_dumps(d)
        self.assertEqual(jd, json_dumps(json.loads(jd)))
Ejemplo n.º 2
0
    def test_setParamsSerializer(self):
        from ..serializer import ParamsSerializer, s
        from myadmin.models.user import User

        class AdminSerializer(BaseModelSerializer):
            f1 = s.CharField(label=_('f1'), default='default f1')
            f2 = s.CharField(label=_('f2'),
                             default='default f1',
                             allow_null=True,
                             required=False)

            class Meta:
                model = User
                fields = '__all__'

        u = User()
        u.username = '******'
        u.id = 3
        a = AdminSerializer(instance=u,
                            data={
                                'username': '******',
                                'alias': 'alias'
                            }).o

        a.f1 = 'f1'
        a.username = '******'
        self.assertEqual(a.f1, 'f1')
        self.assertTrue(a.get('f1'), 'f1')
        self.assertEqual(a.username, 'c_username')
        self.assertTrue(a.get('username'), 'c_username')

        class ChangePasswordSerializer(ParamsSerializer):
            old_password = s.CharField(label=_('旧密码'))
            new_password1 = s.CharField(label=_('新密码'), default='3')
            new_password2 = s.CharField(label=_('确认密码'))

        ss = ChangePasswordSerializer()
        ss.o.old_password = '******'
        ss.new_password2 = 'asd'
        #        ss.validation()
        self.assertEqual(ss.data.old_password, 'asd')

        from framework.utils import json_dumps
        self.assertEqual(json_dumps(ss),
                         json_dumps(json.loads(json_dumps(ss))))
Ejemplo n.º 3
0
    def test_RspData(self):
        from ..response import RspData
        # @dataclass

        a = RspData(3, 54, 6)
        a.code
        print(a, a.code, a.msg)
        a.code = None
        a.msg = '123'
        print(a, a.code)
        self.assertEqual(a.code, None)
        print(json_dumps(a))
Ejemplo n.º 4
0
    def test_response(self):
        self.assertEqual(0, RspData(data={}, msg='asd').code)
        rsp = RspData()

        rsp.code = 1001
        print(rsp.code)
        print(rsp['code'])
        self.assertEqual(1001, rsp.code)
        self.assertEqual(rsp.code, 1001)
        err = RspError()
        print(err, err.code)
        print(json_dumps(rsp))
        print(rsp)
Ejemplo n.º 5
0
def interface(request):
    """
    兼容旧接口
    :param request:
    :return:
    """
    key = request.GET.get('key')
    _r = {}
    if key:
        o = DictDefine.objects.filter(key=key).first()
        if o:
            _r = o.get_dict()
    return HttpResponse(json_dumps(_r))
Ejemplo n.º 6
0
def query_export_do(request, query_compiler: QueryCompiler):
    """查询导出文件
    """

    _g = request.REQUEST.get
    _gl = request.REQUEST.getlist
    export_key = _g('export_key', '')
    is_finish = _g('is_finish', '')  # 完成标记 合并文件
    export_type = int(_g('export_type', '') or 0)
    q_server_id = request.REQUEST.get('server_id', '')
    page_num = int(_g('page_num', '') or 0) or 1  # 页码
    file_type = request.REQUEST.get('file_type', '')
    page_size = 10000
    request.REQUEST.setlist('page_size', [page_size])
    export_fields = _gl('export_fields')

    fields = query_compiler.query.selects
    #  导出字段index
    export_indexs = [fields.index(i) for i in export_fields]

    is_summary = _g('is_summary', '')  # 汇总

    query_exprot = QueryExprot(export_key, export_type)
    if is_finish:  # 完成,文件合并
        merge_file_name = query_exprot.merge_files(fields, export_indexs)
        _r = {
            "url": '/static/export/query/%s' % merge_file_name,
            "export_key": query_exprot.export_key,
            'export_type': query_exprot.export_type
        }
    elif is_summary:  # 文件汇总
        merges = [int(x) for x in request.REQUEST.getlist('merges')]
        done_summary_file_name = query_exprot.summary_file(merges)
        new_url = '/static/export/query/%s' % done_summary_file_name
        return HttpResponseRedirect(new_url)
    else:
        total_record, list_data = query_do(request,
                                           query_compiler,
                                           built_in=True)
        export_file_name = '%s_%s' % (query_compiler.query.name, q_server_id)
        _r = query_exprot.gene_file(export_file_name, list_data, fields,
                                    export_indexs, page_num, page_size,
                                    total_record)
    _r["code"] = 0
    _r["server_id"] = q_server_id
    return HttpResponse(json_dumps(_r))
Ejemplo n.º 7
0
 async def send_json(self, event_data: EventDataSer, close=False):
     event_data.o.server_time = int(time.time() * 1000)
     event_data.o.channel_name = self.channel_name
     await super().send(text_data=json_dumps(event_data.data), close=close)
Ejemplo n.º 8
0
    def json_config(self):

        return json_dumps(self.config)