aboutsummaryrefslogtreecommitdiffstats
path: root/ctrack/organisations
diff options
context:
space:
mode:
authorMR Lemon <matt@matthewlemon>2020-05-01 17:30:48 +0100
committerMR Lemon <matt@matthewlemon>2020-05-01 17:31:03 +0100
commit989b3bf9259b06ed3542d16a81712635ef492c33 (patch)
treef3f53e9000712568942e658dfbd889068bed2fd4 /ctrack/organisations
parentdbe4555b17e2dd0d49b8c4879252692f75f8fe42 (diff)
started to put in an inlineformset!
Diffstat (limited to 'ctrack/organisations')
-rw-r--r--ctrack/organisations/forms.py35
-rw-r--r--ctrack/organisations/templates/organisations/org_create_formset.html27
-rw-r--r--ctrack/organisations/urls.py6
-rw-r--r--ctrack/organisations/views.py19
4 files changed, 74 insertions, 13 deletions
diff --git a/ctrack/organisations/forms.py b/ctrack/organisations/forms.py
index d4baf64..8cf9983 100644
--- a/ctrack/organisations/forms.py
+++ b/ctrack/organisations/forms.py
@@ -47,17 +47,34 @@ class OrganisationCreateForm(forms.ModelForm):
class AddressCreateForm(forms.ModelForm):
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ self.helper = FormHelper(self)
+ self.helper.layout = Layout(
+ Fieldset(
+ "type",
+ "line1",
+ "line2",
+ "line3",
+ "city",
+ "county",
+ "postcode",
+ "country",
+ "other_details"
+ )
+ )
+
class Meta:
model = Address
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
+ # 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
diff --git a/ctrack/organisations/templates/organisations/org_create_formset.html b/ctrack/organisations/templates/organisations/org_create_formset.html
new file mode 100644
index 0000000..4b5c3c2
--- /dev/null
+++ b/ctrack/organisations/templates/organisations/org_create_formset.html
@@ -0,0 +1,27 @@
+{% extends "base.html" %}
+
+{% block title %}
+ Create a new Organisation
+{% endblock title %}
+
+{% load crispy_forms_tags %}
+
+{% block content %}
+
+ <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>
+ </div>
+ </div>
+ </div>
+
+{% endblock %}
+
+
+
diff --git a/ctrack/organisations/urls.py b/ctrack/organisations/urls.py
index 8a23cd2..a4a0244 100644
--- a/ctrack/organisations/urls.py
+++ b/ctrack/organisations/urls.py
@@ -1,11 +1,13 @@
from django.urls import path
-from ctrack.organisations.views import OrganisationDetailView, OrganisationListView, OrganisationCreate
+from ctrack.organisations.views import OrganisationDetailView, OrganisationListView, OrganisationCreate, \
+ create_org_with_address
app_name = "organisations"
urlpatterns = [
path("<slug:slug>/", view=OrganisationDetailView.as_view(), name="detail"),
path("", view=OrganisationListView.as_view(), name="list"),
- path("create", view=OrganisationCreate.as_view(), name="create")
+ path("create", view=create_org_with_address, name="create")
+ # path("create", view=OrganisationCreate.as_view(), name="create")
]
diff --git a/ctrack/organisations/views.py b/ctrack/organisations/views.py
index 74b290e..cad729d 100644
--- a/ctrack/organisations/views.py
+++ b/ctrack/organisations/views.py
@@ -2,10 +2,25 @@ 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.views.generic import DetailView, ListView, CreateView
-from .forms import OrganisationCreateForm
-from .models import Organisation
+from .forms import OrganisationCreateForm, AddressCreateForm
+from .models import Organisation, Address
+
+
+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 OrganisationCreate(LoginRequiredMixin, CreateView):