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))
Example #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')
                    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))
    else:

        # The page has just been entered and so the form hasn't
        # been submitted yet.
        form = NewCaseForm()
        form.populate(patient_id)

    return render_to_response('newcase.html',
                              {'form': form,
                               'viewer': user},
                              context_instance=RequestContext(request))


@login_required
def display_case_list(request):
    ''' Displays the list of cases.'''

    user = request.user

    case_attributes = create_case_attributes(Case.objects)