GeriLife/caregiving

View on GitHub
homes/views.py

Summary

Maintainability
A
0 mins
Test Coverage
B
83%
File not formatted according to black style guide
from typing import Any
File not formatted according to black style guide
 
File not formatted according to black style guide
from django.contrib import messages
File not formatted according to black style guide
from django.contrib.auth import get_user_model
File not formatted according to black style guide
from django.contrib.auth.mixins import LoginRequiredMixin
File not formatted according to black style guide
from django.core.exceptions import PermissionDenied
File not formatted according to black style guide
from django.shortcuts import get_object_or_404
File not formatted according to black style guide
from django.urls import reverse
File not formatted according to black style guide
 
File not formatted according to black style guide
from django.utils.translation import gettext as _
File not formatted according to black style guide
from django.views.generic.edit import FormView
File not formatted according to black style guide
from django.views.generic.detail import DetailView
File not formatted according to black style guide
from django.views.generic.base import TemplateView
File not formatted according to black style guide
 
File not formatted according to black style guide
from homes.forms import AddCaregiverForm
File not formatted according to black style guide
 
File not formatted according to black style guide
from .charts import (
File not formatted according to black style guide
prepare_activity_counts_by_resident_and_activity_type_chart,
File not formatted according to black style guide
prepare_daily_work_percent_by_caregiver_role_and_type_chart,
File not formatted according to black style guide
prepare_monthly_activity_hours_by_caregiver_role_chart,
File not formatted according to black style guide
prepare_monthly_activity_hours_by_type_chart,
File not formatted according to black style guide
prepare_work_by_caregiver_role_and_type_charts,
File not formatted according to black style guide
prepare_work_by_caregiver_role_chart,
File not formatted according to black style guide
prepare_work_by_type_chart,
File not formatted according to black style guide
)
File not formatted according to black style guide
from .models import Home, HomeUserRelation
File not formatted according to black style guide
 
File not formatted according to black style guide
user_model = get_user_model()
File not formatted according to black style guide
 
File not formatted according to black style guide
 
File not formatted according to black style guide
def regroup_homes_by_home_group(homes):
File not formatted according to black style guide
# group homes with group by group name
File not formatted according to black style guide
home_groups_with_homes = {}
File not formatted according to black style guide
 
File not formatted according to black style guide
for home in homes:
File not formatted according to black style guide
if home.home_group.name not in home_groups_with_homes:
File not formatted according to black style guide
home_groups_with_homes[home.home_group.name] = []
File not formatted according to black style guide
 
File not formatted according to black style guide
home_groups_with_homes[home.home_group.name].append(home)
File not formatted according to black style guide
 
File not formatted according to black style guide
# Restructure home_groups_with_homes to a list of tuples
File not formatted according to black style guide
# to make it easier to iterate over in the template
File not formatted according to black style guide
home_groups_with_homes = [
File not formatted according to black style guide
{"group_name": name, "homes": homes}
File not formatted according to black style guide
for name, homes in home_groups_with_homes.items()
File not formatted according to black style guide
]
File not formatted according to black style guide
 
File not formatted according to black style guide
return home_groups_with_homes
File not formatted according to black style guide
 
File not formatted according to black style guide
 
File not formatted according to black style guide
class HomeGroupListView(LoginRequiredMixin, TemplateView):
File not formatted according to black style guide
template_name = "homes/home_group_list.html"
File not formatted according to black style guide
 
File not formatted according to black style guide
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
File not formatted according to black style guide
context = super().get_context_data(**kwargs)
File not formatted according to black style guide
 
File not formatted according to black style guide
user = self.request.user
File not formatted according to black style guide
 
File not formatted according to black style guide
if not user.is_authenticated:
File not formatted according to black style guide
return context
File not formatted according to black style guide
 
