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()
            # Only doctors may lock a case
            if not hasattr(user, "doctor"):
                    return HttpResponseServerError()

            # Assert that if the case is already locked, only the current
            # holder can change it.
            if (case.lock_holder != None and
                         case.lock_holder != user.doctor):
                    return HttpResponseServerError()

            # Assert that if the case is closed, the lock can't be changed.
            if (case.status == 2):
                    return HttpResponseServerError()

            priority_form = UpdateCasePriorityForm()
            status_form = UpdateCaseStatusForm()
            adopt_form = UpdateCaseLockHolderForm(request.POST)
            comment_form = PostCommentForm()
            if adopt_form.is_valid():
                toggle_field = int(adopt_form.cleaned_data['toggle_field'])

                if toggle_field == 1:

                    try:
                        case.lock_holder = user.doctor
                        case.save()
                    except IntegrityError, e:
                        print str(e)
                        print "hard fail"
                        return HttpResponseServerError()