From bf044aedde89574fffdc052f66b845b9bdb75b51 Mon Sep 17 00:00:00 2001 From: MR Lemon Date: Sat, 2 May 2020 15:34:57 +0100 Subject: got a rough inlineformset with no crispy forms - but working --- ctrack/organisations/views.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) (limited to 'ctrack/organisations/views.py') 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): -- cgit v1.2.3