Exemplo n.º 1
0
class JobPostViewSet(viewsets.ModelViewSet):
    """
        Display all not expired jobposts and filter them by title and expired
        Excludes expired jobposts by default: to include expired in search results, add '&expired=true'
    """

    serializer_class = JobPostSerializer
    permission_classes = [BasicViewPermission]
    pagination_class = BasePagination
    queryset = JobPost.objects.filter(
        deadline__gte=yesterday()).order_by("deadline")
    filter_backends = [DjangoFilterBackend, filters.SearchFilter]
    filterset_class = JobPostFilter
    search_fields = ["title", "company"]

    def get_queryset(self):
        if self.kwargs or "expired" in self.request.query_params:
            return JobPost.objects.all().order_by("deadline")
        return JobPost.objects.filter(
            deadline__gte=yesterday()).order_by("deadline")

    def destroy(self, request, *args, **kwargs):
        """ Delete the jobpost """
        super().destroy(request, *args, **kwargs)
        return Response({"detail": "Jobbannonsen ble slettet"},
                        status=status.HTTP_200_OK)
Exemplo n.º 2
0
    def get_queryset(self):
        """
            Return all non-expired events by default.
            Filter expired events based on url query parameter.
        """

        if self.kwargs or "expired" in self.request.query_params:
            queryset = Event.objects.all()
        else:
            queryset = Event.objects.filter(start_date__gte=yesterday())

        return queryset.prefetch_related("forms").order_by("start_date")
Exemplo n.º 3
0
 def expired(self):
     return self.deadline <= yesterday()
Exemplo n.º 4
0
 def filter_expired(self, queryset, name, value):
     if value:
         return queryset.filter(
             deadline__lt=yesterday()).order_by("-deadline")
     return queryset.filter(deadline__gte=yesterday()).order_by("deadline")
Exemplo n.º 5
0
class EventViewSet(viewsets.ModelViewSet):
    serializer_class = EventSerializer
    permission_classes = [BasicViewPermission]
    queryset = Event.objects.filter(
        start_date__gte=yesterday()).order_by("start_date")
    pagination_class = BasePagination

    filter_backends = [DjangoFilterBackend, filters.SearchFilter]
    filterset_class = EventFilter
    search_fields = ["title"]

    def get_queryset(self):
        """
            Return all non-expired events by default.
            Filter expired events based on url query parameter.
        """

        if self.kwargs or "expired" in self.request.query_params:
            queryset = Event.objects.all()
        else:
            queryset = Event.objects.filter(start_date__gte=yesterday())

        return queryset.prefetch_related("forms").order_by("start_date")

    def get_serializer_class(self):
        if hasattr(self, "action") and self.action == "list":
            return EventListSerializer
        return super().get_serializer_class()

    def retrieve(self, request, pk):
        """Return detailed information about the event with the specified pk."""
        try:
            event = self.get_object()
            if is_admin_user(request):
                serializer = EventAdminSerializer(event,
                                                  context={"request": request},
                                                  many=False)
            else:
                serializer = EventSerializer(event,
                                             context={"request": request},
                                             many=False)
            return Response(serializer.data, status=status.HTTP_200_OK)
        except Event.DoesNotExist as event_not_exist:
            capture_exception(event_not_exist)
            return Response(
                {"detail": "Fant ikke arrangementet"},
                status=status.HTTP_404_NOT_FOUND,
            )

    def update(self, request, pk):
        """Update the event with the specified pk."""
        try:
            event = self.get_object()
            self.check_object_permissions(self.request, event)
            serializer = EventCreateAndUpdateSerializer(
                event,
                data=request.data,
                partial=True,
                context={"request": request})

            if serializer.is_valid():
                event = serializer.save()
                serializer = EventSerializer(event)
                return Response(serializer.data, status=status.HTTP_200_OK)
            else:
                return Response(
                    {
                        "detail":
                        "Kunne ikke utføre oppdatering av arrangementet"
                    },
                    status=status.HTTP_400_BAD_REQUEST,
                )

        except Event.DoesNotExist as event_not_exist:
            capture_exception(event_not_exist)
            return Response({"detail": "Fant ikke arrangementet"},
                            status=status.HTTP_404_NOT_FOUND)

    def create(self, request, *args, **kwargs):
        serializer = EventCreateAndUpdateSerializer(
            data=request.data, context={"request": request})

        if serializer.is_valid():
            event = serializer.save()
            serializer = EventSerializer(event)
            return Response(serializer.data, status=status.HTTP_201_CREATED)

        return Response({"detail": serializer.errors},
                        status=status.HTTP_400_BAD_REQUEST)

    def destroy(self, request, *args, **kwargs):
        super().destroy(request, *args, **kwargs)
        return Response({"detail": ("Arrangementet ble slettet")},
                        status=status.HTTP_200_OK)
Exemplo n.º 6
0
 def get_queryset(self):
     if self.kwargs or "expired" in self.request.query_params:
         return JobPost.objects.all().order_by("deadline")
     return JobPost.objects.filter(
         deadline__gte=yesterday()).order_by("deadline")
Exemplo n.º 7
0
 def expired(self):
     return self.end_date <= yesterday()
Exemplo n.º 8
0
 def filter_expired(self, queryset, name, value):
     if value:
         return queryset.filter(
             start_date__lt=yesterday()).order_by("-start_date")
     return queryset.filter(
         start_date__gte=yesterday()).order_by("start_date")