def display_new_case(request, patient_id):
    ''' Display the new case page and process submitted new-case
        forms. patient_id specifies the default patient the case is for. Set
        to "X" if no patient is selected. '''

    user = request.user
    worker = request.user.worker

    if request.method == 'POST':

        form = NewCaseForm(request.POST, request.FILES)
        if form.is_valid():

            patient_id = form.cleaned_data['patient']
            comments = form.cleaned_data['comments']
            priority = form.cleaned_data['priority']
            scan_image = form.cleaned_data['scan_image']

            try:
                matching_patients = Patient.objects.filter(id=patient_id)
                if len(matching_patients) == 0:
                    return HttpResponseServerError()

                patient = matching_patients[0]

                comment = Comment(
                    author=user,
                    text=comments,
                    time_posted=timezone.now())
                comment.save()

                comment_group = CommentGroup()
                comment_group.save()
                comment_group.comments.add(comment)

                case = Case(
                    patient=patient,
                    submitter_comments=comment_group,
                    priority=priority,
                    status=1,
                    submitter=worker,
                    date_opened=timezone.now())
                case.save()

                if scan_image != None:
                    scan = Scan(patient=patient)
                    scan.save()

                    scan.file = scan_image
                    scan.save()

                    comment.scans.add(scan)
                    case.scans.add(scan)

            except IntegrityError, e:
                print str(e)
                print "hard fail"
                return HttpResponseServerError()

            return HttpResponseRedirect("/case/" + str(case.id))
예제 #2
0
def create_new_case_m(request):

    data = is_worker(request)
    if not data:
        json_response = json.dumps({"success": "false",
                                    "type": "notWorker"})
        return HttpResponse(json_response, mimetype='application/json')

    form = NewCaseForm(data)
    worker = data['worker']

    if form.is_valid():
        patient_id = form.cleaned_data['patient']
        comments = form.cleaned_data['comments']
        priority = form.cleaned_data['priority']

        try:
            patient = Patient.objects.filter(id=patient_id)[0]

            comment = Comment(
                author=worker.user,
                text=comments,
                time_posted=timezone.now())
            comment.save()

            comment_group = CommentGroup()
            comment_group.save()
            comment_group.comments.add(comment)

            case = Case(
                patient=patient,
                submitter_comments=comment_group,
                priority=priority,
                status=1,
                submitter=worker,
                date_opened=timezone.now())
            case.save()
        except IntegrityError:
            json_response = json.dumps({"success": "false",
                                        "type": "IntegrityError"})
            return HttpResponse(json_response, mimetype='application/json')

        json_response = json.dumps({"success": "true",
                                    "type": "newCase",
                                    "case_id": str(case.id)})
        return HttpResponse(json_response, mimetype='application/json')
    else:
        json_response = json.dumps({"success": "false",
                                    "type": "invalidForm"})
        return HttpResponse(json_response, mimetype='application/json')
예제 #3
0
def populate_default_test_data():
    ''' Populates the database with default test data. '''

    try:
        worker_user = createUser('theworker', '*****@*****.**', 'password')
        worker_user.save()
        worker = Worker(user=worker_user)
        worker.registration_time = timezone.now()
        worker.save()

    except IntegrityError:
        print 'Failed to create a default worker user'
        return HttpResponseServerError()

    try:
        doctor_user = createUser('thedoctor', '*****@*****.**', 'thepassword')
        doctor_user.save()
        doctor = Doctor(user=doctor_user)
        doctor.user_id = doctor_user.id
        doctor.registration_time = timezone.now()
        doctor.save()
    except IntegrityError:
        print 'Failed to create a default doctor'
        return HttpResponseServerError()

    try:
        doctor2_user = createUser('doctor2', '*****@*****.**', 'd2password')
        doctor2_user.save()
        doctor2 = Doctor(user=doctor2_user)
        doctor2.user_id = doctor2_user.id
        doctor2.registration_time = timezone.now()
        doctor2.save()
    except IntegrityError:
        print 'Failed to create a default doctor'
        return HttpResponseServerError()

    try:
        sample_patient = Patient(first_name="Alexis",
                                 last_name="Advantageous",
                                 gps_coordinates="101010",
                                 address="Yonge street",
                                 date_of_birth="1999-06-10",
                                 phone="646646646464",
                                 health_id="324324234",
                                 gender="Male",
                                 email="*****@*****.**")

        sample_patient.save()
    except IntegrityError:
        print 'Failed to create a default patient'
        return HttpResponseServerError()

    try:
        comment = Comment(author=worker_user,
                          text="Trololololol.",
                          time_posted=timezone.now())
        comment.save()
    except IntegrityError:
        print 'Failed to create a default comment'
        return HttpResponseServerError()

    try:
        comment_group = CommentGroup()
        comment_group.save()
        comment_group.comments.add(comment)
    except IntegrityError:
        print 'Failed to create a default comment group'
        return HttpResponseServerError()

    try:
        sample_case = Case(patient=sample_patient,
                           submitter=worker,
                           lock_holder=None,
                           status=1,
                           priority=10,
                           submitter_comments=comment_group,
                           date_opened="2012-12-12")
        sample_case.save()
    except IntegrityError:
        print 'Failed to create a default worker user'
        return HttpResponseServerError()

    return [
        worker_user, worker, doctor_user, doctor, sample_patient, comment,
        comment_group, sample_case, doctor2_user, doctor2
    ]
