factory/apps/ofm/views.py

269 lines
8.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.shortcuts import render
from apps.utils.viewsets import CustomModelViewSet, CustomGenericViewSet
from .models import Mroom, MroomBooking, MroomSlot, LendingSeal
# Vehicle, FileRecord, Seal
# BorrowRecord, Publicity, PatentInfo, PaperOfm, Platform, Project, PatentRecord, PaperRecord, ProjectApproval, ProjectInfo)
from .serializers import (MroomSerializer, MroomBookingSerializer, MroomSlotSerializer, LendingSealSerializer)
# ,SealSerializer,
# LendingSealSerializer, FileRecordSerializer, BorrowRecordSerializer, PublicitySerializer,
# PatentInfoSerializer, PaperSerializer, PlatformSerializer, ProjectSerializer, ProjectMemberSerializer, PaperRecordSerializer, ProjectApprovalSerializer, ProjectInfoSerializer)
from rest_framework.decorators import action
from apps.utils.mixins import CustomListModelMixin
from rest_framework.exceptions import ParseError
from apps.ofm.filters import MroomBookingFilterset
class MroomViewSet(CustomModelViewSet):
"""list: 会议室
会议室
"""
queryset = Mroom.objects.all()
serializer_class = MroomSerializer
class MroomBookingViewSet(CustomModelViewSet):
"""list: 会议室预订
会议室预订
"""
queryset = MroomBooking.objects.all()
serializer_class = MroomBookingSerializer
select_related_fields = ["create_by"]
filterset_class = MroomBookingFilterset
def add_info_for_list(self, data):
booking_ids = [d["id"] for d in data]
slots = MroomSlot.objects.filter(booking__in=booking_ids).order_by("booking", "mroom", "mdate", "slot")
booking_info = {}
for slot in slots:
booking_id = slot.booking.id
if booking_id not in booking_info:
booking_info[booking_id] = {
"mdate": slot.mdate.strftime("%Y-%m-%d"), # 格式化日期
"mroom": slot.mroom.id,
"mroom_name": slot.mroom.name, # 会议室名称
"time_ranges": [], # 存储时间段(如 ["8:00-9:00", "10:00-11:30"]
"current_slots": [], # 临时存储连续的slot用于合并
}
# 检查是否连续当前slot是否紧接上一个slot
current_slots = booking_info[booking_id]["current_slots"]
if not current_slots or slot.slot == current_slots[-1] + 1:
current_slots.append(slot.slot)
else:
# 如果不连续先把当前连续的slot转换成时间段
if current_slots:
start_time = self._slot_to_time(current_slots[0])
end_time = self._slot_to_time(current_slots[-1] + 1)
booking_info[booking_id]["time_ranges"].append(f"{start_time}-{end_time}")
current_slots.clear()
current_slots.append(slot.slot)
# 处理最后剩余的连续slot
for info in booking_info.values():
if info["current_slots"]:
start_time = self._slot_to_time(info["current_slots"][0])
end_time = self._slot_to_time(info["current_slots"][-1] + 1)
info["time_ranges"].append(f"{start_time}-{end_time}")
info.pop("current_slots") # 清理临时数据
for item in data:
item.update(booking_info.get(item["id"], {}))
return data
@staticmethod
def _slot_to_time(slot):
"""将slot (0-47) 转换为 HH:MM 格式的时间字符串"""
hours = slot // 2
minutes = (slot % 2) * 30
return f"{hours:02d}:{minutes:02d}"
def perform_update(self, serializer):
ins:MroomBooking = self.get_object()
if ins.create_by != self.request.user:
raise ParseError("只允许创建者修改")
return super().perform_update(serializer)
def perform_destroy(self, instance):
if instance.create_by != self.request.user:
raise ParseError("只允许创建者删除")
return super().perform_destroy(instance)
class MroomSlotViewSet(CustomListModelMixin, CustomGenericViewSet):
"""list:
会议室预订时段
"""
queryset = MroomSlot.objects.all()
serializer_class = MroomSlotSerializer
filterset_fields = ["mroom", "mdate", "booking"]
# class SealModelViewSet(CustomModelViewSet):
# """list: 印章列表
# 印章列表
# """
# queryset = Seal.objects.all()
# serializer_class = SealSerializer
# filterset_fields = ["name"]
# ordering = ["name"]
# class SealManageViewSet(CustomModelViewSet):
# """list: 印章管理
# 印章管理
# """
# queryset = SealManage.objects.all()
# serializer_class = SealManageSerializer
# filterset_fields = ["seal"]
class LendingSealViewSet(CustomModelViewSet):
"""list: 印章外出
印章外出
"""
perms_map = {'get': '*', 'post': 'seal.update',
'put': 'seal.update', 'delete': 'seal.delete'}
queryset = LendingSeal.objects.all()
serializer_class = LendingSealSerializer
filterset_fields = ["seal"]
ordering = ["create_time"]
# class VehicleViewSet(CustomModelViewSet):
# """list: 车辆
# 车辆
# """
# queryset = Vehicle.objects.all()
# serializer_class = VehicleSerializer
# filterset_fields = ["submit_user"]
# ordering = ["create_time"]
# class FilerecordViewSet(CustomModelViewSet):
# """list: 文件
# 文件
# """
# queryset = FileRecord.objects.all()
# serializer_class = FileRecordSerializer
# filterset_fields = ["submit_user", "name", "number"]
# ordering = ["create_time", "number", "name"]
# class FileborrowViewSet(CustomModelViewSet):
# """list: 文件借阅
# 文件借阅
# """
# queryset = BorrowRecord.objects.all()
# serializer_class = BorrowRecordSerializer
# filterset_fields = ["files", "borrow_user"]
# ordering = ["create_time"]
# class PublicityViewSet(CustomModelViewSet):
# """list: 公告
# 公告
# """
# queryset = Publicity.objects.all()
# serializer_class = PublicitySerializer
# filterset_fields = ["title","number"]
# ordering = ["create_time", "number"]
# class PatentInfoViewSet(CustomModelViewSet):
# """list: 专利
# 专利
# """
# queryset = PatentInfo.objects.all()
# serializer_class = PatentInfoSerializer
# filterset_fields = ["name", "author", "type"]
# ordering = ["create_time", "name", "author", "type"]
# class PaperViewSet(CustomModelViewSet):
# """list: 论文申密审批
# 论文申密审批
# """
# queryset = PaperOfm.objects.all()
# serializer_class = PaperSerializer
# filterset_fields = ["name", "author"]
# ordering = ["create_time", "name"]
# class PlatformViewSet(CustomModelViewSet):
# """list: 平台
# 平台
# """
# queryset = Platform.objects.all()
# serializer_class = PlatformSerializer
# filterset_fields = ["name"]
# ordering = ["create_time", "name"]
# class ProjectViewSet(CustomModelViewSet):
# """list: 项目
# 项目
# """
# queryset = Project.objects.all()
# serializer_class = ProjectSerializer
# filterset_fields = ["name"]
# ordering = ["create_time", "name"]
# class PatentRecordViewSet(CustomModelViewSet):
# """list: 专利台账登记
# 专利台账登记
# """
# queryset = PatentRecord.objects.all()
# serializer_class = ProjectMemberSerializer
# filterset_fields = ["patent", "type"]
# ordering = ["create_time", "patent", "type"]
# class PaperRecordViewSet(CustomModelViewSet):
# """list: 论文台账登记
# 论文台账登记
# """
# queryset = PaperRecord.objects.all()
# serializer_class = ProjectMemberSerializer
# filterset_fields = ["index", "title", "paper_code","paper_type", "authors"]
# ordering = ["create_time", "paper", "type"]
# class ProjectApprovalViewSet(CustomModelViewSet):
# """list: 立项审批表
# 立项审批表
# """
# queryset = ProjectApproval.objects.all()
# serializer_class = ProjectApprovalSerializer
# filterset_fields = ["project_start_date"]
# ordering = ["project_start_date"]
# class ProjectInfoViewSet(CustomModelViewSet):
# """list: 项目信息
# 项目信息
# """
# queryset = ProjectInfo.objects.all()
# serializer_class = ProjectInfoSerializer
# filterset_fields = ["serial_number", "name", "platform", "project_source"]
# ordering = ["serial_number", "name"]