aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMR Lemon <matt@matthewlemon>2020-05-02 15:34:57 +0100
committerMR Lemon <matt@matthewlemon>2020-05-02 15:34:57 +0100
commitbf044aedde89574fffdc052f66b845b9bdb75b51 (patch)
tree90fc062720eccc691bb16adde54e3a8efe7bd99a
parent51797f3868ad9fe2a0ff59a6732ed54d377cc5a4 (diff)
got a rough inlineformset with no crispy forms - but working
Diffstat (limited to '')
-rw-r--r--ctrack/organisations/forms.py22
-rw-r--r--ctrack/organisations/templates/organisations/org_create_formset.html8
-rw-r--r--ctrack/organisations/urls.py5
-rw-r--r--ctrack/organisations/views.py28
4 files changed, 20 insertions, 43 deletions
diff --git a/ctrack/organisations/forms.py b/ctrack/organisations/forms.py
index d60172a..ac96bcb 100644
--- a/ctrack/organisations/forms.py
+++ b/ctrack/organisations/forms.py
@@ -44,12 +44,6 @@ class OrganisationCreateForm(forms.ModelForm):
"designation_type": "This is probably defined in the Reguation",
}
- def save(self, **kwargs):
- org = super().save(commit=False)
- org.updated_by = kwargs["user"]
- org.save()
- return org
-
class AddressCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
@@ -76,16 +70,8 @@ class AddressCreateForm(forms.ModelForm):
fields = ('type', 'line1', 'line2', 'line3', 'city', 'county', 'postcode',
'country', 'other_details')
- # def __init__(self, *args, **kwargs):
- # self.org = kwargs.pop("org")
- # super().__init__(*args, **kwargs)
- #
- # def save(self):
- # address = super().save(commit=False)
- # address.organisation = self.org
- # address.organisation.save()
- # return address
-
-AddressInlineForm = inlineformset_factory(Organisation, Address, exclude=(), can_delete=False,
- form=AddressCreateForm, extra=1)
+AddressInlineFormSet = inlineformset_factory(Organisation, Address,
+ fields=("type", "line1", "line2", "line3", "city",
+ "county", "postcode", "country", "other_details"),
+ form=AddressCreateForm)
diff --git a/ctrack/organisations/templates/organisations/org_create_formset.html b/ctrack/organisations/templates/organisations/org_create_formset.html
index 01f45db..8b57bc9 100644
--- a/ctrack/organisations/templates/organisations/org_create_formset.html
+++ b/ctrack/organisations/templates/organisations/org_create_formset.html
@@ -13,16 +13,12 @@
<div class="col-md-12">
<form method="post" action="">
{% csrf_token %}
- {{ form.as_p }}
+ {{ form }}
<table>
{{ addresses.management_form }}
-
- {% for addressform in addresses.forms %}
- {{ addressform.as_p }}
- {% endfor %}
+ {{ addresses }}
</table>
<input type="submit" value="SUBMIT">
-{# <input class="btn btn-primary" type="button" value="Save">#}
</form>
</div>
</div>
diff --git a/ctrack/organisations/urls.py b/ctrack/organisations/urls.py
index 52e4153..09743af 100644
--- a/ctrack/organisations/urls.py
+++ b/ctrack/organisations/urls.py
@@ -1,13 +1,12 @@
from django.urls import path
-from ctrack.organisations.views import OrganisationDetailView, OrganisationListView, OrganisationCreate, \
- OrganisationCreateWithAddress
+from ctrack.organisations.views import OrganisationDetailView, OrganisationListView, OrganisationCreate
app_name = "organisations"
urlpatterns = [
path("<slug:slug>/", view=OrganisationDetailView.as_view(), name="detail"),
path("", view=OrganisationListView.as_view(), name="list"),
- path("create", view=OrganisationCreateWithAddress.as_view(), name="create")
+ path("create", view=OrganisationCreate.as_view(), name="create")
# path("create", view=OrganisationCreate.as_view(), name="create")
]
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):