2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-04-29 12:06:44 +00:00

Add generic method for retriving GET or POST params

This commit is contained in:
Oliver Walters 2019-04-28 09:53:42 +10:00
parent e5fc43a00f
commit cd438f0569
3 changed files with 31 additions and 9 deletions

View File

@ -69,6 +69,22 @@ class AjaxMixin(object):
ajax_form_action = '' ajax_form_action = ''
ajax_form_title = '' ajax_form_title = ''
def get_param(self, name, method='GET'):
""" Get a request query parameter value from URL e.g. ?part=3
Args:
name: Variable name e.g. 'part'
method: Request type ('GET' or 'POST')
Returns:
Value of the supplier parameter or None if parameter is not available
"""
if method == 'POST':
return self.request.POST.get(name, None)
else:
return self.request.GET.get(name, None)
def get_data(self): def get_data(self):
""" Get extra context data (default implementation is empty dict) """ Get extra context data (default implementation is empty dict)

View File

@ -92,6 +92,8 @@ class EditBomItemForm(HelperForm):
'quantity', 'quantity',
'note' 'note'
] ]
# Prevent editing of the part associated with this BomItem
widgets = {'part': forms.HiddenInput()} widgets = {'part': forms.HiddenInput()}

View File

@ -406,6 +406,7 @@ class SupplierPartCreate(AjaxCreateView):
ajax_form_title = 'Create new Supplier Part' ajax_form_title = 'Create new Supplier Part'
context_object_name = 'part' context_object_name = 'part'
def get_initial(self): def get_initial(self):
""" Provide initial data for new SupplierPart: """ Provide initial data for new SupplierPart:
@ -414,18 +415,21 @@ class SupplierPartCreate(AjaxCreateView):
""" """
initials = super(SupplierPartCreate, self).get_initial().copy() initials = super(SupplierPartCreate, self).get_initial().copy()
supplier_id = self.request.GET.get('supplier', None) supplier_id = self.get_param('supplier')
part_id = self.request.GET.get('part', None) part_id = self.get_param('part')
if supplier_id: if supplier_id:
initials['supplier'] = get_object_or_404(Company, pk=supplier_id) try:
# TODO initials['supplier'] = Company.objects.get(pk=supplier_id)
# self.fields['supplier'].disabled = True except Company.DoesNotExist:
initials['supplier'] = None
if part_id: if part_id:
initials['part'] = get_object_or_404(Part, pk=part_id) try:
# TODO initials['part'] = Part.objects.get(pk=part_id)
# self.fields['part'].disabled = True except Part.DoesNotExist:
initials['part'] = None
return initials return initials