Пример #1
0
def clinician_pack(request: HttpRequest, clinician_response_id: str,
                   token: str) -> HttpResponse:
    """
    Shows a clinician "pack" as a PDF, including a letter from them to the
    patient, details of the study, forms for the patient to respond, etc.

    Args:
        request: the :class:`django.http.request.HttpRequest`
        clinician_response_id: PK for
            :class:`crate_anon.crateweb.consent.models.ClinicianResponse`
        token: security token (which we'll check against the one we sent to
            the clinician)
    """
    if clinician_response_id == TEST_ID_STR:
        dummies = make_dummy_objects(request)
        clinician_response = dummies.clinician_response
        contact_request = dummies.contact_request
    else:
        clinician_response = get_object_or_404(
            ClinicianResponse,
            pk=clinician_response_id)  # type: ClinicianResponse  # noqa
        contact_request = clinician_response.contact_request
    # Check token authentication
    if token != clinician_response.token:
        return HttpResponseForbidden(
            "Not authorized. The token you passed doesn't match the one you "
            "were sent.")
    # Build and serve
    pdf = contact_request.get_clinician_pack_pdf()
    offered_filename = "clinician_pack_{}.pdf".format(clinician_response_id)
    return serve_buffer(pdf,
                        offered_filename=offered_filename,
                        content_type=ContentType.PDF,
                        as_attachment=False,
                        as_inline=True)
Пример #2
0
def study_details(request: HttpRequest, study_id: str) -> HttpResponseBase:
    """
    View details of a study.

    Args:
        request: the :class:`django.http.request.HttpRequest`
        study_id: PK for :class:`crate_anon.crateweb.consent.models.Study`
    """
    if study_id == TEST_ID_STR:
        study = make_dummy_objects(request).study
    else:
        study = get_object_or_404(Study, pk=study_id)  # type: Study
    if not study.study_details_pdf:
        raise Http404("No details")
    # noinspection PyUnresolvedReferences
    return serve_file(study.study_details_pdf.path,
                      content_type=ContentType.PDF,
                      as_inline=True)
Пример #3
0
def get_consent_mode(request: HttpRequest,
                     consent_mode_id: str) -> ConsentMode:
    """
    Return the specified consent mode.

    Args:
        request: the :class:`django.http.request.HttpRequest`
        consent_mode_id: PK

    Returns:
        :class:`crate_anon.crateweb.consent.models.ConsentMode`

    Raises:
        :exc:`django.http.Http404` if not found
    """
    if consent_mode_id == TEST_ID_STR:
        return make_dummy_objects(request).consent_mode
    return get_object_or_404(ConsentMode,
                             id=consent_mode_id)  # type: ConsentMode
Пример #4
0
def get_patient_lookup(request: HttpRequest,
                       patient_lookup_id: str) -> PatientLookup:
    """
    Return the specified patient lookup.

    Args:
        request: the :class:`django.http.request.HttpRequest`
        patient_lookup_id: PK

    Returns:
        :class:`crate_anon.crateweb.consent.models.PatientLookup`

    Raises:
        :exc:`django.http.Http404` if not found
    """
    if patient_lookup_id == TEST_ID_STR:
        return make_dummy_objects(request).patient_lookup
    return get_object_or_404(PatientLookup,
                             id=patient_lookup_id)  # type: PatientLookup
