aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/organisations
diff options
context:
space:
mode:
authorMR Lemon <matt@matthewlemon>2020-05-01 20:54:48 +0100
committerMR Lemon <matt@matthewlemon>2020-05-01 20:54:48 +0100
commitfbe7fb2c4da9d990b5f8f77c30a23dd296fac15a (patch)
tree711abcfe81f25eec7b494fe276ff594c914fe408 /ctrack/organisations
parent989b3bf9259b06ed3542d16a81712635ef492c33 (diff)
some progress but fking slow...
Diffstat (limited to 'ctrack/organisations')
-rw-r--r--ctrack/organisations/forms.py7
-rw-r--r--ctrack/organisations/templates/organisations/org_create_formset.html17
-rw-r--r--ctrack/organisations/urls.py4
-rw-r--r--ctrack/organisations/views.py50
4 files changed, 56 insertions, 22 deletions
diff --git a/ctrack/organisations/forms.py b/ctrack/organisations/forms.py
index 8cf9983..2609a6b 100644
--- a/ctrack/organisations/forms.py
+++ b/ctrack/organisations/forms.py
@@ -1,6 +1,7 @@
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Submit, Button, Field
from django import forms
+from django.forms import inlineformset_factory
from django.urls import reverse
from ctrack.organisations.models import Organisation, Address
@@ -49,9 +50,11 @@ class OrganisationCreateForm(forms.ModelForm):
class AddressCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
+ cancel_redirect = reverse("organisations:list")
self.helper = FormHelper(self)
self.helper.layout = Layout(
Fieldset(
+ "Add an address",
"type",
"line1",
"line2",
@@ -78,3 +81,7 @@ class AddressCreateForm(forms.ModelForm):
# address.organisation = self.org
# address.organisation.save()
# return address
+
+
+AddressInlineForm = inlineformset_factory(Organisation, Address, exclude=(), can_delete=False,
+ form=AddressCreateForm, extra=1)
diff --git a/ctrack/organisations/templates/organisations/org_create_formset.html b/ctrack/organisations/templates/organisations/org_create_formset.html
index 4b5c3c2..01f45db 100644
--- a/ctrack/organisations/templates/organisations/org_create_formset.html
+++ b/ctrack/organisations/templates/organisations/org_create_formset.html
@@ -11,11 +11,18 @@
<div class="container">
<div class="row">
<div class="col-md-12">
- <form method="post">
- {{ formset.management_form }}
- {% for form in formset %}
- {% crispy form %}
- {% endfor %}
+ <form method="post" action="">
+ {% csrf_token %}
+ {{ form.as_p }}
+ <table>
+ {{ addresses.management_form }}
+
+ {% for addressform in addresses.forms %}
+ {{ addressform.as_p }}
+ {% endfor %}
+ </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 a4a0244..52e4153 100644
--- a/ctrack/organisations/urls.py
+++ b/ctrack/organisations/urls.py
@@ -1,13 +1,13 @@
from django.urls import path
from ctrack.organisations.views import OrganisationDetailView, OrganisationListView, OrganisationCreate, \
- create_org_with_address
+ OrganisationCreateWithAddress
app_name = "organisations"
urlpatterns = [
path("<slug:slug>/", view=OrganisationDetailView.as_view(), name="detail"),
path("", view=OrganisationListView.as_view(), name="list"),
- path("create", view=create_org_with_address, name="create")
+ path("create", view=OrganisationCreateWithAddress.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 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):