def get(self, request, format="JSON"): user = request.user try: dept_id = user.dept.id dept_arr = [] depts = Department.objects.all() getDept(dept_id, depts, dept_arr).append(dept_id) query_params = list(request.query_params.keys()) kwargs = {} if 'title' in query_params: kwargs['title__contains'] = request.query_params['title'] kwargs['dept_id__in'] = dept_arr print(dept_arr) type_list = Role.objects.filter(**kwargs) page = PageNumberPagination() # 生成分页器对象 page.page_size = 20 # 设置每页的条数 page.page_query_param = 'page' # url中查询第几页的key默认为'page page.page_size_query_param = 'limit' # url中这一页数据的条数的key, 默认为None # page.max_page_size = 5 # 每页的最大数据条数 page_list = page.paginate_queryset(type_list, request, self) # 生成这一页的数据列表 # page_list. # 将这一页的数据列表序列化 # return Response(ret.data) # 返回查到的数据列表 ret = Roleserializer(page_list, many=True) jsondata = {} jsondata['code'] = 20000 jsondata['total'] = page.page.paginator.count jsondata['items'] = ret.data return Response(jsondata) except: return Response({'code': 0, 'msg': '设备列表获取失败'})
def get(self, request, format="JSON"): # user=request.user try: query_params = list(request.query_params.keys()) kwargs = {} devices_list = warnType.objects.filter( **kwargs).order_by('-addtime') page = PageNumberPagination() # 生成分页器对象 page.page_size = 2 # 设置每页的条数 page.page_query_param = 'page' # url中查询第几页的key默认为'page page.page_size_query_param = 'limit' # url中这一页数据的条数的key, 默认为None # page.max_page_size = 5 # 每页的最大数据条数 page_list = page.paginate_queryset(devices_list, request, self) # 生成这一页的数据列表 # page_list. # 将这一页的数据列表序列化 # return Response(ret.data) # 返回查到的数据列表 ret = WarnTypeSerialiser(devices_list, many=True) jsondata = {} jsondata['code'] = 20000 # jsondata['total'] = page.page.paginator.count jsondata['items'] = ret.data return Response(jsondata) except: return Response({'code': 0, 'msg': '告警类型获取失败'})
def get(self, request, format="JSON"): user = request.user try: query_params = list(request.query_params.keys()) kwargs = {} if 'start' in query_params and request.query_params['start'] != '': kwargs['addtime__date__gte'] = request.query_params['start'] if 'end' in query_params and request.query_params['end'] != '': kwargs['addtime__date__lte'] = request.query_params['end'] kwargs['user__id'] = user.id log_list = Logs.objects.filter(**kwargs).order_by('-addtime') page = PageNumberPagination() # 生成分页器对象 page.page_size = 2 # 设置每页的条数 page.page_query_param = 'page' # url中查询第几页的key默认为'page page.page_size_query_param = 'limit' # url中这一页数据的条数的key, 默认为None # page.max_page_size = 5 # 每页的最大数据条数 page_list = page.paginate_queryset(log_list, request, self) # 生成这一页的数据列表 # page_list. # 将这一页的数据列表序列化 # return Response(ret.data) # 返回查到的数据列表 ret = LogsSerialiser(page_list, many=True) jsondata = {} jsondata['code'] = 20000 jsondata['total'] = page.page.paginator.count jsondata['items'] = ret.data return Response(jsondata) except: return Response({'code': 0, 'msg': '日志列表获取失败'})
def restaurants_list(request): paginator = PageNumberPagination() paginator.page_size = 2 paginator.page_query_param = 'page' restaurants = RestaurantProfile.objects.all() context = paginator.paginate_queryset(restaurants, request) serializer = RestaurantSerializer(context, many=True) return paginator.get_paginated_response(serializer.data)
def get_possible_values(self, request, pk=None): queryset = AttributeValue.objects.filter( attribute_id=pk).values('value').distinct() pagination = PageNumberPagination() pagination.page_size = 10 pagination.page_query_param = 'pagenumber' qs = pagination.paginate_queryset(queryset, request) serializer = serializers.AttributeValueFilterPosibleVal(qs, many=True) return pagination.get_paginated_response(serializer.data)
def get(self,request): data=exercisemodel.fillblanks.objects.all() page = PageNumberPagination() # 每页显示条数 page.page_size = 5 page.page_query_param = 'page' page_list = page.paginate_queryset(data, request, view=self) serializer = FillblanksSerializer(instance=page_list,many=True) return page.get_paginated_response(serializer.data)
def get(self, request): state = request.query_params.get('choose') todos = Todo.objects.filter(state=state).order_by('-todo_id') page = PageNumberPagination() page.page_size = 7 page.page_query_param = 'page' page_list = page.paginate_queryset(todos, request, self) serializer = TaskSerializer(instance=page_list, many=True) return page.get_paginated_response(serializer.data)
def get(self, request): result = {} data = Book.objects.all().order_by('id') page = PageNumberPagination() page.page_size = 10 page.page_query_param = 'page' page_list = page.paginate_queryset(data, request) se = BookSerializer(instance=page_list, many=True) result['code'] = 200 result['data'] = se.data result['total'] = data.count() return Response(result)
def get(self, request, format="JSON"): user = request.user try: dept_id = user.dept.id dept_arr = [] depts = Department.objects.all() getDept(dept_id, depts, dept_arr).append(dept_id) query_params = list(request.query_params.keys()) kwargs = {} if 'sn' in query_params: kwargs['sn__contains'] = request.query_params['sn'] if 'group' in query_params and request.query_params['group'] != '': kwargs['group__id'] = request.query_params['group'] if 'building' in query_params and request.query_params[ 'building'] != '': kwargs['building__id'] = request.query_params['building'] if 'type' in query_params and request.query_params['type'] != '': kwargs['type__id'] = request.query_params['type'] if 'name' in query_params: kwargs['name__contains'] = request.query_params['name'] kwargs['user__dept_id__in'] = dept_arr devices_list = Devices.objects.filter( **kwargs).order_by('-addtime') page = PageNumberPagination() # 生成分页器对象 page.page_size = 2 # 设置每页的条数 page.page_query_param = 'page' # url中查询第几页的key默认为'page page.page_size_query_param = 'limit' # url中这一页数据的条数的key, 默认为None # page.max_page_size = 5 # 每页的最大数据条数 page_list = page.paginate_queryset(devices_list, request, self) # 生成这一页的数据列表 # page_list. # 将这一页的数据列表序列化 # return Response(ret.data) # 返回查到的数据列表 ret = DeviceListSerialiser(page_list, many=True) jsondata = {} jsondata['code'] = 20000 jsondata['total'] = page.page.paginator.count jsondata['items'] = ret.data content = user.username + '进行获取设备列表操作;结果:成功!' Logs.objects.create(content=content, user=user, addtime=datetime.datetime.now(), ip=request.META['REMOTE_ADDR']) return Response(jsondata) except: content = user.username + '进行获取设备列表操作;结果:失败!' Logs.objects.create(content=content, user=user, addtime=datetime.datetime.now(), ip=request.META['REMOTE_ADDR']) return Response({'code': 0, 'msg': '设备列表获取失败'})
def get(self, request, format="JSON"): user = request.user try: query_params = list(request.query_params.keys()) kwargs = {} kwargs['device__user_id'] = user.id if 'sn' in query_params: kwargs['device__sn__contains'] = request.query_params['sn'] if 'start' in query_params and request.query_params['start'] != '': kwargs['addtime__date__gte'] = request.query_params['start'] if 'end' in query_params and request.query_params['end'] != '': kwargs['addtime__date__lte'] = request.query_params['end'] if 'type' in query_params: kwargs['warnType_id'] = request.query_params['type'] if 'name' in query_params: kwargs['device__name__contains'] = request.query_params['name'] devices_list = warndata.objects.filter( **kwargs).order_by('-addtime') page = PageNumberPagination() # 生成分页器对象 page.page_size = 2 # 设置每页的条数 page.page_query_param = 'page' # url中查询第几页的key默认为'page page.page_size_query_param = 'limit' # url中这一页数据的条数的key, 默认为None # page.max_page_size = 5 # 每页的最大数据条数 page_list = page.paginate_queryset(devices_list, request, self) # 生成这一页的数据列表 # page_list. # 将这一页的数据列表序列化 # return Response(ret.data) # 返回查到的数据列表 ret = WarnDataSerializer(page_list, many=True) jsondata = {} jsondata['code'] = 20000 jsondata['total'] = page.page.paginator.count jsondata['items'] = ret.data content = user.username + '进行获取告警列表操作;结果:成功!' Logs.objects.create(content=content, user=user, addtime=datetime.datetime.now(), ip=request.META['REMOTE_ADDR']) return Response(jsondata) except: content = user.username + '进行获取告警列表操作;结果:失败!' Logs.objects.create(content=content, user=user, addtime=datetime.datetime.now(), ip=request.META['REMOTE_ADDR']) return Response({'code': 0, 'msg': '告警列表获取失败'})
def get(self, request): result = {} token = request.GET.get('token', None) if token: user = Token.objects.filter(token_code=token).first() se = TokenSerializer(user) result['data'] = se.data result['code'] = 200 return Response(result) else: data = User.objects.all().order_by('id') page = PageNumberPagination() page.page_size = 10 page.page_query_param = 'page' page_list = page.paginate_queryset(data, request) ser = UserSerializer(instance=page_list, many=True) result['count'] = data.count() result['code'] = 200 result['data'] = ser.data return Response(result)
def get(self, request, format="JSON"): user = request.user try: dept_id = user.dept.id dept_arr = [] depts = Department.objects.all() getDept(dept_id, depts, dept_arr).append(dept_id) query_params = list(request.query_params.keys()) kwargs = {} if 'deviceid' in query_params: kwargs['device__id'] = request.query_params['deviceid'] kwargs['addTime__hour'] = datetime.datetime.now().hour # if 'name' in query_params: # kwargs['name__contains'] = request.query_params['name'] kwargs['device__user__dept_id__in'] = dept_arr data_list = RealTimeData.objects.filter( **kwargs).order_by('-addTime') page = PageNumberPagination() # 生成分页器对象 page.page_size = 10 # 设置每页的条数 page.page_query_param = 'page' # url中查询第几页的key默认为'page page.page_size_query_param = 'limit' # url中这一页数据的条数的key, 默认为None # page.max_page_size = 5 # 每页的最大数据条数 page_list = page.paginate_queryset(data_list, request, self) # 生成这一页的数据列表 # page_list. # 将这一页的数据列表序列化 # return Response(ret.data) # 返回查到的数据列表 # ret = GroupListSerialiser(page_list, many=True) ret = RealTimeListSerialiser(page_list, many=True) jsondata = {} jsondata['code'] = 20000 jsondata['items'] = ret.data jsondata['total'] = page.page.paginator.count return Response(jsondata) except: return Response({'code': 0, 'msg': '设备数据获取失败'})
def get(self, request, format="JSON"): user = request.user try: dept_id = user.dept.id dept_arr = [] depts = Department.objects.all() getDept(dept_id, depts, dept_arr).append(dept_id) query_params = list(request.query_params.keys()) kwargs = {} if 'name' in query_params: kwargs['name__contains'] = request.query_params['name'] kwargs['user__dept_id__in'] = dept_arr buidling_list = Buidling.objects.filter( **kwargs).order_by('-addtime') page = PageNumberPagination() # 生成分页器对象 page.page_size = 20 # 设置每页的条数 page.page_query_param = 'page' # url中查询第几页的key默认为'page page.page_size_query_param = 'limit' # url中这一页数据的条数的key, 默认为None # page.max_page_size = 5 # 每页的最大数据条数 page_list = page.paginate_queryset(buidling_list, request, self) # 生成这一页的数据列表 # page_list. # 将这一页的数据列表序列化 # return Response(ret.data) # 返回查到的数据列表 ret = BuidlingListSerialiser(page_list, many=True) jsondata = {} for building in ret.data: online = 0 offline = 0 open = 0 close = 0 total = 0 power = ActualData.objects.filter( device__building_id=building['id']).aggregate( power=Sum('tPower')) for group in building['group']: for device in group['device']: total += 1 if device['status'] == '0': offline += 1 else: online += 1 if device['switch'] == '0': close += 1 else: open += 1 building['power'] = power building['online'] = online building['offline'] = offline building['open'] = open building['close'] = close building['device_total'] = total jsondata['code'] = 20000 jsondata['total'] = page.page.paginator.count jsondata['items'] = ret.data content = user.username + '进行获取楼层列表操作;结果:成功!' Logs.objects.create(content=content, user=user, addtime=datetime.datetime.now(), ip=request.META['REMOTE_ADDR']) return Response(jsondata) except: content = user.username + '进行获取楼层列表操作;结果:失败!' Logs.objects.create(content=content, user=user, addtime=datetime.datetime.now(), ip=request.META['REMOTE_ADDR']) return Response({'code': 0, 'msg': '设备列表获取失败'})