blob: ef07c1bd7aae5e81d59c1901232154bc2371495f (
plain) (
tree)
|
|
from typing import Any
from typing import Dict
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db import transaction
from django.utils import timezone
from django.views.generic import DetailView, ListView, CreateView
from .forms import OrganisationCreateForm, AddressInlineForm
from .models import Organisation
class OrganisationCreateWithAddress(CreateView):
model = Organisation
template_name = "organisations/org_create_formset.html"
form_class = OrganisationCreateForm
def get_context_data(self, **kwargs):
data = super().get_context_data(**kwargs)
if self.request.POST:
data["addresses"] = AddressInlineForm(self.request.POST)
else:
data["addresses"] = AddressInlineForm()
return data
def form_valid(self, form):
context = self.get_context_data()
addresses = context["addresses"]
user = self.request.user
with transaction.atomic():
self.object = form.save(user=user)
if addresses.is_valid():
addresses.instance = self.object
addresses.save()
return super(OrganisationCreateWithAddress, self).form_valid(form)
class OrganisationCreate(LoginRequiredMixin, CreateView):
form_class = OrganisationCreateForm
model = Organisation
template_name = "organisations/organisation_create.html"
class OrganisationListView(LoginRequiredMixin, ListView):
model = Organisation
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["organisation_list"] = Organisation.objects.all().order_by("name")
return context
class OrganisationDetailView(LoginRequiredMixin, DetailView):
model = Organisation
def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
context = super().get_context_data()
org = kwargs['object']
no_addr = org.addresses.count()
if no_addr > 1:
context['no_addr'] = no_addr
addr = org.addresses.all()
context['addr'] = addr
else:
context['no_addr'] = 1
addr = org.addresses.first()
context['addr'] = addr
people = org.person_set.all()
context['people'] = people
applicable_systems = org.applicablesystem_set.all()
context['applicable_systems'] = applicable_systems
return context
|