homes/views.py
File not formatted according to black style guidefrom typing import AnyFile not formatted according to black style guide File not formatted according to black style guidefrom django.contrib import messagesFile not formatted according to black style guidefrom django.contrib.auth import get_user_modelFile not formatted according to black style guidefrom django.contrib.auth.mixins import LoginRequiredMixinFile not formatted according to black style guidefrom django.core.exceptions import PermissionDeniedFile not formatted according to black style guidefrom django.shortcuts import get_object_or_404File not formatted according to black style guidefrom django.urls import reverseFile not formatted according to black style guide File not formatted according to black style guidefrom django.utils.translation import gettext as _File not formatted according to black style guidefrom django.views.generic.edit import FormViewFile not formatted according to black style guidefrom django.views.generic.detail import DetailViewFile not formatted according to black style guidefrom django.views.generic.base import TemplateViewFile not formatted according to black style guide File not formatted according to black style guidefrom homes.forms import AddCaregiverFormFile not formatted according to black style guide File not formatted according to black style guidefrom .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 guidefrom .models import Home, HomeUserRelationFile not formatted according to black style guide File not formatted according to black style guideuser_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 guidedef regroup_homes_by_home_group(homes):File not formatted according to black style guide # group homes with group by group nameFile 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 tuplesFile not formatted according to black style guide # to make it easier to iterate over in the templateFile 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_homesFile not formatted according to black style guide File not formatted according to black style guide File not formatted according to black style guideclass 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.userFile 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 contextFile 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_homesFile not formatted according to black style guide File not formatted according to black style guide return contextFile 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 inFile not formatted according to black style guide File not formatted according to black style guide File not formatted according to black style guideclass HomeDetailView(LoginRequiredMixin, DetailView):File not formatted according to black style guide model = HomeFile 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 URLFile 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_uuidFile 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 foundFile not formatted according to black style guide File not formatted according to black style guide # ensure the user has access to the homeFile 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 PermissionDeniedFile not formatted according to black style guide File not formatted according to black style guide return homeFile 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 contextFile 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 contextFile 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 homeFile 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 recordedFile not formatted according to black style guide # by selecting one recordFile 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 recordedFile 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 contextFile not formatted according to black style guide File not formatted according to black style guide File not formatted according to black style guideclass HomeUserRelationListView(LoginRequiredMixin, FormView):File not formatted according to black style guide form_class = AddCaregiverForm # Use form_class instead of formFile 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 homeFile 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 PermissionDeniedFile not formatted according to black style guide File not formatted according to black style guide context["home"] = homeFile 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 contextFile 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 emailFile 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 nameFile not formatted according to black style guide view_name = self.request.resolver_match.view_nameFile not formatted according to black style guide # Get the current URL parametersFile not formatted according to black style guide kwargs = self.request.resolver_match.kwargsFile not formatted according to black style guide # Construct the success URLFile 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