def post(self, request): try: return self.validate_and_create_slots(request) except PermissionDenied as e: return Response(parse_error(str(e)), status=status.HTTP_403_FORBIDDEN) except ValidationError as e: return Response(parse_error(str(e)), status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response(parse_error(str(e)))
def destroy(self, request, *args, **kwargs): try: event = self.get_object() if request.user != event.created_by: raise PermissionDenied( "You do not have rights to delete this event. Event - {}". format(event)) return super().destroy(request, *args, **kwargs) except PermissionDenied as e: return Response(parse_error(str(e)), status=status.HTTP_403_FORBIDDEN) except Exception as e: return Response(parse_error(str(e)))
def delete(self, request, *args, **kwargs): try: event_id = self.kwargs.get("event_id") for row in request.data: row['event_id'] = event_id self.validate_user_permissions(request) self.delete_slots_util(request.data) return Response(status=status.HTTP_204_NO_CONTENT) except PermissionDenied as e: return Response(parse_error(str(e)), status=status.HTTP_403_FORBIDDEN) except Exception as e: return Response(parse_error(str(e)))
def create(self, request, *args, **kwargs): try: if hasattr(request.data, '_mutable'): request.data._mutable = True request.data['user_id'] = request.user.id serializer = self.get_serializer(data=request.data) if serializer.is_valid(): self.validate_slot_time(request.data) return super().create(request, *args, **kwargs) except ValidationError as e: return Response(parse_error(str(e)), status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response(parse_error(str(e)))
def get(self, request, *args, **kwargs): try: event_id = self.kwargs.get("event_id") date = self.kwargs.get("date") if event_id and date: return self.get_event_slot_list_for_date(event_id, date) elif event_id: return self.get_event_slot_list(event_id) else: return Response( parse_error("Missing event_id in request", status=status.HTTP_400_BAD_REQUEST)) except Exception as e: return Response(parse_error(str(e)))
def update(self, request, *args, **kwargs): try: event = self.get_object() if request.user != event.created_by: raise PermissionDenied( "You do not have rights to update this event. Event - {}". format(event)) if hasattr(request.data, '_mutable'): request.data._mutable = True request.data['created_by'] = request.user.id return super().update(request, *args, **kwargs) except PermissionDenied as e: return Response(parse_error(str(e)), status=status.HTTP_403_FORBIDDEN) except Exception as e: return Response(parse_error(str(e)))
def destroy(self, request, *args, **kwargs): try: booking = UserBooking.objects.get(user_id=request.user.id, event_id=kwargs.get('pk')) booking.delete() return Response(status=status.HTTP_204_NO_CONTENT) except Exception as e: return Response(parse_error(str(e)))
def update(self, request, *args, **kwargs): try: if hasattr(request.data, '_mutable'): request.data._mutable = True request.data['event_id'] = kwargs.get('pk') request.data['user_id'] = request.user.id booking = UserBooking.objects.get(user_id=request.user.id, event_id=request.data.get('event_id')) serializer = self.get_serializer(booking, data=request.data, partial=False) serializer.is_valid(raise_exception=True) self.validate_slot_time(request.data) self.perform_update(serializer) return Response(serializer.data) except ValidationError as e: return Response(parse_error(str(e)), status=status.HTTP_400_BAD_REQUEST) except Exception as e: return Response(parse_error(str(e)))
def create(self, request, *args, **kwargs): try: if hasattr(request.data, '_mutable'): request.data._mutable = True request.data['created_by'] = request.user.id return super().create(request, *args, **kwargs) except Exception as e: return Response(parse_error(str(e)))