diff options
author | MR Lemon <matt@matthewlemon> | 2020-05-02 15:34:57 +0100 |
---|---|---|
committer | MR Lemon <matt@matthewlemon> | 2020-05-02 15:34:57 +0100 |
commit | bf044aedde89574fffdc052f66b845b9bdb75b51 (patch) | |
tree | 90fc062720eccc691bb16adde54e3a8efe7bd99a /ctrack/organisations/views.py | |
parent | 51797f3868ad9fe2a0ff59a6732ed54d377cc5a4 (diff) |
got a rough inlineformset with no crispy forms - but working
Diffstat (limited to 'ctrack/organisations/views.py')
-rw-r--r-- | ctrack/organisations/views.py | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/ctrack/organisations/views.py b/ctrack/organisations/views.py index ef07c1b..427d385 100644 --- a/ctrack/organisations/views.py +++ b/ctrack/organisations/views.py @@ -3,43 +3,39 @@ from typing import Dict from django.contrib.auth.mixins import LoginRequiredMixin from django.db import transaction -from django.utils import timezone +from django.urls import reverse_lazy from django.views.generic import DetailView, ListView, CreateView -from .forms import OrganisationCreateForm, AddressInlineForm +from .forms import OrganisationCreateForm, AddressInlineFormSet from .models import Organisation -class OrganisationCreateWithAddress(CreateView): +class OrganisationCreate(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) + context = super().get_context_data(**kwargs) if self.request.POST: - data["addresses"] = AddressInlineForm(self.request.POST) + context["addresses"] = AddressInlineFormSet(self.request.POST) else: - data["addresses"] = AddressInlineForm() - return data + context["addresses"] = AddressInlineFormSet() + return context 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) - + form.instance.updated_by = self.request.user + self.object = form.save() if addresses.is_valid(): addresses.instance = self.object addresses.save() - return super(OrganisationCreateWithAddress, self).form_valid(form) + return super().form_valid(form) - -class OrganisationCreate(LoginRequiredMixin, CreateView): - form_class = OrganisationCreateForm - model = Organisation - template_name = "organisations/organisation_create.html" + def get_success_url(self) -> str: + return reverse_lazy("organisations:detail", kwargs={"slug": self.object.slug}) class OrganisationListView(LoginRequiredMixin, ListView): |