Beispiel #1
0
def create(request):
    """
    Form to create a question.
    If it is assigned to all, creates the proper PendingQuestion entries.
    """
    if request.method == 'POST':
        # Create a form instance and populate it with data from the request (binding):
        form = QuestionForm(request.POST)
        if form.is_valid():
            save_question(form, request.user.doctor)
            request.user.doctor.assigned_questions.add(form.instance)
            request.session[
                'message'] = "Question has been successfully created"
            # If assigned to all, assign it to all doctor's patients
            if form.instance.assigned_to_all:
                all_patients = request.user.doctor.patient_set.all()
                for patient in all_patients:
                    pending_question = PendingQuestion(
                        doctor=request.user.doctor,
                        question=form.instance,
                        patient=patient,
                        answering=False)
                    pending_question.save()
            page = request.session.pop('my_questions_page', 1)
            return redirect(f'{reverse("my_questions")}?page={page}')
    else:
        form = QuestionForm()
    context = {
        'form': form,
    }
    return render(request, 'questions_manager/create.html', context)
Beispiel #2
0
def finish(update, context):
    """
    Saves patient in DB, assigns him/her to data_analyst, creates PendingQuestion entries for assigned_to_all questions
    and finally creates the user's PendingQuestionJob
    """
    patient = context.user_data['patient']
    patient.save()
    # Add patient to data analysts and assigned_to_all questions
    try:
        data_analysts = Doctor.objects.filter(is_analyst=True)
        for doctor in data_analysts:
            patient.assigned_doctors.add(doctor)
            patient.save()
            assigned_to_all = doctor.assigned_questions.filter(assigned_to_all=True)
            for question in assigned_to_all:
                pending_question = PendingQuestion(doctor=doctor,
                                                   question=question,
                                                   patient=patient,
                                                   answering=False)
                pending_question.save()
        logger.info("Patient %s assigned to data_analysts", patient.username)
    except Exception:
        logger.exception("Exception while adding patient %s to data_analysts.", patient.username)
    update.message.reply_text(messages[patient.language]['registration_ok'])
    logger.info(f'Creating pending_questions job for user {update.message.from_user.username}')
    PendingQuestionJob(context, patient)
    return ConversationHandler.END
Beispiel #3
0
def assign_question_to_patient(request, question_id, patient_id):
    """
    Assigns a question to a patient bu creating a PendingQuestion
    :param question_id (int)
    :param patient_id (int)
    """
    pending_question = PendingQuestion(doctor=request.user.doctor,
                                       question_id=question_id,
                                       patient_id=patient_id,
                                       answering=False)
    pending_question.save()
    # request.session['message'] = "Question successfully assigned to patient"
    page = request.session.pop('patient_questions_page', 1)
    return redirect(f'{reverse("assign_questions")}/{patient_id}?page={page}')
Beispiel #4
0
def assign(request, question_id):
    """
    Assigns a question to the request's doctor.
    If it is assigned to all, assigns it to all the patients.
    :param question_id (int)
    """
    question = Question.objects.get(id=question_id)
    question.doctor_set.add(request.user.doctor)
    question.save()
    if question.assigned_to_all:
        all_patients = request.user.doctor.patient_set.all()
        for patient in all_patients:
            pending_question = PendingQuestion(doctor=request.user.doctor,
                                               question=question,
                                               patient=patient,
                                               answering=False)
            pending_question.save()
    page = request.session.pop('public_questions_page', 1)
    return redirect(f'{reverse("public_questions")}?page={page}')
Beispiel #5
0
def assign(request):
    """
    Assigns a patient to the request's doctor.
    Required param: username.
    Data clean: remove @ and case insensitive.
    """
    not_found = False
    if request.method == "POST":
        form = AssignPatientForm(request.POST)
        if form.is_valid():
            try:
                username = form.cleaned_data.get('username')
                patient = Patient.objects.get(username__iexact=username)
                if patient in request.user.doctor.patient_set.all():
                    request.session[
                        'message'] = f'Patient {username} is already in your patients'
                else:
                    request.user.doctor.patient_set.add(patient)
                    # Assign assigned_to_all questions
                    questions = request.user.doctor.assigned_questions.filter(
                        assigned_to_all=True)
                    for question in questions:
                        pending = PendingQuestion(doctor=request.user.doctor,
                                                  question=question,
                                                  patient=patient,
                                                  answering=False)
                        pending.save()
                    request.session[
                        'message'] = f'Patient {username} has been successfully added'
                page = request.session.pop('patients_page', 1)
                return redirect(f'{reverse("patients_index")}?page={page}')
            except Patient.DoesNotExist:
                not_found = True
    else:
        form = AssignPatientForm()
    return render(request,
                  'patients_manager/assign.html',
                  context={
                      "form": form,
                      "not_found": not_found
                  })