File not formatted according to black style guide
if user.is_superuser:
File not formatted according to black style guide
context["homes_without_group"] = Home.objects.filter(
File not formatted according to black style guide
home_group__isnull=True,
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
context["homes_with_group"] = Home.objects.filter(
File not formatted according to black style guide
home_group__isnull=False,
File not formatted according to black style guide
)
File not formatted according to black style guide
else:
File not formatted according to black style guide
context["homes_without_group"] = self.request.user.homes.filter(
File not formatted according to black style guide
home_group__isnull=True,
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
context["homes_with_group"] = self.request.user.homes.filter(
File not formatted according to black style guide
home_group__isnull=False,
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
home_groups_with_homes = regroup_homes_by_home_group(
File not formatted according to black style guide
context["homes_with_group"],
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
context["home_groups_with_homes"] = home_groups_with_homes
File not formatted according to black style guide
 
File not formatted according to black style guide
return context
File not formatted according to black style guide
 
File not formatted according to black style guide
 
File not formatted according to black style guide
# user should be logged in
File not formatted according to black style guide
 
File not formatted according to black style guide
 
File not formatted according to black style guide
class HomeDetailView(LoginRequiredMixin, DetailView):
File not formatted according to black style guide
model = Home
File not formatted according to black style guide
context_object_name = "home"
File not formatted according to black style guide
 
File not formatted according to black style guide
def get_object(self, queryset=None):
File not formatted according to black style guide
if queryset is None:
File not formatted according to black style guide
queryset = self.get_queryset()
File not formatted according to black style guide
 
File not formatted according to black style guide
url_uuid = self.kwargs.get("url_uuid") # Get the url_uuid from the URL
File not formatted according to black style guide
 
File not formatted according to black style guide
if url_uuid is not None:
File not formatted according to black style guide
queryset = queryset.filter(
File not formatted according to black style guide
url_uuid=url_uuid,
File not formatted according to black style guide
) # Filter the queryset based on url_uuid
File not formatted according to black style guide
 
File not formatted according to black style guide
home = get_object_or_404(
File not formatted according to black style guide
queryset,
File not formatted according to black style guide
) # Get the object or return a 404 error if not found
File not formatted according to black style guide
 
File not formatted according to black style guide
# ensure the user has access to the home
File not formatted according to black style guide
if not home.has_access(user=self.request.user):
File not formatted according to black style guide
raise PermissionDenied
File not formatted according to black style guide
 
File not formatted according to black style guide
return home
File not formatted according to black style guide
 
File not formatted according to black style guide
def prepare_activity_charts(self, context):
File not formatted according to black style guide
"""Prepare activity charts and add them to the template context."""
File not formatted according to black style guide
home = context["home"]
File not formatted according to black style guide
 
File not formatted according to black style guide
context["activity_counts_by_resident_and_activity_type_chart"] = (
File not formatted according to black style guide
prepare_activity_counts_by_resident_and_activity_type_chart(home)
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
context["monthly_activity_hours_by_type_chart"] = (
File not formatted according to black style guide
prepare_monthly_activity_hours_by_type_chart(home)
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
context["monthly_activity_hours_by_caregiver_role_chart"] = (
File not formatted according to black style guide
prepare_monthly_activity_hours_by_caregiver_role_chart(home)
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
return context
File not formatted according to black style guide
 
File not formatted according to black style guide
def prepare_work_charts(self, context):
File not formatted according to black style guide
"""Prepare work charts and add them to the template context."""
File not formatted according to black style guide
home = context["home"]
File not formatted according to black style guide
 
File not formatted according to black style guide
context["work_by_type_chart"] = prepare_work_by_type_chart(home)
File not formatted according to black style guide
 
File not formatted according to black style guide
context["work_by_caregiver_role_chart"] = prepare_work_by_caregiver_role_chart(
File not formatted according to black style guide
home,
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
context["daily_work_percent_by_caregiver_role_and_type_chart"] = (
File not formatted according to black style guide
prepare_daily_work_percent_by_caregiver_role_and_type_chart(home)
File not formatted according to black style guide
)
File not formatted according to black style guide
 
File not formatted according to black style guide
context = prepare_work_by_caregiver_role_and_type_charts(context)
File not formatted according to black style guide
 
File not formatted according to black style guide
return context
File not formatted according to black style guide
 
File not formatted according to black style guide
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
File not formatted according to black style guide
"""Add charts and permissions to the template context."""
File not formatted according to black style guide
context = super().get_context_data(**kwargs)
File not formatted according to black style guide
 
File not formatted according to black style guide
home = context["home"]
File not formatted according to black style guide
 
File not formatted according to black style guide
# Check if user can manage the home
File not formatted according to black style guide
context["user_can_manage"] = home.user_can_manage(self.request.user)
File not formatted according to black style guide
 
File not formatted according to black style guide
# Check if work has been recorded
File not formatted according to black style guide
# by selecting one record
File not formatted according to black style guide
context["work_has_been_recorded"] = home.work_performed.exists()
File not formatted according to black style guide
context["activity_has_been_recorded"] = home.activity_performed.exists()
File not formatted according to black style guide
 
File not formatted according to black style guide
# Only prepare charts if work has been recorded
File not formatted according to black style guide
if context["work_has_been_recorded"]:
File not formatted according to black style guide
context = self.prepare_work_charts(context)
File not formatted according to black style guide
if context["activity_has_been_recorded"]:
File not formatted according to black style guide
context = self.prepare_activity_charts(context)
File not formatted according to black style guide
return context
File not formatted according to black style guide
 
File not formatted according to black style guide
 
File not formatted according to black style guide
class HomeUserRelationListView(LoginRequiredMixin, FormView):
File not formatted according to black style guide
form_class = AddCaregiverForm # Use form_class instead of form
File not formatted according to black style guide
template_name = "homes/home_user_relation_list.html"
File not formatted according to black style guide
 
File not formatted according to black style guide
def get_context_data(self, **kwargs):
File not formatted according to black style guide
context = super().get_context_data(**kwargs)
File not formatted according to black style guide
 
File not formatted according to black style guide
home = get_object_or_404(Home, url_uuid=self.kwargs.get("url_uuid"))
File not formatted according to black style guide
 
File not formatted according to black style guide
# ensure the user can manage the home
File not formatted according to black style guide
if not home.user_can_manage(user=self.request.user):
File not formatted according to black style guide
raise PermissionDenied
File not formatted according to black style guide
 
File not formatted according to black style guide
context["home"] = home
File not formatted according to black style guide
context["home_user_relations"] = HomeUserRelation.objects.filter(home=home)
File not formatted according to black style guide
 
File not formatted according to black style guide
return context
File not formatted according to black style guide
 
File not formatted according to black style guide
def form_valid(self, form):
File not formatted according to black style guide
email = form.cleaned_data["email"]
File not formatted according to black style guide
 
File not formatted according to black style guide
user_exists = user_model.objects.filter(email=email).exists()
File not formatted according to black style guide
 
File not formatted according to black style guide
if not user_exists:
File not formatted according to black style guide
# TODO: Send an invitation email
File not formatted according to black style guide
error_message = _("User does not exist")
File not formatted according to black style guide
form.add_error("email", error_message)
File not formatted according to black style guide
 
File not formatted according to black style guide
return self.form_invalid(form)
File not formatted according to black style guide
 
File not formatted according to black style guide
user = user_model.objects.get(email=email)
File not formatted according to black style guide
 
File not formatted according to black style guide
home = get_object_or_404(Home, url_uuid=self.kwargs.get("url_uuid"))
File not formatted according to black style guide
 
File not formatted according to black style guide
home_user_exists = HomeUserRelation.objects.filter(
File not formatted according to black style guide
home=home,
File not formatted according to black style guide
user=user,
File not formatted according to black style guide
).exists()
File not formatted according to black style guide
 
File not formatted according to black style guide
if home_user_exists:
File not formatted according to black style guide
error_message = _("User is already a caregiver in this home")
File not formatted according to black style guide
form.add_error("email", error_message)
File not formatted according to black style guide
 
File not formatted according to black style guide
return self.form_invalid(form)
File not formatted according to black style guide
 
File not formatted according to black style guide
try:
File not formatted according to black style guide
HomeUserRelation.objects.create(
File not formatted according to black style guide
home=home,
File not formatted according to black style guide
user=user,
File not formatted according to black style guide
)
File not formatted according to black style guide
except Exception:
File not formatted according to black style guide
error_message = _("Something went wrong")
File not formatted according to black style guide
messages.error(self.request, error_message)
File not formatted according to black style guide
 
File not formatted according to black style guide
return self.form_invalid(form)
File not formatted according to black style guide
 
File not formatted according to black style guide
return super().form_valid(form)
File not formatted according to black style guide
 
File not formatted according to black style guide
def get_success_url(self):
File not formatted according to black style guide
"""Redirect to current page after successful form submission."""
File not formatted according to black style guide
# Get the current view name
File not formatted according to black style guide
view_name = self.request.resolver_match.view_name
File not formatted according to black style guide
# Get the current URL parameters
File not formatted according to black style guide
kwargs = self.request.resolver_match.kwargs
File not formatted according to black style guide
# Construct the success URL
File not formatted according to black style guide
success_url = reverse(view_name, kwargs=kwargs)
File not formatted according to black style guide
 
File not formatted according to black style guide
return success_url