def put(self, request, pk, format=None): event = self.get_object(pk) serializer = EventSerializer(event, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def patch(self, request, format=None): if request.user.is_authenticated() and is_manager(request): shift_id = request.POST.get('shift_id') shift = Shift.objects.get(id=shift_id) shift_data = {'manager_approved': True} shift_serializer = ShiftSerializer(shift, data=shift_data, partial=True) event = Event.objects.get(id=shift.event_id) current_emp_id = event.employee_id event_data = {'employee_id': shift.interested_emp_id} event_serializer = EventSerializer(event, data=event_data, partial=True) stat_giver = Stat.objects.get(user_id=current_emp_id) stat_taker = Stat.objects.get(user_id=shift.interested_emp_id) stat_giver_serializer = StatSerializer( stat_giver, data={}, context={'situation': 'giver'}) stat_taker_serializer = StatSerializer( stat_taker, data={}, context={'situation': 'taker'}) if stat_giver_serializer.is_valid( ) and stat_taker_serializer.is_valid(): stat_giver_serializer.save() stat_taker_serializer.save() if shift_serializer.is_valid() and event_serializer.is_valid(): shift_serializer.save() event_serializer.save() return Response(shift_serializer.data) return Response(shift_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def new(request): parser_class = (FileUploadParser, ) if request.method == 'POST': serializer = EventSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return Response(status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): serializer = EventSerializer(data=request.data, context={'request': request}) employee_id = request.data.get('employee_id') employee_profile = Profile.objects.get(user_id=employee_id) profile_serializer = ProfileSerializer(employee_profile) if serializer.is_valid(): serializer.save() add_event_data = serializer.data add_event_data.update({'employee_profile': profile_serializer.data}) return Response(add_event_data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def edit(request, event_id): parser_class = (FileUploadParser, ) try: event = Event.objects.get(id=event_id) except Event.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'PATCH': serializer = EventSerializer(event, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) else: return Response(status=status.HTTP_400_BAD_REQUEST)
def post(self, request, format=None): serializer = EventSerializer(data=request.data, context={'request': request}) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)