diff options
Diffstat (limited to '')
-rw-r--r-- | ctrack/organisations/forms.py | 35 | ||||
-rw-r--r-- | ctrack/organisations/templates/organisations/org_create_formset.html | 27 | ||||
-rw-r--r-- | ctrack/organisations/urls.py | 6 | ||||
-rw-r--r-- | ctrack/organisations/views.py | 19 | ||||
-rw-r--r-- | requirements/base.txt | 2 |
5 files changed, 75 insertions, 14 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): diff --git a/requirements/base.txt b/requirements/base.txt index 5fea182..6bfe455 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -1,6 +1,6 @@ # Django # ------------------------------------------------------------------------------ -django==3.0 # pyup: < 3.0 # https://www.djangoproject.com/ +django==3.0.5 # pyup: < 3.0 # https://www.djangoproject.com/ django-environ # https://github.com/joke2k/django-environ django-allauth # https://github.com/pennersr/django-allauth django-crispy-forms # https://github.com/django-crispy-forms/django-crispy-forms |