Advertisement
Guest User

Untitled

a guest
Feb 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. def edit(request,company_uuid,contact_uuid,address_uuid):
  2. mcompany = get_object_or_404(models.Company, uuid = company_uuid)
  3. extra_questions, extra_ids = get_questions(request, 2)
  4. if request.method == 'POST': # If the form has been submitted...
  5. form = forms.EditCompanyForm(request.POST,instance=mcompany, extra=extra_questions, extra_ids=extra_ids)
  6. if form.is_valid():
  7. fModel = form.save(commit = False)
  8. for (question, answer) in form.extra_answers():
  9. save_answer(request, question, answer, mcompany.uuid, 2)
  10. form.save()
  11. url = reverse('contacts_details',kwargs={'company_uuid':mcompany.uuid,
  12. 'address_uuid':address_uuid,
  13. 'contact_uuid':contact_uuid})
  14. return HttpResponseRedirect(url)
  15. else:
  16. form = forms.EditCompanyForm(request.POST,instance=mcompany, extra=extra_questions, extra_ids=extra_ids)
  17. return share.output_page(request,'contacts/edit.html',{'form': form,
  18. 'company_uuid':mcompany.uuid,
  19. 'address_uuid':address_uuid,
  20. 'contact_uuid':contact_uuid})
  21.  
  22. class EditCompanyForm(jsforms.JMSModelForm):
  23. """
  24. The Company form with the contacts fields included
  25. """
  26. name = forms.CharField(label = _('Company Name'),widget = forms.TextInput(attrs={'class':'large-input-box'}))
  27. discount=forms.FloatField(widget = jsforms.StandardUnit("PC"),required=False)
  28. allow_download = forms.BooleanField(required=False,label=_('Allow download'))
  29.  
  30. def __init__(self, *args, **kwargs):
  31.  
  32. extra = kwargs.pop('extra')
  33. extra_id = kwargs.pop('extra_ids')
  34. super(EditCompanyForm, self).__init__(*args, **kwargs)
  35.  
  36. for i, question in enumerate(extra):
  37. settings = ExtraFieldSetup.objects.get(uuid=extra_id[i])
  38. if settings.type == 2:
  39. list = []
  40. list_partial = str(settings.extra).split(':')
  41. for choice in list_partial:
  42. x = choice, choice
  43. list.append(x)
  44. self.fields['custom_%s' % i] = forms.ChoiceField(choices=list, label=question, required=False)
  45. else:
  46. self.fields['custom_%s' % i] = forms.CharField(label=question, required=False)
  47.  
  48. def extra_answers(self):
  49. for name, value in self.cleaned_data.items():
  50. if name.startswith('custom_'):
  51. yield (self.fields[name].label, value)
  52.  
  53. class Meta:
  54. model = models.Company
  55. exclude = ('company', 'otherdetails', 'jms_code', 'logo', 'hidden', 'user')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement