def edit_record(request, patient_id, record_id): try: if request.user.is_authenticated: record = get_object_or_404(Record, id=record_id) if request.user == record.author.user: if request.method == "POST": if request.POST.get("session_datetime"): record.session_datetime = request.POST.get("session_datetime") if (request.POST.get("session_duration_hours") and request.POST.get("session_duration_minutes")): ses_dur_hrs = request.POST.get("session_duration_hours") ses_dur_mins = request.POST.get("session_duration_minutes") session_duration = datetime.strptime(ses_dur_hrs + ":" + ses_dur_mins, '%H:%M') record.session_duration = session_duration if request.POST.get("session_resume"): record.session_resume = request.POST.get("session_resume") record.save() return redirect_record(patient_id, record_id) else: ses_dur_hrs = record.session_duration.strftime('%H') ses_dur_mins = record.session_duration.strftime('%M') return render(request, 'records/edit_record.html', {'record': record, 'session_duration_hours': ses_dur_hrs, 'session_duration_minutes': ses_dur_mins, } ) else: return redirect_home() else: return HttpResponseRedirect("/login") except Exception as e: print(e) return redirect_home()
def my_records_list(request, patient_id): if request.user.is_authenticated: try: professional = Professional.objects.get(user=request.user) patient = Patient.objects.get(id=patient_id) case = Case.objects.filter(Q(patient=patient), Q(professional=professional) | Q(coordinator=professional)) if not case: raise ObjectDoesNotExist record_list = Record.objects.all().filter(author=professional,patient=patient).order_by('-session_datetime') paginator = Paginator(record_list, 15) # Show 15 records per page page = request.GET.get('page') page_records = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. page_records = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. page_records = paginator.page(paginator.num_pages) except ObjectDoesNotExist: return redirect_home() except Exception as e: print(e) return redirect_home() return render(request, 'records/record_list.html', { 'page_records': page_records, 'patient': patient, }) else: return HttpResponseRedirect("/login")
def admin_home(request): if request.user.is_authenticated and request.user.is_staff: return render(request, 'administration/home.html', {}) elif request.user.is_authenticated: return redirect_home() else: return render(request, 'login/login.html')
def create_article(request): if request.user.is_authenticated and request.user.is_staff: if request.method == 'POST': # Create a form instance and populate it with data from the request form = ArticleForm(request.POST) # Check whether is valid: if form.is_valid(): title = form.cleaned_data['title'] content = form.cleaned_data['content'] is_draft = form.cleaned_data['is_draft'] new_article = Article.objects.create(author=request.user, title=title, content=content, is_draft=is_draft) new_article.save() add_log(request.user.username, "add", "news", new_article.log_str()) return HttpResponseRedirect('/administration/') else: form = ArticleForm() error_message = ("Error en los datos ingresados.") return render(request, 'news/create_article.html', { 'form': form, 'error_message': error_message }) # if a GET (or any other method) we'll create a blank form else: form = ArticleForm() return render(request, 'news/create_article.html', {'form': form}) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login")
def modify_comment(request, goal_id, comment_id, subgoal_id=None): if subgoal_id: supgoal_id = goal_id goal_id = subgoal_id goal = get_object_or_404(AbstractGoal, pk=goal_id) comment = get_object_or_404(Comment, pk=comment_id) if request.user.is_authenticated: if (request.user == goal.owner): if request.method == "POST": if request.POST.get("comment_text"): comment.text = request.POST.get("comment_text") comment.save() if subgoal_id: return redirect_goal(supgoal_id, goal_id) else: return redirect_goal(goal_id) else: if subgoal_id: return render(request, 'commentary/modify_subcomment.html', { 'goal': goal, 'comment': comment, 'supgoal_id': supgoal_id }) else: return render(request, 'commentary/modify_comment.html', { 'goal': goal, 'comment': comment }) else: return redirect_home(request.user.username) else: return HttpResponseRedirect("/login")
def create_record_from_patient(request, patient_id): try: if request.user.is_authenticated: professional = Professional.objects.get(user=request.user) patient = Patient.objects.get(id=patient_id) case = Case.objects.all().filter(Q(patient=patient), Q(professional=professional) | Q(coordinator=professional)) if case: if request.method == "POST": ses_dur_hrs = request.POST.get("session_duration_hours") ses_dur_mins = request.POST.get("session_duration_minutes") session_duration = datetime.strptime(ses_dur_hrs + ":" + ses_dur_mins, '%H:%M') record = Record(session_datetime=request.POST.get("session_datetime"), session_resume=request.POST.get("session_resume"), case=case.first(), author=professional, patient=patient, session_duration=session_duration) record.save() notification = Notification(record=record) notification.save() return redirect_my_patient_records(patient_id) else: return render(request, 'records/create_record_from_patient.html', {'patient': patient }) else: return redirect_home() else: return HttpResponseRedirect("/login") except Exception as e: print(e) return HttpResponseRedirect("/login")
def new_comment(request, patient_id, record_id): record = get_object_or_404(Record, id=record_id) if request.user.is_authenticated: professional = Professional.objects.get(user=request.user) patient = Patient.objects.get(id=patient_id) case = Case.objects.all().filter( Q(professional=professional) | Q(coordinator=professional), Q(patient=patient)) if case: if request.method == "POST": if request.POST.get("comment_text"): comment = Comment(owner=professional, record=record, text=request.POST.get("comment_text")) comment.save() return redirect_record(patient.id, record.id) else: return render(request, 'commentary/new_comment.html', {'record': record}) else: return redirect_home() else: return HttpResponseRedirect("/login")
def category_edit(request,category_id): if request.user.is_authenticated: try: user = User.objects.get(username=request.user.username) category = get_object_or_404(Category, pk=category_id) goals_all = user.abstractgoal_set.all() except Exception as e: return HttpResponse("The user do not exist") if request.method == "POST": if request.POST.get("category_tipo"): category.category_tipo = request.POST.get("category_tipo") if request.POST.getlist('goal_add[]'): add_goal_list = request.POST.getlist('goal_add[]') for add_goal in add_goal_list: category.goal.add(user.abstractgoal_set.get(pk=add_goal)) if request.POST.getlist('goal_rem[]'): rem_goal_list = request.POST.getlist('goal_rem[]') for rem_goal in rem_goal_list: category.goal.remove(user.abstractgoal_set.get(pk=rem_goal)) category.save() if request.POST.get("deleted"): category.delete() return HttpResponseRedirect("/category") else: if user == category.owner: return render(request,'category/category_edit.html',{'category' : category, 'goals_all' : goals_all}) else: return redirect_home(request.user) else: return HttpResponseRedirect("/login")
def category_info(request,category_id): if request.user.is_authenticated: category = get_object_or_404(Category, pk=category_id) if request.user == category.owner: return render(request,'category/category_info.html',{'category' : category}) else: return redirect_home(request.user) else: return HttpResponseRedirect("/login")
def modify_article(request, article_id): try: if request.user.is_authenticated and request.user.is_staff: article = Article.objects.get(id=article_id) if request.method == 'POST': form = ArticleEditForm(request.POST) if form.is_valid(): article.title = form.cleaned_data['title'] article.content = form.cleaned_data['content'] article.is_draft = form.cleaned_data['is_draft'] article.save() add_log(request.user.username, "mod", "news", article.log_str()) return HttpResponseRedirect('/administration/') else: error_message = "Entradas invalidas." return render( request, 'news/modify_article.html', { 'form': form, 'article_id': article_id, 'error_message': error_message }) # if a GET (or any other method) we'll create the populated form else: # Create a form instance and populate it with data from the request pre_data = { 'title': article.title, 'content': article.content, 'is_draft': article.is_draft, } form = ArticleEditForm(initial=pre_data) return render(request, 'news/modify_article.html', { 'form': form, 'article_id': article.id }) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login") except Exception as e: return redirect_home()
def all_records_list(request, patient_id): if request.user.is_authenticated: try: prof = Professional.objects.get(user=request.user) patient = Patient.objects.get(id=patient_id) # Check if the logged user is inside any of the cases of the patient case = Case.objects.filter( Q(patient=patient), Q(professional=prof) | Q(coordinator=prof)) if not case: # If it's not in any of the cases (it's trying to access a # record from a patient he doesn't have), redirect to homepage raise ObjectDoesNotExist record_list = Record.objects.all().filter( patient=patient).order_by('-session_datetime') paginator = Paginator(record_list, 15) # Show 15 records per page page = request.GET.get('page') page_records = paginator.page(page) except PageNotAnInteger: # If page is not an integer, deliver first page. page_records = paginator.page(1) except EmptyPage: # If page is out of range (e.g. 9999), deliver last page of results. page_records = paginator.page(paginator.num_pages) except ObjectDoesNotExist: return redirect_home() except Exception as e: print(e) return redirect_home() return render(request, 'records/record_list.html', { 'page_records': page_records, 'patient': patient, }) else: return HttpResponseRedirect("/login")
def delete_record(request, patient_id, record_id): if request.user.is_authenticated: record = get_object_or_404(Record, id=record_id) if record.author.user == request.user: record.delete() return redirect_my_patient_records(patient_id) else: return redirect_home() else: return HttpResponseRedirect("/login")
def article_detail(request, article_id): if request.user.is_authenticated: try: article = Article.objects.get(id=article_id) except Exception as e: return redirect_home() return render(request, 'news/article_detail.html', { 'article': article, }) else: return HttpResponseRedirect("/login")
def delete_goal(request, goal_id): goal = get_object_or_404(Goal, pk=goal_id) if request.user.is_authenticated: if request.user == goal.owner: if request.method == "POST": goal.delete() return redirect_home(request.user.username) else: return render(request, 'goal/delete.html', {'goal': goal}) else: return HttpResponseRedirect("/login")
def detail_goal(request, goal_id): if request.user.is_authenticated: goal = get_object_or_404(Goal, pk=goal_id) if request.user == goal.owner: comments = (Comment.objects.all().filter( goal=goal).order_by('-create_date')) return render(request, 'goal/detail.html', { 'goal': goal, 'comments': comments }) return redirect_home(request.user)
def delete_comment(request, patient_id, record_id, comment_id): comment = get_object_or_404(Comment, id=comment_id) record = get_object_or_404(Record, id=record_id) if request.user.is_authenticated: if (request.user == comment.owner.user): if request.method == "GET": comment.delete() return redirect_record(patient_id, record.id) else: return redirect_home() else: return HttpResponseRedirect("/login")
def select_records(request, patient_id): if request.user.is_authenticated: try: professional = Professional.objects.get(user=request.user) patient = Patient.objects.get(id=patient_id) case = Case.objects.filter(Q(patient=patient), Q(professional=professional) | Q(coordinator=professional)) if not case: raise ObjectDoesNotExist return render(request, 'records/select_records.html', { 'patient': patient, }) except ObjectDoesNotExist: return redirect_home() except Exception as e: print(e) return redirect_home() else: return HttpResponseRedirect("/login")
def secretary_detail(request, secretary_id): try: if request.user.is_authenticated and request.user.is_staff: try: secretary = Secretary.objects.get(id=secretary_id) return render(request, 'administration/secretary_detail.html', {'secretary': secretary}) except ObjectDoesNotExist as e: print(e) return redirect_home() except Exception as e: return HttpResponse(e) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login") except ObjectDoesNotExist as e: print(e) return HttpResponseRedirect('/administration/') except Exception as e: return HttpResponse(e)
def patient_detail(request, patient_id): if request.user.is_authenticated and request.user.is_staff: try: patient = Patient.objects.get(id=patient_id) return render(request, 'administration/patient_detail.html', {'patient': patient}) except ObjectDoesNotExist as e: print(e) return redirect_home() except Exception as e: return HttpResponse(e) else: return render(request, 'login/login.html')
def record_detail(request, patient_id, record_id): try: if request.user.is_authenticated: prof = Professional.objects.get(user=request.user) patient = Patient.objects.get(id=patient_id) case = Case.objects.all().filter(Q(professional=prof) | Q(coordinator=prof) ,Q(patient=patient)) if case: try: record = Record.objects.get(id=record_id) comments = (Comment.objects.all().filter(record=record) .order_by('create_date')) # Assuming there is only one coordinator per patient, if the # record is case-orphan we assign the new case to the first case # that has professional as a professional or as a coordinator, # and patient as a patient if record.case is None: record.case = case.first() record.save() except ObjectDoesNotExist: return redirect_home() except Exception as e: return HttpResponse(e) return render(request, 'records/record_detail.html', { 'record': record, 'comments': comments, }) else: return redirect_home() else: return HttpResponseRedirect("/login") except Exception as e: print(e) return redirect_home()
def modify_article_menu(request): try: if request.user.is_authenticated and request.user.is_staff: if request.method == 'POST': # Create a form instance and populate it with data from the request form = ArticleListForm(request.POST) # Check whether is valid: if form.is_valid(): article = form.cleaned_data['article'] return redirect('news:modify_article', article_id=article.id) else: return HttpResponseRedirect('/news/') # if a GET (or any other method) we'll create a blank form else: form = ArticleListForm() return render(request, 'news/modify_article_menu.html', {'form': form}) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login") except Exception as e: return redirect_home()
def delete_comment(request, goal_id, comment_id, subgoal_id=None): comment = get_object_or_404(Comment, pk=comment_id) goal = get_object_or_404(AbstractGoal, pk=goal_id) if request.user.is_authenticated: if (request.user == goal.owner): if request.method == "GET": comment.delete() if subgoal_id: return redirect_goal(goal_id, subgoal_id) else: return redirect_goal(goal_id) else: return redirect_home(request.user.username) else: return HttpResponseRedirect("/login")
def patient_list(request): if request.user.is_authenticated: try: prof = Professional.objects.get(user=request.user) patients_ids = Case.objects.all().filter(Q(professional=prof) | Q(coordinator=prof)).values("patient__id") patient_list = Patient.objects.filter(id__in=patients_ids).order_by('last_name') except Exception as e: return redirect_home() return render(request, 'records/patient_list.html', { 'patient_list': patient_list, }) else: return HttpResponseRedirect("/login")
def professional_detail(request, prof_id): try: if request.user.is_authenticated and request.user.is_staff: try: prof = Professional.objects.get(id=prof_id) return render(request, 'administration/prof_detail.html', {'prof': prof}) except ObjectDoesNotExist as e: print(e) return redirect_home() except Exception as e: return HttpResponse(e) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login") except ObjectDoesNotExist as e: print(e) return HttpResponseRedirect('/administration/') except Exception as e: return HttpResponse(e)
def patient_list(request): if request.user.is_authenticated: try: prof = Professional.objects.get(user=request.user) cases = Case.objects.all().filter(Q(professional=prof) | Q(coordinator=prof))\ .distinct('patient').values_list('id', flat=True) case_list = Case.objects.filter( id__in=cases).order_by('patient__last_name') except Exception as e: return redirect_home() return render(request, 'records/patient_list.html', { 'case_list': case_list, }) else: return HttpResponseRedirect("/login")
def change_pwd_prof(request, prof_id): try: if request.user.is_authenticated and request.user.is_staff: prof = Professional.objects.get(id=prof_id) if request.method == 'POST': form = ChangePasswordForm(request.POST) if form.is_valid(): prof.user.set_password(form.cleaned_data['password']) prof.user.save() prof.save() add_log(request.user.username, "chpwd", "prof", prof.user.username) return HttpResponseRedirect('/administration/') else: error_message = "Elija una contraseña valida" prof_username = prof.user.username return render( request, 'administration/prof_change_pwd.html', { 'form': form, 'prof_id': prof_id, 'prof_username': prof_username, 'error_message': error_message }) else: prof_username = prof.user.username form = ChangePasswordForm() return render( request, 'administration/prof_change_pwd.html', { 'form': form, 'prof_id': prof.id, 'prof_username': prof_username, }) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login") except ObjectDoesNotExist as e: print(e) return HttpResponseRedirect('/administration/') except Exception as e: return HttpResponse(e)
def list_professional(request): if request.user.is_authenticated and request.user.is_staff: if request.method == 'POST': # Create a form instance and populate it with data from the request form = ProfessionalListForm(request.POST) # Check whether is valid: if form.is_valid(): prof = form.cleaned_data['professional'] return redirect('administration:professional_detail', prof_id=prof.id) else: return HttpResponseRedirect('/administration/') else: form = ProfessionalListForm() return render(request, 'administration/list_professional.html', {'form': form}) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login")
def delete_article(request): if request.user.is_authenticated and request.user.is_staff: if request.method == 'POST': # Create a form instance and populate it with data from the request form = ArticleListForm(request.POST) # Check whether is valid: if form.is_valid(): article = form.cleaned_data['article'] add_log(request.user.username, "del", "news", article.log_str()) article.delete() return HttpResponseRedirect('/administration/') # if a GET (or any other method) we'll create a blank form else: form = ArticleListForm() return render(request, 'news/delete_article.html', {'form': form}) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login")
def modify_patient_menu(request): if request.user.is_authenticated and request.user.is_staff: if request.method == 'POST': # Create a form instance and populate it with data from the request form = PatientListForm(request.POST) # Check whether is valid: if form.is_valid(): patient = form.cleaned_data['patient'] return redirect('administration:modify_patient', patient_id=patient.id) else: return HttpResponseRedirect('/administration/') else: form = PatientListForm() return render(request, 'administration/modify_patient_menu.html', {'form': form}) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login")
def list_secretary(request): if request.user.is_authenticated and request.user.is_staff: if request.method == 'POST': # Create a form instance and populate it with data from the request form = SecretaryListForm(request.POST) # Check whether is valid: if form.is_valid(): secretary = form.cleaned_data['secretary'] return redirect('administration:secretary_detail', secretary_id=secretary.id) else: return HttpResponseRedirect('/administration/') else: form = SecretaryListForm() return render(request, 'administration/list_secretary.html', {'form': form}) elif request.user.is_authenticated: return redirect_home() else: return HttpResponseRedirect("/login")