89 lines
3.6 KiB
Python
89 lines
3.6 KiB
Python
from django.shortcuts import render
|
|
from .serializers import *
|
|
from .models import *
|
|
from rest_framework.views import APIView
|
|
from rest_framework.generics import ListAPIView
|
|
from rest_framework.response import Response
|
|
import json
|
|
from django.db.models.functions import TruncYear
|
|
from django.db.models import Q
|
|
from drf_yasg.utils import swagger_auto_schema
|
|
|
|
|
|
|
|
|
|
class ExclusionDatesAPIView(APIView):
|
|
def get(self, request):
|
|
year = self.request.query_params.get('year', datetime.now().year)
|
|
exclude = ExclusionDates.objects.filter(exclusiondate__year=year)
|
|
months = {}
|
|
for i in range(1,12):
|
|
months[i] = exclude.filter(exclusiondate__month=i).values_list("exclusiondate", flat=True)
|
|
return Response({year:months})
|
|
|
|
|
|
class MyCalendars(APIView):
|
|
|
|
def get(self, request):
|
|
calendars = Calendar.objects.filter(Q(get_participants__participant_calendar__id=request.user.user.id) | Q(author__id=request.user.user.id)).distinct()
|
|
return Response(CalendarsSerializer(calendars,many=True).data)
|
|
|
|
@swagger_auto_schema(request_body=MyCalendarPostParameters)
|
|
def post(self,request):
|
|
'''
|
|
Создание/редактирование календаря
|
|
если нет id = пойдет создавать календарь. В противном случае - на обновление
|
|
|
|
|
|
'''
|
|
try:
|
|
ob=MyCalendarPostParameters(data=request.data)
|
|
if ob.is_valid():
|
|
ob = ob.data
|
|
participant = ob['participant'] if 'participant' in ob else False
|
|
del ob['participant']
|
|
if 'id' in ob:
|
|
newcal,created = Calendar.objects.update_or_create(id=ob['id'],defaults=ob)
|
|
|
|
else:
|
|
newcal=Calendar.objects.create(**ob, author=DictEmployee(id=request.user.user.id))
|
|
|
|
if participant != False:
|
|
ptspnt = []
|
|
for x in participant:
|
|
x['participant_calendar']=DictEmployee(id=x['participant_calendar'])
|
|
print(x['participant_calendar'])
|
|
CalendarParticipants.objects.update_or_create(calendar=newcal, participant_calendar=x['participant_calendar'],
|
|
defaults=x)
|
|
ptspnt.append(x['participant_calendar'])
|
|
print(ptspnt)
|
|
CalendarParticipants.objects.exclude(calendar=newcal,participant_calendar__in=ptspnt).delete()
|
|
else:
|
|
return Response({"mess":"Ошибка валидации полей"})
|
|
except Exception as e:
|
|
print(e)
|
|
return Response({"mess":"Ошибка операции"})
|
|
|
|
return Response({"mess": f"Календарь успешно {'обновлен' if 'id' in ob else 'создан'}"})
|
|
|
|
|
|
|
|
@swagger_auto_schema(query_serializer=DeleteCalendarSerializer)
|
|
def delete(self,request):
|
|
try:
|
|
Calendar.objects.get(id=request.query_params.get("delid")).delete()
|
|
except Exception as e:
|
|
print(e)
|
|
return Response({'mess':'Удаление не удалось'})
|
|
return Response({"mess": "Календарь удален"})
|
|
|
|
|
|
class GetEmployees(APIView):
|
|
def get(self, request):
|
|
|
|
queryset = DictDepartment.objects.filter(deleted=0,id__gt=0)
|
|
|
|
return Response(EmployeeSerializer(queryset, many=True).data)
|
|
|
|
|