Exemplo n.º 1
0
 def validate(self, request, data, action):
     client = data.get("client")
     if action == "create":
         if client.eligibility_queue.filter(status=None).count():
             raise ApplicationValidationError(
                 {"client": ["Client is already in the queue"]}
             )
Exemplo n.º 2
0
def validate_fields_with_rules(user,
                               data,
                               error_message='Not found',
                               **kwargs):
    for field, rule_name in kwargs.items():
        assert rules.rule_exists(rule_name)
        if field in data and not rules.test_rule(rule_name, user, data[field]):
            raise ApplicationValidationError(field, [error_message])
Exemplo n.º 3
0
 def filter_by_type(self, qs, name, value):
     if value == "new":
         return qs.filter(status=None, client__is_new=True)
     elif value == "existing":
         return qs.filter(status=None, client__is_new=False)
     elif value == "historical":
         return qs.exclude(status=None)
     else:
         raise ApplicationValidationError(
             {"type": ["Allowed values are: [new|existing|historical]"]})
     return qs
Exemplo n.º 4
0
 def filter_by_type(self, qs, name, value):
     status = [
         IEPStatus.IN_ORIENTATION,
         IEPStatus.NOT_ELIGIBLE,
         IEPStatus.IN_PLANNING,
         IEPStatus.IN_PROGRESS,
     ]
     if value == "new":
         return qs.filter(status__in=status, client__is_new=True)
     elif value == "existing":
         return qs.filter(status__in=status, client__is_new=False)
     elif value == "historical":
         return qs.filter(
             status__in=[IEPStatus.ENDED, IEPStatus.NOT_ELIGIBLE])
     else:
         raise ApplicationValidationError(
             {"type": ["Allowed values are: [new|existing|historical]"]})
     return qs
Exemplo n.º 5
0
    def perform_create(self, serializer):
        """
        when new IEP is created, eligibility request is created  (if needed)
        or associated (if already exists) with this iep
        """
        client = serializer.validated_data["client"]
        agency = self.request.user.profile.agency
        if agency is None:
            ApplicationValidationError(
                {"user": "******"})
        existing_request = EligibilityQueue.objects.filter(
            client=client, requestor=agency, status=None).first()

        serializer.save(
            created_by=self.request.user,
            status="awaiting_approval",
            eligibility_request=existing_request or
            EligibilityQueue.objects.create(client=client, requestor=agency),
        )
Exemplo n.º 6
0
def validate_fields_with_abilities(ability, data, **kwargs):
    for field, action in kwargs.items():
        if field in data and not ability.can(action, data[field]):
            raise ApplicationValidationError(
                {field: [f"Cannot {action} {field} {data[field]}"]}
            )
Exemplo n.º 7
0
    def update(self, instance, validated_data):
        if "enrollments" in validated_data:
            iep_enrollments_to_remove = set()
            existing_programs = set()
            for iep_enrollment in instance.iep_enrollments.all():
                iep_enrollments_to_remove.add(iep_enrollment.id)
                existing_programs.add(iep_enrollment.enrollment.program)

            enrollment_data = validated_data["enrollments"]

            for row in enrollment_data:
                if "id" not in row:
                    # do not allow to modify client
                    if "client" in row:
                        del row["client"]
                    # create new enrollment
                    enrollment = Enrollment.objects.create(
                        client=instance.client, **row)
                    instance.iep_enrollments.create(enrollment=enrollment)
                else:
                    try:
                        iep_enrollment = instance.iep_enrollments.get(
                            enrollment__id=row["id"])
                        enrollment = iep_enrollment.enrollment
                        del row["id"]
                        iep_enrollments_to_remove.remove(iep_enrollment.id)
                        for attr, value in row.items():
                            setattr(enrollment, attr, value)
                        enrollment.save()
                    except ClientIEPEnrollment.DoesNotExist:
                        raise ApplicationValidationError(
                            {"enrollment.id": "Not found"})

            first_invalid = instance.iep_enrollments.filter(
                id__in=iep_enrollments_to_remove,
                enrollment__status__in=[
                    EnrollmentStatus.ENROLLED.name,
                    EnrollmentStatus.COMPLETED.name,
                    EnrollmentStatus.EXITED.name,
                ],
            ).first()
            if first_invalid:
                raise ApplicationValidationError({
                    f"enrollment.[{first_invalid.id}]":
                    "Cannot be deleted from IEP"
                })

            instance.iep_enrollments.filter(
                id__in=iep_enrollments_to_remove).delete()

        info = model_meta.get_field_info(instance)
        for attr, value in validated_data.items():
            if attr == "job_placement":
                if instance.job_placement:
                    JobPlacementWriter().update(instance.job_placement, value)
                else:
                    instance.job_placement = JobPlacementWriter().create(value)
            elif attr in info.relations and info.relations[attr].to_many:
                pass
            else:
                setattr(instance, attr, value)

        instance.save()

        return instance