Esempio n. 1
0
    def monthly_list(self, request, pk, *args, **kwargs):
        """
        Return the list of rotations for a given month and department
        """
        batch = get_object_or_404(Batch, id=pk)

        if len(request.query_params.keys()) == 0:
            raise ParseError(detail="No query parameters were specified.")  # FIXME: Is this the most accurate error?

        department = request.query_params.get('department')
        month = request.query_params.get('month')

        if department is None or month is None:
            raise ParseError(detail="Both `department` and `month` query parameters should be specified.")

        month = Month.from_int(int(month))
        department = Department.objects.get(id=department)
        rotations = Rotation.objects.filter(
            internship__intern__batch=batch,
            department=department,
            month=month,
        ).prefetch_related(
            'internship__intern__profile',
        ).order_by(
            Lower('internship__intern__profile__en_first_name'),
            Lower('internship__intern__profile__en_father_name'),
            Lower('internship__intern__profile__en_grandfather_name'),
            Lower('internship__intern__profile__en_last_name'),
        )

        if request.query_params.get('excel'):
            return excel_file_as_http_response(batch, department, month, rotations)

        serialized = FullRotationSerializer(rotations, many=True)
        return Response(serialized.data)
Esempio n. 2
0
    def request_freeze_cancel(self, request, month=None):
        intern = request.user
        month = Month.from_int(int(month))
        current_freeze = intern.freezes.current_for_month(month)

        if not current_freeze:
            raise ObjectDoesNotExist("This month has no freeze to cancel.")

        if intern.freeze_cancel_requests.current_for_month(month):
            raise PermissionDenied("There is a pending freeze cancel request for this month already.")

        # TODO: Check that there are no rotations, leaves, freezes, or related requests in the month to be disabled

        cancel_request = FreezeCancelRequest.objects.create(
            intern=intern,
            month=month,
        )

        # --notifications--

        # Subscribe user to receive update notifications on the request
        subscribe(request.user.settings_set.first(), "freeze_cancel_request_approved", object_id=cancel_request.id)
        subscribe(request.user.settings_set.first(), "freeze_cancel_request_declined", object_id=cancel_request.id)

        # Notify medical internship unit of the request
        notify(
            "A freeze cancellation request has been submitted by %s" % (request.user.profile.get_en_full_name()),
            "freeze_cancel_request_submitted",
            url="/planner/%d/" % cancel_request.intern.profile.intern.internship.id,
            )

        messages.success(request._request, "Your freeze cancellation request has been submitted successfully.")

        return Response(status=HTTP_201_CREATED)
Esempio n. 3
0
    def request_freeze(self, request, month=None):
        if request.method == "POST":
            month = Month.from_int(int(month))
            intern = self.request.user

            # TODO: Check that month is not frozen or disabled or there is a freeze request
            # TODO: Check that month has not current rotation or rotation request

            form = FreezeRequestForm(data=request.data, instance=FreezeRequest(intern=intern, month=month))

            if form.is_valid():

                freeze_request = form.save()

                # Subscribe user to receive update notifications on the request
                subscribe(intern.settings_set.first(), "freeze_request_approved", object_id=freeze_request.id)
                subscribe(intern.settings_set.first(), "freeze_request_declined", object_id=freeze_request.id)

                # Notify medical internship unit of the request
                notify(
                    "A new freeze request has been submitted by %s" % (request.user.profile.get_en_full_name()),
                    "freeze_request_submitted",
                    url="/planner/%d/" % freeze_request.intern.profile.intern.internship.id,
                )

                # Display success message to user
                messages.success(request._request, "Your freeze request has been submitted successfully.")

            response_data = {'errors': form.errors}
            return Response(response_data)
        return FreezeRequestFormView.as_view()(request._request, month)
Esempio n. 4
0
    def respond(self, request, department_id, month_id, list_type, *args, **kwargs):
        department = get_object_or_404(Department, id=department_id)
        month = Month.from_int(int(month_id))

        serialized = AcceptanceListSerializer(
            data=request.data,
            instance=AcceptanceList(department, month, list_type)
        )
        serialized.is_valid(raise_exception=True)
        acceptance_list = serialized.save()

        acceptance_list.respond_all()

        return Response(status=HTTP_200_OK)
Esempio n. 5
0
    def with_specialty_details(self, request, month_id, specialty_id):
        month = Month.from_int(int(month_id))
        specialty = get_object_or_404(Specialty, id=specialty_id)
        hospitals = self.get_queryset().prefetch_related('departments')
        for hospital in hospitals:
            hospital.specialty_departments = \
                filter(lambda dep: dep.specialty == specialty, hospital.departments.all())

            for dep in hospital.specialty_departments:
                dep.acceptance_setting = AcceptanceSetting(
                    dep,
                    month,
                )

        serialized = ExtendedHospitalSerializer(hospitals, many=True)
        return Response(serialized.data)
Esempio n. 6
0
    def cancel_rotation(self, request, month=None):
        internship = request.user.profile.intern.internship
        month = Month.from_int(int(month))
        current_rotation = internship.rotations.current_for_month(month)

        if not current_rotation:
            raise ObjectDoesNotExist("This month has no rotation to cancel.")

        if internship.rotation_requests.current_for_month(month):
            raise PermissionDenied("There is a pending rotation request for this month already.")

        requested_department = current_rotation.rotation_request.requested_department
        requested_department.id = None
        requested_department.save()

        rr = internship.rotation_requests.create(
            month=month,
            specialty=requested_department.department.specialty,
            requested_department=requested_department,
            is_delete=True,
        )

        # --notifications--

        # Subscribe user to receive update notifications on the request
        subscribe(request.user.settings_set.first(), "rotation_request_approved", object_id=rr.id)
        subscribe(request.user.settings_set.first(), "rotation_request_declined", object_id=rr.id)

        # Notify medical internship unit of the request
        notify(
            "A cancellation request has been submitted by %s" % (request.user.profile.get_en_full_name()),
            "rotation_request_submitted",
            url="/planner/%d/" % rr.internship.id,
            )  # FIXME: avoid sending a lot of simultaneous notifications

        messages.success(request._request, "Your cancellation request has been submitted successfully.")

        return Response(status=HTTP_201_CREATED)
Esempio n. 7
0
 def to_internal_value(self, data):
     return Month.from_int(int(data))
Esempio n. 8
0
 def get_object(self):
     queryset = self.get_queryset()
     month = Month.from_int(int(self.kwargs[self.lookup_field]))
     return filter(lambda m: m.month == month, queryset)[0]
Esempio n. 9
0
 def get_object(self):
     department = get_object_or_404(Department, id=self.kwargs['department_id'])
     month = Month.from_int(int(self.kwargs['month_id']))
     return AcceptanceSetting(department, month)
Esempio n. 10
0
    def retrieve_list(self, request, department_id, month_id, list_type, *args, **kwargs):
        department = get_object_or_404(Department, id=department_id)
        month = Month.from_int(int(month_id))

        acceptance_list = AcceptanceList(department, month, list_type)
        return Response(AcceptanceListSerializer(acceptance_list).data)
Esempio n. 11
0
 def get_queryset(self):
     department = Department.objects.get(id=self.kwargs['department_id'])
     month = Month.from_int(int(self.kwargs['month_id']))
     return RotationRequest.objects.unreviewed().filter(month=month, requested_department__department=department)