event serializer优化

This commit is contained in:
曹前明 2022-09-12 21:23:26 +08:00
parent a815e7524f
commit a0f11e8af7
4 changed files with 23 additions and 5 deletions

View File

@ -86,7 +86,7 @@ class Event(CommonBDModel):
vchannel = models.ForeignKey(TDevice, verbose_name='抓拍设备', on_delete=models.SET_NULL, null=True, blank=True)
location = models.JSONField('事件点位坐标', default=dict, null=False, blank=True)
opl = models.ForeignKey(Opl, verbose_name='关联许可证', on_delete=models.SET_NULL, null=True, blank=True)
obj_cate = models.CharField('发生对象', max_length=20, help_text='people(人员)/...')
obj_cate = models.CharField('发生对象', max_length=20, help_text='people(人员)/opl(作业)/other(其他)...')
employee = models.ForeignKey(Employee, verbose_name='当事人',
on_delete=models.SET_NULL, null=True, blank=True)
msg = models.TextField('事件文本', null=True, blank=True)

View File

@ -8,7 +8,7 @@ from rest_framework import serializers
from apps.system.serializers import UserSimpleSerializer
from apps.utils.constants import EXCLUDE_FIELDS
from rest_framework.exceptions import ParseError
from apps.hrm.serializers import EmployeeSerializer
from apps.hrm.serializers import EmployeeSerializer, EmployeeSimpleSerializer
class EventCateCreateUpdateSerializer(CustomModelSerializer):
@ -75,9 +75,26 @@ class NotifySettingsSerializer(CustomModelSerializer):
class EventSerializer(serializers.ModelSerializer):
area_ = AreaSerializer(source='area', read_only=True)
cates_ = EventCateSimpleSerializer(source='cates', read_only=True, many=True)
employee_ = EmployeeSimpleSerializer(source='employee', read_only=True)
operation_name = serializers.CharField(source='opl.operation.name', read_only=True)
vchannel_ = TDeviceSimpleSerializer(source='vchannel', read_only=True)
handle_user_name = serializers.CharField(source='handle_user.name', read_only=True)
face_img_f = MyFilePathField(source='face_img', read_only=True)
global_img_f = MyFilePathField(source='global_img', read_only=True)
voice_f = MyFilePathField(source='voice', read_only=True)
class Meta:
model = Event
fields = '__all__'
class EventDetailSerializer(serializers.ModelSerializer):
area_ = AreaSerializer(source='area', read_only=True)
cates_ = EventCateSimpleSerializer(source='cates', read_only=True, many=True)
employee_ = EmployeeSerializer(source='employee', read_only=True)
operation_name = serializers.CharField(source='opl.operation.name', read_only=True)
vchannel_ = TDeviceSimpleSerializer(source='vchannel', read_only=True)
handle_user_name = serializers.CharField(source='handle_user.name', read_only=True)
face_img_f = MyFilePathField(source='face_img', read_only=True)

View File

@ -135,7 +135,7 @@ def save_voice_and_speak(event: Event):
for i in sps2:
if i not in sps:
sps.append(i)
myLogger.info('获取到喇叭:' + str(sps))
# myLogger.info('获取到喇叭:' + str(sps))
if sps:
spClient.speak(event.voice, sps, v_num)
except Exception:

View File

@ -19,7 +19,7 @@ from apps.ecm.serializers import (AlgoChannelCreateSerializer,
AlgoChannelSerializer, EventAggSerializer,
EventCateCreateUpdateSerializer,
EventCateListSerializer,
EventHandleSerializer, EventSerializer,
EventHandleSerializer, EventSerializer, EventSimpleSerializer,
NotifySettingsSerializer, RemindSerializer)
from apps.ecm.service import update_remind_read
from apps.utils.viewsets import CustomGenericViewSet, CustomModelViewSet
@ -75,7 +75,8 @@ class EventViewSet(ListModelMixin, RetrieveModelMixin, DestroyModelMixin, Custom
perms_map = {'get': '*'}
queryset = Event.objects.all()
serializer_class = EventSerializer
select_related_fields = ['area', 'employee', 'handle_user', 'vchannel']
retrieve_serializer_class = EventSimpleSerializer
select_related_fields = ['area', 'employee', 'handle_user', 'vchannel', 'opl']
prefetch_related_fields = ['cates']
filterset_class = EventFilterSet