Scenario:
You have two modes of form submit,
1
one is the 'real' submit, where you submit everything to be written to the db
-- triggered on the submit button.
2
the other is the 'fake submit', which a javascript adds a hidden field into the form,
if you find that, you don't write to db,
but you re-construct the form (call form constructor!) clobber some of the modelfields with your own values.
To do that, construct a dict from the form.cleaned_data, change some of the values thus
form.cleaned_data['somekey'] = "data queried from fi language Property table"
and call the form constructor with these arguments:
form = SomeForm(form.cleaned_data, instance=self.product)
Then, at the end of the def request() method,
it falls out to the API,
which returns the current template
(i.e. that is to say, don't return redirect(...), if you want to show the newly populated form again, at the same template.)
##################################################
def request(self, request, *args, **kwargs):
def populate_form(form_info):
form_name = form_info.name
form_cls = form_info.cls
form = self._create_form(form_info)
setattr(self, form_name, form)
method_name = 'populate_' + form_name
try:
self.call_method(method_name, form)
except CalledMethodNotFound:
request_logger.debug('Not calling {0}, method not found'.format(method_name))
self.request = request
self.view_args = args
self.view_kwargs = kwargs
data = getattr(request, request.method)
self.form_name = data.get('frm')
self.button_name = data.get('btn')
submitted_form_info = None
self.build_breadcrumbs() #TODO: SLOTTED IN THIS HERE.
## if request.method == 'POST':
## self.product_edit_form = ProductEditForm(request.POST)
## if self.product_edit_form.is_valid():
## print "ok"
## # Process the data in form.cleaned_data
## # ...
## #return self.redirect_product_edit(self.operator_id, self.product_id)
## else:
## self.product_edit_form = ProductEditForm(self.get_submit_data(), instance=self.product)
## print "pk"
##
## #return self.redirect_product_edit(self.operator_id, self.product_id)
# for forms in collection
# form_name = "product_edit_form"
# self.form_name = data.get('frm')
# if form_name = submitted_data("frm") # if this was the form that was submitteed
## Just create the fucking form with the fucking name, regardless of request.POST or not.
## do nothing else.
## self.product_edit_form = ProductEditForm(self.get_submit_data(), instance=self.product) #OK
##EXAMPLE---
if request.method == 'POST':
self.product_edit_form = ProductEditForm(self.get_submit_data(), instance=self.product)
self.create_product_edit_form()
if not data.get('hidden_lang_change'): # A 'REAL' submit
if self.product_edit_form.is_valid():
print "asdf", self.product_edit_form.cleaned_data
#TEST: A 'language change submit': repopulate some fields from the Property table AFTER creating the form
self.product_edit_form.fields['description'].help_text = "AFAVSVAAAS"
#self.product_edit_form.fields['description'].initial = "---AFAVSVAAAS---"
self.product_edit_form.cleaned_data['description'] = "NEW DESC"
self.product_edit_form = ProductEditForm(self.product_edit_form.cleaned_data, instance=self.product)
print "lalalalaa"
else:
self.product_edit_form.fields['description'].help_text = "zzzzzzzzzZZZZZZ"
print "efgh"
else: # A 'language change submit': repopulate some fields from the Property table AFTER creating the form
pass
print "submitted"
# if errors, you end up at the same form here again. With "this field is required" error.
# if no errors, you also end up submitted here unless you "return redirect ..." -- but with no error messages on the form!
else:
self.create_product_edit_form()
print "new form"
## for form_info in self.form_infos:
## form_name = form_info.name
##
## if form_name == self.form_name:
## submitted_form_info = form_info
## if form_info.readonly:
## populate_form(form_info)
## else:
## populate_form(form_info)
##
## if self.form_name:
## if not submitted_form_info:
## raise ValueError('Invalid form {0}'.format(self.form_name))
##
## if not submitted_form_info.readonly:
## response = self._submit_form(submitted_form_info)
## if response:
## return response
## else:
## audit_logger.info('%s', self.__class__.__name__)
def create_product_edit_form(self):
self.product_edit_form = ProductEditForm(self.get_submit_data(), instance=self.product)
self.product_edit_form.fields['lang'].choices = [
(code, get_localized_desc(code)) for code in LANGUAGE_CODES
]
self.product_edit_form.fields['lang'].help_text = '(Select language to refresh fields to the new language)'
No comments:
Post a Comment