示例#1
0
 def get(self, request, *args, **kwargs):
     try:
         pk = kwargs.get('pk')
         obj = models.UserGroup.objects.filter(id=pk).first()
         ser = GroupSerializers(instance=obj, many=False)
         return MyJsonResponse(data=ser.data, code=1000, msg='ok')
     except Exception as e:
         print(e)
         return MyJsonResponse(data=[], code=1001, msg='获取组信息失败')
示例#2
0
 def get(self, request, *args, **kwargs):
     print(request.query_params)
     keyword = request.query_params.get("keyword", None)
     if keyword is None or keyword == "":
         response = MyJsonResponse(data=[], code=-1, msg="keyword为空")
     else:
         sql = "select id,title,url,`date`,auth,create_time from blog where is_delete =%s and title like %s order by id desc"
         queryset = fetchall_to_dict(sql=sql, params=(0, '%' + keyword + '%'))
         # 自定义封装的通用的分页的方法
         result = get_pagiration_result("page", queryset, request, self)
         return MyJsonResponse(data=result, code=1000, msg="ok")
示例#3
0
 def get(self, request, *args, **kwargs):
     ret = {'code': 1000, 'msg': None}
     # request.user request.auth 是固定写法,是Authtication 的authenticate 的返回值(返回值是一个元祖)
     print('user', request.user)
     print('auth', request.auth)
     try:
         ret['data'] = ORDER_DICT
         ret['msg'] = 'OK'
     except Exception as e:
         return MyJsonResponse(data=[], code=1002, msg='请求异常')
     return MyJsonResponse(data=ORDER_DICT, code=1000, msg='ok')
示例#4
0
 def get(self, request, *args, **kwargs):
     params = request.query_params
     id = params.get("id", None)
     if id is None:
         response = MyJsonResponse(data=[], code=-1, msg="id不能为空")
     else:
         if models.Blog.objects.filter(id=id).count() == 0:
             response = MyJsonResponse(data=[], code=-1, msg="id不存在")
         else:
             models.Blog.objects.filter(id=id).update(is_delete=1)
             response = MyJsonResponse(data=[], code=1000, msg="删除成功")
     return response
示例#5
0
 def get(self, request, *args, **kwargs):
     user_name = request._request.GET.get('user_name')
     password = request._request.GET.get('password')
     count = models.UserInfo.objects.filter(user_name=user_name).count()
     if (count != 0):
         return MyJsonResponse(data=[], code=1001, msg='用户已经存在')
     models.UserInfo.objects.create(
         user_name=user_name,
         password=password_md5(password=password),
         user_type=1,
         group_id=1)
     return MyJsonResponse(data=[], code=1000, msg='ok')
示例#6
0
 def post(self, request, *args, **kwargs):
     time.sleep(1)
     serializer_class = seria.BolgListSerializers
     pg = MyPageNumberPagination()
     keyword = request.data.get("keyword", None)
     if keyword is None or keyword == "":
         response = MyJsonResponse(data=[], code=-1, msg="keyword为空")
     else:
         queryset = models.Blog.objects.filter(title__icontains=keyword).order_by('-id')
         pg_queryset = pg.paginate_queryset(queryset=queryset, request=request, view=self)
         ser = serializer_class(instance=pg_queryset, many=True, )
         response = MyJsonResponse(data=pg.get_paginated_response(ser.data).data, msg="ok", code=1000)
     return response
示例#7
0
 def get(self, request, *args, **kwargs):
     # 使用自定义的分页,此分页集成与rest中的PageNumberPagination
     pg = MyPageNumberPagination()
     sql = "select id,title,url,`date`,auth,create_time from blog where is_delete=%s order by id desc"
     queryset = fetchall_to_dict(sql=sql, params=(0))
     result = get_pagiration_result("page", queryset, request, self)
     return MyJsonResponse(result, code=1000, msg='ok')
示例#8
0
 def get(self, request, *args, **kwargs):
     obj = models.UserInfo.objects.all()
     # 如果需要反向生成URL  必须加上context={'request': request}
     ser = UserInfoSerializers3(instance=obj,
                                many=True,
                                context={'request': request})
     return MyJsonResponse(ser.data)
示例#9
0
 def get(self, request, *args, **kwargs):
     ser = seria.BolgCreateSerializers(data=request._request.GET)
     # request.query_params==request._resuest.GET
     params = request.query_params
     if ser.is_valid():
         title_count = models.Blog.objects.filter(title=params.get('title')).count()
         if title_count > 0:
             response = MyJsonResponse(data=[], code=1001, msg="title重复")
         else:
             models.Blog.objects.create(title=params.get('title'), url=params.get('url'), date=params.get('date'),
                                        auth=params.get('auth'), create_time=datetime.datetime.now())
             response = MyJsonResponse(data={}, code=1000, msg="success")
     else:
         print(ser.errors)
         response = MyJsonResponse(data=ser.errors, code=-1, msg="fail")
     print(response)
     return response
示例#10
0
 def get(self, request, *args, **kwargs):
     # 方式一序列化
     # roles=models.UserRole.objects.all().values('id','title')
     # role_list=list(roles)
     '''方式二序列化'''
     roles = models.UserRole.objects.all()
     ser = RoleSerializers(instance=roles, many=True)
     return MyJsonResponse(data=ser.data, code=1000, msg='ok')
示例#11
0
    def get(self, request, *args, **kwargs):
        ser = seria.BolgCreateSerializers(data=request._request.GET)
        params = request.query_params
        if ser.is_valid():
            try:
                models.Blog.objects.filter(id=params.get("id")).update(title=params.get("title"),
                                                                       date=params.get('date'),
                                                                       url=params.get('url'),
                                                                       auth=params.get('auth'),
                                                                       update_time=datetime.datetime.now())
                response = MyJsonResponse(data=[], code=1000, msg='更新成功')
            except Exception as e:
                response = MyJsonResponse(data=[], code=1001, msg=str(e))

        else:
            print(ser.errors)
            response = MyJsonResponse(data=ser.errors, code=-1, msg="fail")
        print(response)
        return response
示例#12
0
 def get(self, request, *args, **kwargs):
     # data = json.dumps(UserInfoSerializers2(models.UserInfo.objects.all(), many=True).data, ensure_ascii=False)
     # print(JsonResponse(ret).content)
     # return JsonResponse ( ret ) 被序列化后用HttpResponse  自定义的字典形式用JsonResponse
     # return HttpResponse(data)  HttpResponse 需json.dumps(ser.data)
     '''Response无需json.dumps(ser.data)  直接传ser.data即可 可渲染浏览器'''
     # return Response(UserInfoSerializers2(models.UserInfo.objects.all(), many=True).data)
     #  自定义的方式返回
     return MyJsonResponse(data=UserInfoSerializers2(
         models.UserInfo.objects.all(), many=True).data,
                           msg='ok',
                           code=1000)
示例#13
0
 def get(self, request, *args, **kwargs):
     pg = MyLimitOffsetPagination()
     qs = models.UserInfo.objects.all()
     pg_queryset = pg.paginate_queryset(queryset=qs,
                                        request=request,
                                        view=self)
     ser = UserInfoSerializers2(
         instance=pg_queryset,
         many=True,
     )
     # http://127.0.0.1:8000/api/v1.0/page2/?offset=0&limit=4  从第0个开始取,向后取4条
     return MyJsonResponse(data=pg.get_paginated_response(ser.data).data,
                           msg='ok',
                           code=1000)
示例#14
0
 def get(self, request, *args, **kwargs):
     pg = MyCursorPagination()
     qs = models.UserInfo.objects.all()
     pg_queryset = pg.paginate_queryset(queryset=qs,
                                        request=request,
                                        view=self)
     ser = UserInfoSerializers2(
         instance=pg_queryset,
         many=True,
     )
     # http://127.0.0.1:8000/api/v1.0/page3/?cursor=cD03   页码是加密的 (适合上一页 下一个  不能自己选择页码  增强性能)
     return MyJsonResponse(data=pg.get_paginated_response(ser.data).data,
                           msg='ok',
                           code=1000)
示例#15
0
 def post(self, request, *args, **kwargs):
     try:
         user_name = request._request.POST.get("user_name")
         password = request._request.POST.get("password")
         print(user_name)
         print(password)
         obj = models.UserInfo.objects.filter(
             user_name=user_name, password=password_md5(password))
         print(obj)
         if not obj:
             return MyJsonResponse(data=[], code=1001, msg='用户名或密码错误')
         else:
             # 为用户登录创建token
             token = md5(user_name)
             user_id = obj[0].id
             print('id', user_id)
             # 根据user_id 从token表中查询
             count = models.UserToken.objects.filter(
                 user_id=user_id).count()
             if count == 0:
                 # 创建
                 models.UserToken.objects.create(user_id=user_id,
                                                 user_token=token)
             else:
                 # 更新
                 models.UserToken.objects.update(user_id=user_id,
                                                 user_token=token)
     except Exception as e:
         print(e)
         return MyJsonResponse(data=[], code=1002, msg='请求异常')
     return MyJsonResponse(data={
         'token': token,
         "user_id": user_id
     },
                           code=1000,
                           msg='ok')
示例#16
0
 def get(self, request, *args, **kwargs):
     sql = "select user_info.id, user_name,user_type,group_id as `group` from user_info inner join  user_group on user_info.group_id=user_group.id where user_info.group_id=1"
     queryset = fetchall_to_dict(sql=sql)
     print(queryset)
     # 取出所有的group_id
     for row in queryset:
         group_id = row.get("group")
         sql_group = "select id,title from user_group where id=%s"
         query_set_gropu = fetchone_to_dict(sql=sql_group,
                                            params=(group_id))
         row["group"] = query_set_gropu
     page_number_pagiration = MyPageNumberPagination()
     page = page_number_pagiration.paginate_queryset(queryset,
                                                     request=request,
                                                     view=self)
     result = page_number_pagiration.get_paginated_response(page).data
     return MyJsonResponse(data=result, msg='ok', code=1000)
示例#17
0
    def get(self, request, *args, **kwargs):

        # 框架自带的分页
        # http://127.0.0.1:8000/api/v1.0/page1/?page=2这种方式请求
        # pg = PageNumberPagination()
        pg = MyPageNumberPagination()
        # qs = models.UserRole.objects.all()
        qs = models.UserInfo.objects.all()
        pg_queryset = pg.paginate_queryset(queryset=qs,
                                           request=request,
                                           view=self)
        ser = UserInfoSerializers2(
            instance=pg_queryset,
            many=True,
        )
        # http://127.0.0.1:8000/api/v1.0/page1/?page=1&size=4:显示第一页,每页显示4个
        return MyJsonResponse(data=pg.get_paginated_response(ser.data).data,
                              msg='ok',
                              code=1000)
示例#18
0
 def post(self, request, *args, **kwargs):
     return MyJsonResponse(code=1000, msg='UserInfo', data=ORDER_DICT)
示例#19
0
 def list(self, request, *args, **kwargs):
     # 调用父类的list方法,拿到数据
     data = super(ModelViewSet, self).list(request, *args, **kwargs)
     print(data.data)
     return MyJsonResponse(data.data, code=1000, msg='ok')
示例#20
0
 def get(self, request, *args, **kwargs):
     queryset = self.get_queryset()
     pg = self.paginate_queryset(queryset)
     ser = self.get_serializer(instance=pg, many=True)
     return MyJsonResponse(data=ser.data)