Customize "CompanyCreate" form based on calling url

This commit is contained in:
Oliver Walters 2020-04-13 13:09:23 +10:00
parent 9e9e29679d
commit 8adc83b650
4 changed files with 92 additions and 30 deletions

View File

@ -115,9 +115,12 @@ class AjaxMixin(object):
# (this can be overridden by a child class) # (this can be overridden by a child class)
ajax_template_name = 'modal_form.html' ajax_template_name = 'modal_form.html'
ajax_form_action = ''
ajax_form_title = '' ajax_form_title = ''
def get_form_title(self):
""" Default implementation - return the ajax_form_title variable """
return self.ajax_form_title
def get_param(self, name, method='GET'): def get_param(self, name, method='GET'):
""" Get a request query parameter value from URL e.g. ?part=3 """ Get a request query parameter value from URL e.g. ?part=3
@ -169,7 +172,7 @@ class AjaxMixin(object):
else: else:
context['form'] = None context['form'] = None
data['title'] = self.ajax_form_title data['title'] = self.get_form_title()
data['html_form'] = render_to_string( data['html_form'] = render_to_string(
self.ajax_template_name, self.ajax_template_name,

View File

@ -14,7 +14,7 @@ InvenTree | {% trans "Supplier List" %}
<div id='button-toolbar'> <div id='button-toolbar'>
<div class='btn-group'> <div class='btn-group'>
<button type='button' class="btn btn-success" id='new-company'>{{ new_button_text }}</button> <button type='button' class="btn btn-success" id='new-company'>{{ button_text }}</button>
</div> </div>
</div> </div>
@ -26,9 +26,7 @@ InvenTree | {% trans "Supplier List" %}
{% block js_ready %} {% block js_ready %}
{{ block.super }} {{ block.super }}
$('#new-company').click(function () { $('#new-company').click(function () {
launchModalForm( launchModalForm("{{ create_url }}", {
"{% url 'company-create' %}",
{
follow: true follow: true
}); });
}); });

View File

@ -29,6 +29,9 @@ company_detail_urls = [
company_urls = [ company_urls = [
url(r'new/supplier/', views.CompanyCreate.as_view(), name='supplier-create'),
url(r'new/manufacturer/', views.CompanyCreate.as_view(), name='manufacturer-create'),
url(r'new/customer/', views.CompanyCreate.as_view(), name='customer-create'),
url(r'new/?', views.CompanyCreate.as_view(), name='company-create'), url(r'new/?', views.CompanyCreate.as_view(), name='company-create'),
url(r'^(?P<pk>\d+)/', include(company_detail_urls)), url(r'^(?P<pk>\d+)/', include(company_detail_urls)),

View File

@ -43,29 +43,50 @@ class CompanyIndex(ListView):
ctx = super().get_context_data(**kwargs) ctx = super().get_context_data(**kwargs)
url = self.request.path # Provide custom context data to the template,
# based on the URL we use to access this page
print(url) lookup = {
print(reverse('supplier-index')) reverse('supplier-index'): {
print(reverse('manufacturer-index')) 'title': _('Suppliers'),
print(reverse('customer-index')) 'button_text': _('New Supplier'),
'filters': {'is_supplier': 'true'},
'create_url': reverse('supplier-create'),
},
reverse('manufacturer-index'): {
'title': _('Manufacturers'),
'button_text': _('New Manufacturer'),
'filters': {'is_manufacturer': 'true'},
'create_url': reverse('manufacturer-create'),
},
reverse('customer-index'): {
'title': _('Customers'),
'button_text': _('New Customer'),
'filters': {'is_customer': 'true'},
'create_url': reverse('customer-create'),
}
}
if url == reverse('supplier-index'): default = {
ctx["title"] = _("Suppliers") 'title': _('Companies'),
ctx['new_button_text'] = _("New Supplier") 'button_text': _('New Company'),
ctx["filters"] = {"is_supplier": "true"} 'filters': {},
elif url == reverse('manufacturer-index'): 'create_url': reverse('company-create'),
ctx["title"] = _("Manufacturers") }
ctx['new_button_text'] = _("New Manufacturer")
ctx["filters"] = {"is_manufacturer": "true"} context = None
elif url == reverse('customer-index'):
ctx["title"] = _("Customers") for item in lookup:
ctx['new_button_text'] = _("New Customer") print(self.request.path, item)
ctx["filters"] = {"is_customer": "true"} if self.request.path == item:
else: context = lookup[item]
ctx["title"] = _("Companies") break
ctx["new_button_text"] = _("New Company")
ctx["filters"] = {} if context is None:
context = default
for key,value in context.items():
ctx[key] = value
return ctx return ctx
@ -156,7 +177,44 @@ class CompanyCreate(AjaxCreateView):
context_object_name = 'company' context_object_name = 'company'
form_class = EditCompanyForm form_class = EditCompanyForm
ajax_template_name = 'modal_form.html' ajax_template_name = 'modal_form.html'
ajax_form_title = _("Create new Company")
def get_form_title(self):
url = self.request.path
if url == reverse('supplier-create'):
return _("Create new Supplier")
if url == reverse('manufacturer-create'):
return _('Create new Manufacturer')
if url == reverse('customer-create'):
return _('Create new Customer')
return _('Create new Company')
def get_initial(self):
""" Initial values for the form data """
initials = super().get_initial().copy()
url = self.request.path
if url == reverse('supplier-create'):
initials['is_supplier'] = True
initials['is_customer'] = False
initials['is_manufacturer'] = False
elif url == reverse('manufacturer-create'):
initials['is_manufacturer'] = True
initials['is_supplier'] = True
initials['is_customer'] = False
elif url == reverse('customer-create'):
initials['is_customer'] = True
initials['is_manufacturer'] = False
initials['is_supplier'] = False
return initials
def get_data(self): def get_data(self):
return { return {