def display_case(request, case_id, mode='v'):

    ''' Displays the specified case. '''

    user = request.user

    case = Case.objects.filter(id=case_id)[0]

    if request.method == 'POST':

        if mode == 'c':

            priority_form = UpdateCasePriorityForm()
            status_form = UpdateCaseStatusForm()
            adopt_form = UpdateCaseLockHolderForm()
            print "POST:" + str(request.POST)
            comment_form = PostCommentForm(request.POST, request.FILES)
            if comment_form.is_valid():

                try:

                    # Id of the parent, actually
                    comment_id = comment_form.cleaned_data['comment_id']

                    comments = comment_form.cleaned_data['comments']

                    scan_image = comment_form.cleaned_data['scan_image']

                    comment = Comment(author=user, text=comments,
                                      time_posted=timezone.now())
                    comment.save()

                    if scan_image != None:

                        scan = Scan(patient=case.patient, comments="")
                        scan.save()
                        scan.file = scan_image
                        scan.save()
                        case.scans.add(scan)

                        comment.scans.add(scan)

                    if comment_id == -1:

                        # Search for a group with the user id.
                        matching_group = None

                        # Check submitter comments for a match
                        if (hasattr(user, "worker") and
                                case.submitter == user.worker):
                            case.submitter_comments.comments.add(comment)
                        else:

                            # Check reviewer comments for a match
                            groups = CommentGroup.objects.all().filter(
                            reviewed_case_set=case)[:]

                            for group in groups:
                                comments = group.comments.all()

                                if len(comments) > 0:
                                    author = comments[0].author
                                    if author == user:
                                        matching_group = group
                                        break

                            if matching_group == None:
                                matching_group = CommentGroup()
                                matching_group.save()

                            matching_group.comments.add(comment)

                            case.reviewer_comments.add(matching_group)

                    else:

                        matching_comments = Comment.objects.filter(
                                id=comment_id)

                        if len(matching_comments) == 0:
                            return HttpResponseServerError()

                        parent_comment = matching_comments[0]
                        parent_comment.children.add(comment)

                    case.save()
                except IntegrityError, e:
                    print str(e)
                    print "hard fail"
                    return HttpResponseServerError()

            else:

                print "Invalid PostCommentForm."

            # In any case, clear the comment form.
            comment_form.fields["comments"].initial = ""

        elif mode == 'p':

            # Priority may not be changed if closed.
            if case.status == 2:
                return HttpResponseServerError()

            priority_form = UpdateCasePriorityForm(request.POST)
            status_form = UpdateCaseStatusForm()
            adopt_form = UpdateCaseLockHolderForm()
            comment_form = PostCommentForm()
            if priority_form.is_valid():

                priority = int(priority_form.cleaned_data['priority'])

                # Assert that the priority is non-negative
                if priority < 0:
                    return HttpResponseServerError()

                try:
                    case.priority = priority
                    case.save()
                except IntegrityError, e:
                    print str(e)
                    print "hard fail"
                    return HttpResponseServerError()
def populate_default_test_data():
    ''' Populates the database with default test data. '''

    try:
        worker_user = createUser('theworker', '*****@*****.**', 'password')
        worker_user.save()
        worker = Worker(user=worker_user)
        worker.registration_time = timezone.now()
        worker.save()

    except IntegrityError:
        print 'Failed to create a default worker user'
        return HttpResponseServerError()

    try:
        doctor_user = createUser('thedoctor', '*****@*****.**', 'thepassword')
        doctor_user.save()
        doctor = Doctor(user=doctor_user)
        doctor.user_id = doctor_user.id
        doctor.registration_time = timezone.now()
        doctor.save()
    except IntegrityError:
        print 'Failed to create a default doctor'
        return HttpResponseServerError()

    try:
        doctor2_user = createUser('doctor2', '*****@*****.**', 'd2password')
        doctor2_user.save()
        doctor2 = Doctor(user=doctor2_user)
        doctor2.user_id = doctor2_user.id
        doctor2.registration_time = timezone.now()
        doctor2.save()
    except IntegrityError:
        print 'Failed to create a default doctor'
        return HttpResponseServerError()

    try:
        sample_patient = Patient(
                first_name="Alexis",
                last_name="Advantageous",
                gps_coordinates="101010",
                address="Yonge street",
                date_of_birth="1999-06-10",
                phone="646646646464",
                health_id="324324234",
                gender="Male",
                email="*****@*****.**"
            )

        sample_patient.save()
    except IntegrityError:
        print 'Failed to create a default patient'
        return HttpResponseServerError()

    try:
        comment = Comment(author=worker_user,
                          text="Trololololol.",
                          time_posted=timezone.now())
        comment.save()
    except IntegrityError:
        print 'Failed to create a default comment'
        return HttpResponseServerError()

    try:
        comment_group = CommentGroup()
        comment_group.save()
        comment_group.comments.add(comment)
    except IntegrityError:
        print 'Failed to create a default comment group'
        return HttpResponseServerError()

    try:
        sample_case = Case(
                patient=sample_patient,
                submitter=worker,
                lock_holder=None,
                status=1,
                priority=10,
                submitter_comments=comment_group,
                date_opened="2012-12-12"
            )
        sample_case.save()
    except IntegrityError:
        print 'Failed to create a default worker user'
        return HttpResponseServerError()

    return [worker_user, worker, doctor_user, doctor, sample_patient, comment,
            comment_group, sample_case, doctor2_user, doctor2]