def get(request): date = request.GET.get('date') if not date: return json_error('You must specify date') date = datetime.datetime.strptime(date, "%Y-%m-%d").date() doctor_id = request.GET.get('doctor') if not doctor_id: return json_error('You must specify doctor') try: doctor = UserProfile.objects.get(pk=doctor_id) except UserProfile.DoesNotExist: return json_error('You must specify id of existing doctor') create_visit_for_doctor(doctor, date) visits = Visit.objects.filter(doctor=doctor, start_date__date=date).order_by('start_date') visit_serializer = VisitSerializer(visits, many=True, context={'user': request.user}) if request.GET.get('export', False): context = { 'visits': visit_serializer.data, 'doctor': doctor, 'date': date } html = render_to_string('export.html', context=context) html_document = HTML(string=html) pdf_buffer = BytesIO() html_document.write_pdf(pdf_buffer) response = HttpResponse(pdf_buffer.getvalue(), content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="report_%s_%s.pdf"' % (doctor.get_full_name(), date.strftime("%d %b %Y")) return response return JsonResponse(visit_serializer.data, safe=False)
def patch(request, pk, visit): data = request.json if not request.user.can_change_visit(): if visit.patient: return json_error("You don't have permission to cancel visit of another user") data = {'patient': request.user.pk} visit_serializer = VisitSerializer(visit, data=data, partial=True, context={'user': request.user}) if visit_serializer.is_valid(): visit_serializer.save() return JsonResponse(visit_serializer.data) return json_error(visit_serializer.errors)
def get(request): name = request.GET.get('name', '') try: role = int(request.GET.get('role', UserProfile.DOCTOR)) except TypeError: return json_error('Role must be number') hospital = request.GET.get('hospital') if not request.user.can_change_visit() and role != UserProfile.DOCTOR: return json_error("You can see only doctors") users = UserProfile.objects.filter(Q(first_name__icontains=name) | Q(last_name__icontains=name), role=role) if hospital: users = users.filter(hospital=hospital) user_serializer = UserSerializer(users, many=True) return JsonResponse(user_serializer.data, safe=False)
def _wrapped_view(request, *args, **kwargs): if request.method in ['POST', 'PUT', 'DELETE', 'PATCH']: if request.body: try: request.json = json.loads(request.body.decode()) except ValueError: return json_error('Invalid JSON input') else: request.json = {} return view_func(request, *args, **kwargs)
def post(request): hospital_serializer = HospitalSerializer(data=request.json) if hospital_serializer.is_valid(): hospital_serializer.save() return JsonResponse({}) return json_error(hospital_serializer.errors)
def patch(request): user_serializer = UserSerializer(request.user, data=request.json, partial=True) if user_serializer.is_valid(): user_serializer.save() return JsonResponse(user_serializer.data) return json_error(user_serializer.errors)
def post(request): user_serializer = UserSerializer(data=request.json) if user_serializer.is_valid(): user_serializer.save() return JsonResponse({}) return json_error(user_serializer.errors)
def delete(request, pk, visit): if not request.user.can_change_visit() and visit.patient != request.user: return json_error("You don't have permission to remove visit for another user") visit.patient = None visit.save() return JsonResponse({})
def patch(request, pk, hospital): hospital_serializer = HospitalSerializer(hospital, data=request.json, partial=True) if hospital_serializer.is_valid(): hospital_serializer.save() return JsonResponse(hospital_serializer.data) return json_error(hospital_serializer.errors)