Пример #5
0
def clinician_response_view(request: HttpRequest,
                            clinician_response_id: str) -> HttpResponse:
    """
    Shows the response choices to the clinician. They'll get here by clicking
    on a link in an e-mail.

    **REC DOCUMENTS 09, 11, 13 (B): Web form for clinicians to respond with.**

    Args:
        request: the :class:`django.http.request.HttpRequest`
        clinician_response_id: PK for
            :class:`crate_anon.crateweb.consent.models.ClinicianResponse`
    """
    if clinician_response_id == TEST_ID_STR:
        dummies = make_dummy_objects(request)
        clinician_response = dummies.clinician_response
        contact_request = dummies.contact_request
        study = dummies.study
        patient_lookup = dummies.patient_lookup
        consent_mode = dummies.consent_mode
    else:
        clinician_response = get_object_or_404(
            ClinicianResponse,
            pk=clinician_response_id)  # type: ClinicianResponse  # noqa
        contact_request = clinician_response.contact_request
        study = contact_request.study
        patient_lookup = contact_request.patient_lookup
        consent_mode = contact_request.consent_mode

    # Build form.
    # - We have an existing clinician_response and wish to modify it
    #   (potentially).
    # - If the clinician is responding to an e-mail, they will be passing
    #   a couple of parameters (including the token) via GET query parameters.
    #   If they're clicking "Submit", they'll be using POST.
    if request.method == 'GET':
        from_email = True
        clinician_response.response_route = ClinicianResponse.ROUTE_EMAIL
        data = request.GET
    else:
        from_email = False
        clinician_response.response_route = ClinicianResponse.ROUTE_WEB
        data = request.POST
    form = ClinicianResponseForm(instance=clinician_response, data=data)
    # log.debug("Form data: {}".format(form.data))

    # Token valid? Check raw data. Say goodbye otherwise.
    # - The raw data in the form is not influenced by the form's instance.
    if form.data['token'] != clinician_response.token:
        # log.critical("Token from user: {!r}".format(form.data['token']))
        # log.critical("Original token: {!r}".format(clinician_response.token))
        return HttpResponseForbidden(
            "Not authorized. The token you passed doesn't match the one you "
            "were sent.")

    # Already responded?
    if clinician_response.responded:
        passed_to_pt = (
            clinician_response.response == ClinicianResponse.RESPONSE_A)
        return render(
            request, 'clinician_already_responded.html', {
                'clinician_response': clinician_response,
                'consent_mode': consent_mode,
                'contact_request': contact_request,
                'Leaflet': Leaflet,
                'passed_to_pt': passed_to_pt,
                'patient_lookup': patient_lookup,
                'settings': settings,
                'study': study,
            })

    # Is the clinician saying yes or no (direct from e-mail)?
    if (from_email and form.data['email_choice']
            in (ClinicianResponse.EMAIL_CHOICE_Y,
                ClinicianResponse.EMAIL_CHOICE_N)):
        # We can't use form.save() as the data may not validate.
        # It won't validate because the response/clinician name is blank.
        # We can't write to the form directly. So...
        clinician_response.email_choice = form.data['email_choice']
        if clinician_response.email_choice == ClinicianResponse.EMAIL_CHOICE_Y:
            # Ask RDBM to do the work
            clinician_response.response = ClinicianResponse.RESPONSE_R
        else:
            # Veto on clinical grounds
            clinician_response.response = ClinicianResponse.RESPONSE_B
        return finalize_clinician_response_in_background(
            request, clinician_response)

    # Has the clinician made a decision via the web form?
    if form.is_valid():
        clinician_response = form.save(commit=False)  # return unsaved instance
        return finalize_clinician_response_in_background(
            request, clinician_response)

    # If we get here, we need to offer the form up for editing,
    # and mark it as a web response.
    clinician_involvement_requested = (
        contact_request.clinician_involvement ==
        ContactRequest.CLINICIAN_INVOLVEMENT_REQUESTED)
    clinician_involvement_required_yellow = (
        contact_request.clinician_involvement ==
        ContactRequest.CLINICIAN_INVOLVEMENT_REQUIRED_YELLOW)
    clinician_involvement_required_unknown = (
        contact_request.clinician_involvement ==
        ContactRequest.CLINICIAN_INVOLVEMENT_REQUIRED_UNKNOWN)
    extra_form = contact_request.is_extra_form()
    return render(
        request,
        'clinician_response.html',
        {
            'clinician_response':
            clinician_response,
            'ClinicianResponse':
            ClinicianResponse,
            'consent_mode':
            consent_mode,
            'contact_request':
            contact_request,
            'Leaflet':
            Leaflet,
            'patient_lookup':
            patient_lookup,
            'settings':
            settings,
            'study':
            study,
            'form':
            form,
            'clinician_involvement_requested':
            clinician_involvement_requested,
            'clinician_involvement_required_yellow':
            clinician_involvement_required_yellow,
            'clinician_involvement_required_unknown':
            clinician_involvement_required_unknown,
            # 'option_c_available': clinician_involvement_requested,
            'option_c_available':
            True,
            'option_r_available':
            not extra_form,
            'extra_form':
            extra_form,
            'unknown_consent_mode':
            contact_request.is_consent_mode_unknown(),
            'permitted_to_contact_discharged_patients_for_n_days':
            settings.PERMITTED_TO_CONTACT_DISCHARGED_PATIENTS_FOR_N_DAYS,
            'permitted_to_contact_discharged_patients_for_n_years':
            days_to_years(
                settings.PERMITTED_TO_CONTACT_DISCHARGED_PATIENTS_FOR_N_DAYS),
        })
Пример #6
0
def get_consent_mode(request: HttpRequest,
                     consent_mode_id: str) -> ConsentMode:
    if consent_mode_id == TEST_ID_STR:
        return make_dummy_objects(request).consent_mode
    return get_object_or_404(ConsentMode,
                             id=consent_mode_id)  # type: ConsentMode
Пример #7
0
def get_patient_lookup(request: HttpRequest,
                       patient_lookup_id: str) -> PatientLookup:
    if patient_lookup_id == TEST_ID_STR:
        return make_dummy_objects(request).patient_lookup
    return get_object_or_404(PatientLookup,
                             id=patient_lookup_id)  # type: PatientLookup
Пример #8
0
def get_contact_request(request: HttpRequest,
                        contact_request_id: str) -> ContactRequest:
    if contact_request_id == TEST_ID_STR:
        return make_dummy_objects(request).contact_request
    return get_object_or_404(ContactRequest,
                             id=contact_request_id)  # type: ContactRequest