Beispiel #1
0
    def validate(self, data):
        urn = data.pop("urn")
        std_urn = standardise_urn(urn)
        data["urn"] = std_urn

        # Has this URN been used already?
        sent_case = Case.objects.filter(urn=std_urn,
                                        case_number=data["case_number"],
                                        sent=True).exists()

        if sent_case:
            raise exceptions.ValidationError(
                "URN / Case number already exists and has been used")

        return data
Beispiel #2
0
    def validate(self, data):
        urn = data.pop("urn")
        std_urn = standardise_urn(urn)
        data["urn"] = std_urn

        # Has this URN been used already?
        sent_results = Result.objects.filter(urn=urn,
                                             case_number=data["case_number"],
                                             sent=True).exists()

        if sent_results:
            AuditEvent().populate(
                event_type="result_api",
                event_subtype="result_invalid_duplicate_urn_used",
                event_trace="URN: {0}".format(urn),
            )
            raise serializers.ValidationError(
                "URN / Result number already exists and has been used")

        return data
    def validate(self, data):
        urn = data.pop("urn")
        std_urn = standardise_urn(urn)
        data["urn"] = std_urn

        # Has this URN been used already?
        sent_results = Result.objects.filter(
            urn=urn,
            case_number=data["case_number"],
            sent=True).exists()

        if sent_results:
            AuditEvent().populate(
                event_type="result_api",
                event_subtype="result_invalid_duplicate_urn_used",
                event_trace="URN: {0}".format(urn),
            )
            raise serializers.ValidationError(
                "URN / Result number already exists and has been used")

        return data
    def handle(self, *args, **options):
        total_matched, total_missed, matched, missed = 0, 0, 0, 0

        with open(options['csv_file'][0]) as csvfile:
            for row in csvfile.readlines():
                if not row.strip():
                    print "----------------\nMatched {}\nMissed {}\n\n".format(matched, missed)
                    total_matched += matched
                    total_missed += missed

                elif row.startswith("#"):
                    if matched > 0 or missed > 0:
                        matched = 0
                        missed = 0
                    print row
                else:
                    urn = standardise_urn(row)
                    if Case.objects.filter(urn__iexact=urn).exists():
                        matched += 1
                    else:
                        missed += 1
                        print "{} - failed".format(row.strip())

        print "----------------\nTotal:\nMatched {}\nMissed {}".format(total_matched, total_missed)
Beispiel #5
0
    def validate(self, data):
        if "date_of_hearing" not in data:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="invalid_case_missing_dateofhearing",
                event_trace=str(data),
            )
            raise serializers.ValidationError(
                "date_of_hearing is a required field")

        if len(data.get("offences", [])) == 0:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="case_invalid_no_offences",
                event_trace=str(data),
            )
            raise serializers.ValidationError("case has no offences")

        offence_codes = [
            offence["offence_code"][:4] for offence in data["offences"]
        ]

        match = all([
            CaseOffenceFilter.objects.filter(
                filter_match__startswith=offence_code).exists()
            for offence_code in offence_codes
        ])

        if not match:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="case_invalid_not_in_whitelist",
                event_trace=str(data),
            )
            raise serializers.ValidationError(
                ("Case {} contains offence codes [{}] not present "
                 "in the whitelist").format(data.get("urn"), offence_codes))

        init_type = data.get("initiation_type", "")
        if init_type not in self.accepted_initiation_types:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="case_invalid_invalid_initiation_type",
                event_trace=str(data),
            )
            raise serializers.ValidationError(
                "Case contains invalid initiation type {}".format(
                    data.get("initiation_type", "")))

        urn = data.pop("urn")
        std_urn = standardise_urn(urn)
        data["urn"] = std_urn

        # Has this URN been used already?
        sent_case = Case.objects.filter(urn=std_urn,
                                        case_number=data["case_number"],
                                        sent=True).exists()

        if sent_case:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="case_invalid_duplicate_urn_used",
                event_trace=str(data),
            )
            raise serializers.ValidationError(
                "URN / Case number already exists and has been used")

        return data
    def validate(self, data):
        if "date_of_hearing" not in data:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="invalid_case_missing_dateofhearing",
                event_trace=str(data),
            )
            raise serializers.ValidationError(
                "date_of_hearing is a required field")

        if len(data.get("offences", [])) == 0:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="case_invalid_no_offences",
                event_trace=str(data),
            )
            raise serializers.ValidationError("case has no offences")

        offence_codes = [
            offence["offence_code"][:4]
            for offence in data["offences"]]

        match = all([
            CaseOffenceFilter.objects.filter(
                filter_match__startswith=offence_code).exists()
            for offence_code in offence_codes])

        if not match:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="case_invalid_not_in_whitelist",
                event_trace=str(data),
            )
            raise serializers.ValidationError(
                ("Case {} contains offence codes [{}] not present "
                 "in the whitelist").format(
                    data.get("urn"),
                    offence_codes))

        init_type = data.get("initiation_type", "")
        if init_type not in self.accepted_initiation_types:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="case_invalid_invalid_initiation_type",
                event_trace=str(data),
            )
            raise serializers.ValidationError(
                "Case contains invalid initiation type {}".format(
                    data.get("initiation_type", "")))

        urn = data.pop("urn")
        std_urn = standardise_urn(urn)
        data["urn"] = std_urn

        # Has this URN been used already?
        sent_case = Case.objects.filter(
            urn=std_urn,
            case_number=data["case_number"],
            sent=True).exists()

        if sent_case:
            AuditEvent().populate(
                event_type="case_api",
                event_subtype="case_invalid_duplicate_urn_used",
                event_trace=str(data),
            )
            raise serializers.ValidationError(
                "URN / Case number already exists and has been used")

        return data
