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))
def upload_image_m(request): data = is_worker(request) if not data: json_response = json.dumps({"success": "false", "type": "notWorker"}) return HttpResponse(json_response, mimetype='application/json') try: case = Case.objects.filter(id=data['case_id'])[0] except KeyError: json_response = json.dumps({"success": "false", "type": "KeyError"}) return HttpResponse(json_response, mimetype='application/json') try: scan = Scan( patient=case.patient) scan.save() except IntegrityError: scan.delete() json_response = json.dumps({"success": "false", "type": "IntegrityError"}) return HttpResponse(json_response, mimetype='application/json') try: image_data = b64decode(data['image_string']) scan.file = ContentFile(image_data, "test.png") scan.save() #comment.scans.add(scan) case.scans.add(scan) #case.scan = scan #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": "uploadSuccess"}) return HttpResponse(json_response, mimetype='application/json')
data = is_worker(request) if not data: json_response = json.dumps({"success": "false", "type": "notWorker"}) return HttpResponse(json_response, mimetype='application/json') try: case = Case.objects.filter(id=data['case_id'])[0] except KeyError: json_response = json.dumps({"success": "false", "type": "KeyError"}) return HttpResponse(json_response, mimetype='application/json') try: scan = Scan( patient=case.patient) scan.save() except IntegrityError: scan.delete() json_response = json.dumps({"success": "false", "type": "IntegrityError"}) return HttpResponse(json_response, mimetype='application/json') try: image_data = b64decode(data['image_string']) scan.file = ContentFile(image_data, "test.png") scan.save() #comment.scans.add(scan) case.scans.add(scan) #case.scan = scan
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()