You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
2.0 KiB
Python

2 years ago
import logging
from django.db.models import Count
# Create your views here.
from rest_framework import viewsets
from rest_framework.response import Response
from app.models import XZNSH
2 years ago
from app.serializers import SerialMyModel, SerialFilter
from app.pagination import MyPageNumberPagination
from user.models import UserProfile
from django.contrib.auth import authenticate, login, logout
from .utils import generate_token, decode_token_exp_time
logger = logging.getLogger('mylogger')
class XZNSHModelViewSet(viewsets.ModelViewSet):
2 years ago
# 查询类
queryset = XZNSH.objects.all().order_by("-uid") # 按照uid倒序
2 years ago
# 序列化类
serializer_class = SerialMyModel
# 分页类
pagination_class = MyPageNumberPagination
# 条件筛选
filterset_class = SerialFilter
def get_events(self, request, *args, **kwargs):
2 years ago
res = self.queryset.values("event_type").filter(event_type__isnull=False).annotate(count=Count("event_type")).order_by('-count')
response = self.filter_filed("event_type", res)
return Response(response)
def get_scenes(self, request, *args, **kwargs):
res = self.queryset.values("scene").filter(scene__isnull=False).annotate(count=Count("scene")).order_by('-count')
response = self.filter_filed("scene", res)
return Response(response)
def get_violation_reason(self, request, *args, **kwargs):
res = self.queryset.values("violation_reason").filter(violation_reason__isnull=False).annotate(count=Count("violation_reason")).order_by('-count')
response = self.filter_filed("violation_reason", res)
return Response(response)
@staticmethod
def filter_filed(filed_name, query_set):
result = list(query_set)
2 years ago
data = dict()
for index, item in enumerate(result, 1):
2 years ago
data[index] = item.get(filed_name)
2 years ago
response = {
'success': True,
'msg': '查询成功',
'data': data
}
2 years ago
return response
2 years ago