def process_receipt(subject, body):
    """

    Returns a tuple of status (True/False) and message
    """

    plea_id, count_id, status, urn, doh = \
        extract_data_from_email(subject, body)

    try:
        case_obj = Case.objects.get(id=plea_id)
    except Case.DoesNotExist:
        raise ReceiptProcessingError('Cannot find Case(<{}>)'
                                     .format(plea_id))
    try:
        count_obj = CourtEmailCount.objects.get(id=count_id)
    except CourtEmailCount.DoesNotExist:
        raise ReceiptProcessingError('Cannot find CourtEmailCount(<{}>)'
                                     .format(count_id))

    if status == "Passed":
        if case_obj.has_action("receipt_success"):
            return False, "{} already processed. Skipping.".format(urn)

        if standardise_urn(urn) != case_obj.urn:
            # HMCTS have changed the URN, update our records and log the change

            old_urn, case_obj.urn = case_obj.urn, standardise_urn(urn)

            case_obj.add_action("receipt_success", "\\URN CHANGED! Old Urn: {}".format(old_urn))

            status_text = 'Passed [URN CHANGED! old urn: {}] {}'.format(urn, old_urn)
        else:
            case_obj.add_action("receipt_success", "")
            status_text = 'Passed: {}'.format(urn)

        case_obj.processed = True
        case_obj.save()

        success = True

        # We can't modify the DOH as the hearing time is not provided by
        # hmcts, at current

        #
        # do outbound actions, e.g. send an email to a user.
        #

    else:
        case_obj.add_action("receipt_failure", "")

        success = False

        status_text = 'Failed: {}'.format(urn)

    count_obj.get_status_from_case(case_obj)
    count_obj.save()

    case_obj.save()

    return success, status_text
def process_receipt(subject, body):
    """

    Returns a tuple of status (True/False) and message
    """

    plea_id, count_id, status, urn, doh = \
        extract_data_from_email(subject, body)

    try:
        case_obj = Case.objects.get(id=plea_id)
    except Case.DoesNotExist:
        raise ReceiptProcessingError('Cannot find Case(<{}>)'
                                     .format(plea_id))
    try:
        count_obj = CourtEmailCount.objects.get(id=count_id)
    except CourtEmailCount.DoesNotExist:
        raise ReceiptProcessingError('Cannot find CourtEmailCount(<{}>)'
                                     .format(count_id))

    if status == "Passed":
        if case_obj.has_action("receipt_success"):
            return False, "{} already processed. Skipping.".format(urn)

        if standardise_urn(urn) != case_obj.urn:
            # HMCTS have changed the URN, update our records and log the change

            old_urn, case_obj.urn = case_obj.urn, standardise_urn(urn)

            case_obj.add_action("receipt_success", "\URN CHANGED! Old Urn: {}".format(old_urn))

            status_text = 'Passed [URN CHANGED! old urn: {}] {}'.format(urn, old_urn)
        else:
            case_obj.add_action("receipt_success", "")
            status_text = 'Passed: {}'.format(urn)

        case_obj.processed = True
        case_obj.save()

        success = True

        # We can't modify the DOH as the hearing time is not provided by
        # hmcts, at current

        #
        # do outbound actions, e.g. send an email to a user.
        #

    else:
        case_obj.add_action("receipt_failure", "")

        success = False

        status_text = 'Failed: {}'.format(urn)

    count_obj.get_status_from_case(case_obj)
    count_obj.save()

    case_obj.save()

    return success, status_text