def public_order(request): """ Handles dispplaying order based on secret key """ # Get secret key from GET parameters key = request.GET.get('key') # If there's no key, show 404 if not key: raise Http404('bad key') # If we have a key, try to find a matching OrderKey and grab the corresponding order order_key = get_object_or_404(OrderKey, secret_key=key) cur_order = order_key.order # Prepare variables for template and display context = { "cur_order": cur_order, "thumbnails": get_image_files(cur_order.images.all()), 'show_medical': True, } return render(request, 'order_pub.html', context)
def render_to_pdf(request, order_id): """ Generate a PDF for the provided Order """ # Attempt to grab order via order_id from url. 404 if not found. try: cur_order = Order.objects.get(pk=order_id) except Order.DoesNotExist: raise Http404 # Ensure only patients can submit surveys, and that they can only submit surveys on their own orders if not is_in_group( request.user, "Patient") or cur_order.patient.pk != request.user.user_obj.pk: return Http404 # Define which user groups can see medical info, add to context medical_groups = ['Technicians', 'Radiologists', 'Physicians'] context = { 'cur_order': cur_order, 'user': request.user, 'url': request.build_absolute_uri('/')[:-1], 'show_medical': is_in_group(request.user, medical_groups) } # Send thumbnails into context and render HTML context['thumbnails'] = get_image_files(cur_order.images.all()) template = get_template('pdf_temp.html') html = template.render(context) result = BytesIO() # Free PythonAnywhere accounts do not have access to this type of file download... try: pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result) except: return None if not pdf.err: return HttpResponse(result.getvalue(), content_type='application/pdf') return None
def order(request, order_id): # Attempt to grab order via order_id from url. 404 if not found. try: cur_order = Order.objects.get(pk=order_id) pat_id = cur_order.get_patient_id() print(cur_order.modality) pat = Patient.objects.get(pk=pat_id) print(pat.pat_email) except Order.DoesNotExist: raise Http404 # Check if we have a POST request if request.method == 'POST': print('request pot') print(cur_order.level_id, request.user) # Check if level and permissions for the logged in user are both receptionists or admins if cur_order.level_id == 1 and is_in_group( request.user, ['Receptionists', 'Administrators']): print('permission tocheck') # Assign POST data to selection form, check if it's valid, and save if so form = TeamSelectionForm(data=request.POST, instance=cur_order) print(form) if form.is_valid(): modal = cur_order.modality time = "30" calc = PriceCaculator(modal, time) price = calc.calcPrice() #send email that shows current account balance send_mail( subject='Account Balance', message= 'Thank you for your visit! Your account balance is currently:' + ' ' + '$' + str(price), from_email='*****@*****.**', recipient_list=["*****@*****.**"], fail_silently=False) form.save() else: # Show errors messages = { 'headline1': 'Invalid Form', 'headline2': 'Please try again.', 'headline3': f"{form.errors}" } return show_message(request, messages) # Check if level and permissions for the logged in user are both technicians elif cur_order.level_id == 2 and is_in_group( request.user, ['Technicians', 'Radiologists']): if request.user in cur_order.team.technicians.all( ) | cur_order.team.technicians.all(): # Save image complete info cur_order.imaged = request.user.get_username() cur_order.imaged_time = timezone.now() cur_order.save() else: # Show auth error messages = { 'headline1': 'Not Authorized', 'headline2': '', 'headline3': '', } return show_message(request, messages) # Check if level and permissions for the logged in user are both radiology elif cur_order.level_id == 3 and is_in_group(request.user, ['Radiologists']): if request.user in cur_order.team.radiologists.all(): # Set up data in our form and check validity of data. form = AnalysisForm(data=request.POST, instance=cur_order) if form.is_valid(): print("completed") # Save form, then grab saved item form.save() cur_order.refresh_from_db() # Add completed user and completed time to record, then save cur_order.completed = request.user.get_username() cur_order.completed_time = timezone.now() cur_order.save() else: # Show form errors messages = { 'headline1': 'Invalid Form', 'headline2': 'Please try again.', 'headline3': f"{form.errors}" } return show_message(request, messages) else: # Show auth error messages = { 'headline1': 'Not Authorized', 'headline2': '', 'headline3': '', } return show_message(request, messages) else: # Show invalid request error messages = { 'headline1': 'Order already complete.', 'headline2': '', 'headline3': '', } return show_message(request, messages) # If we've made it to there, that means we've successfully submitted the order. # Therefore, we'll re-grab it from the DB and increment it's level by one. print(cur_order) cur_order.refresh_from_db() cur_order.level_id += 1 cur_order.save() # Send an email notification to the correct user(s) send_notification.now(order_id) # Set up the variables for our template context = { "cur_order": cur_order, } # Check for user permission and level order. Add appropriate elements for template rendering. if cur_order.level_id == 1 and is_in_group( request.user, ['Receptionists', 'Administrators']): # Add scheduler form if not yet checked in context['schedule_form'] = ScheduleForm(instance=cur_order) context['checkin_form'] = TeamSelectionForm(instance=cur_order) elif cur_order.level_id == 2 and is_in_group( request.user, ['Technicians', 'Radiologists']): # Prepare context for template if at checked in step if request.user in cur_order.team.radiologists.all( ) | cur_order.team.technicians.all(): context['image_form'] = ImageUploadForm(instance=cur_order) elif cur_order.level_id == 3 and is_in_group(request.user, ['Radiologists']): # Prepare context for template if at imaging complete step if request.user in cur_order.team.radiologists.all(): context['analysis_form'] = AnalysisForm(instance=cur_order) elif cur_order.level_id == 4: # Prepare context for template if at analysis complete step pass elif cur_order.level_id == 5: # Prepare context for template if archived pass # Define which user groups can see medical info, add to context medical_groups = ['Technicians', 'Radiologists', 'Physicians'] context['show_medical'] = is_in_group(request.user, medical_groups) # Send thumbnails into context and render HTML context['thumbnails'] = get_image_files(cur_order.images.all()) return render(request, 'order.html', context)
def order(request, order_id): # Attempt to grab order via order_id from url. 404 if not found. try: cur_order = Order.objects.get(pk=order_id) except Order.DoesNotExist: raise Http404 # Check if we have a POST request if request.method == 'POST': # Check if level and permissions for the logged in user are both receptionists or admins if cur_order.level_id == 1 and is_in_group( request.user, ['Receptionists', 'Administrators']): # Assign POST data to selection form, check if it's valid, and save if so form = TeamSelectionForm(data=request.POST, instance=cur_order) if form.is_valid(): form.save() else: # Show errors messages = { 'headline1': 'Invalid Form', 'headline2': 'Please try again.', 'headline3': f"{form.errors}" } return show_message(request, messages) # Check if level and permissions for the logged in user are both technicians elif cur_order.level_id == 2 and is_in_group( request.user, ['Technicians', 'Radiologists']): if request.user in cur_order.team.technicians.all( ) | cur_order.team.technicians.all(): # Save image complete info cur_order.imaged = request.user.get_username() cur_order.imaged_time = timezone.now() cur_order.save() else: # Show auth error messages = { 'headline1': 'Not Authorized', 'headline2': '', 'headline3': '', } return show_message(request, messages) # Check if level and permissions for the logged in user are both radiology elif cur_order.level_id == 3 and is_in_group(request.user, ['Radiologists']): if request.user in cur_order.team.radiologists.all(): # Set up data in our form and check validity of data. # send invoice form = AnalysisForm(data=request.POST, instance=cur_order) if form.is_valid(): # Save form, then grab saved item form.save() cur_order.refresh_from_db() # Add completed user and completed time to record, then save cur_order.completed = request.user.get_username() cur_order.completed_time = timezone.now() cur_order.save() if cur_order.modality == ModalityOption.objects.get( name='MRI'): price = 2611 if cur_order.modality == ModalityOption.objects.get( name='CAT Scan'): price = 1200 if cur_order.modality == ModalityOption.objects.get( name='X-Ray'): price = 460 a = Invoice(order=cur_order, patient=cur_order.patient, total=price) a.save() else: # Show form errors messages = { 'headline1': 'Invalid Form', 'headline2': 'Please try again.', 'headline3': f"{form.errors}" } return show_message(request, messages) else: # Show auth error messages = { 'headline1': 'Not Authorized', 'headline2': '', 'headline3': '', } return show_message(request, messages) else: # Show invalid request error messages = { 'headline1': 'Order already complete.', 'headline2': '', 'headline3': '', } return show_message(request, messages) # If we've made it to there, that means we've successfully submitted the order. # Therefore, we'll re-grab it from the DB and increment it's level by one. cur_order.refresh_from_db() cur_order.level_id += 1 cur_order.save() # Send an email notification to the correct user(s) send_notification.now(order_id) # Set up the variables for our template context = { "cur_order": cur_order, } # Check for user permission and level order. Add appropriate elements for template rendering. if cur_order.level_id == 1 and is_in_group( request.user, ['Receptionists', 'Administrators']): # Add scheduler form if not yet checked in context['schedule_form'] = ScheduleForm(instance=cur_order) context['checkin_form'] = TeamSelectionForm(instance=cur_order) elif cur_order.level_id == 2 and is_in_group( request.user, ['Technicians', 'Radiologists']): # Prepare context for template if at checked in step if request.user in cur_order.team.radiologists.all( ) | cur_order.team.technicians.all(): context['image_form'] = ImageUploadForm(instance=cur_order) elif cur_order.level_id == 3 and is_in_group(request.user, ['Radiologists']): # Prepare context for template if at imaging complete step if request.user in cur_order.team.radiologists.all(): context['analysis_form'] = AnalysisForm(instance=cur_order) elif cur_order.level_id == 4: # Prepare context for template if at analysis complete step pass elif cur_order.level_id == 5: # Prepare context for template if archived pass # Define which user groups can see medical info, add to context medical_groups = ['Technicians', 'Radiologists', 'Physicians'] context['show_medical'] = is_in_group(request.user, medical_groups) # Send thumbnails into context and render HTML context['thumbnails'] = get_image_files(cur_order.images.all()) return render(request, 'order.html', context)