diff options
author | MR Lemon <matt@matthewlemon> | 2020-05-01 20:54:48 +0100 |
---|---|---|
committer | MR Lemon <matt@matthewlemon> | 2020-05-01 20:54:48 +0100 |
commit | fbe7fb2c4da9d990b5f8f77c30a23dd296fac15a (patch) | |
tree | 711abcfe81f25eec7b494fe276ff594c914fe408 /ctrack/organisations/views.py | |
parent | 989b3bf9259b06ed3542d16a81712635ef492c33 (diff) |
some progress but fking slow...
Diffstat (limited to 'ctrack/organisations/views.py')
-rw-r--r-- | ctrack/organisations/views.py | 50 |
1 files changed, 35 insertions, 15 deletions
diff --git a/ctrack/organisations/views.py b/ctrack/organisations/views.py index cad729d..ab090a7 100644 --- a/ctrack/organisations/views.py +++ b/ctrack/organisations/views.py @@ -2,25 +2,45 @@ from typing import Any from typing import Dict from django.contrib.auth.mixins import LoginRequiredMixin -from django.forms import inlineformset_factory -from django.http import HttpResponseRedirect -from django.shortcuts import render +from django.db import transaction from django.views.generic import DetailView, ListView, CreateView -from .forms import OrganisationCreateForm, AddressCreateForm -from .models import Organisation, Address +from .forms import OrganisationCreateForm, AddressInlineForm +from .models import Organisation -def create_org_with_address(request): - OrgCreateInlineFormSet = inlineformset_factory(Organisation, Address, exclude=(), can_delete=False, form=AddressCreateForm, extra=3) - if request.method == "POST": - formset = OrgCreateInlineFormSet(request.POST) - if formset.is_valid(): - formset.save() - return HttpResponseRedirect("/") - else: - formset = OrgCreateInlineFormSet() - return render(request, "organisations/org_create_formset.html", {"formset": formset}) +class OrganisationCreateWithAddress(CreateView): + model = Organisation + template_name = "organisations/org_create_formset.html" + fields = [ + "name", + "submode", + "oes", + "designation_type", + "registered_company_name", + "registered_company_number", + "comments", + "active" + ] + + 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"] + with transaction.atomic(): + self.object = form.save() + + if addresses.is_valid(): + addresses.instance = self.object + addresses.save() + return super(OrganisationCreateWithAddress, self).form_valid(form) class OrganisationCreate(LoginRequiredMixin, CreateView): |