main.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import openerp.http as http
  2. from openerp.http import request
  3. import logging
  4. _logger = logging.getLogger(__name__)
  5. import werkzeug
  6. import base64
  7. from psycopg2 import IntegrityError
  8. class MyController(http.Controller):
  9. @http.route('/form/thankyou', type="http", auth="public", website=True)
  10. def ehtml_thanks(self, **kw):
  11. return http.request.render('entity_html_form.ehtml_thank_you', {})
  12. @http.route('/form/myinsert',type="http", auth="public", website=True)
  13. def my_insert(self, **kwargs):
  14. try:
  15. values = {}
  16. for field_name, field_value in kwargs.items():
  17. values[field_name] = field_value
  18. #the referral string is what the campaign looks for
  19. secure_values = {}
  20. history_values = {}
  21. ref_url = http.request.httprequest.headers['Referer']
  22. entity_form = http.request.env['ehtml.formgen'].sudo().browse(int(values['form_id']))
  23. new_history = http.request.env['ehtml.history'].sudo().create({'ref_url':ref_url, 'html_id': entity_form.id})
  24. #populate an array which has ONLY the fields that are in the form (prevent injection)
  25. for fi in entity_form.fields_ids:
  26. if fi.field_id.ttype == "binary":
  27. secure_values[fi.field_id.name] = base64.encodestring(values[fi.html_name].read() )
  28. else:
  29. secure_values[fi.field_id.name] = values[fi.html_name]
  30. new_history.insert_data.sudo().create({'html_id': new_history.id, 'field_id':fi.field_id.id, 'insert_value':values[fi.html_name]})
  31. #default values
  32. for df in entity_form.defaults_values:
  33. secure_values[df.field_id.name] = df.default_value
  34. new_history.insert_data.sudo().create({'html_id': new_history.id, 'field_id':df.field_id.id, 'insert_value':df.default_value})
  35. new_record = http.request.env[entity_form.model_id.model].sudo().create(secure_values)
  36. new_history.record_id = entity_form.id
  37. return werkzeug.utils.redirect(entity_form.return_url)
  38. except IntegrityError:
  39. # can't use the usual `http.request.env.cr` style,
  40. # because `env` queries db and everything explodes
  41. http.request._cr.rollback()
  42. return http.request.render('entity_html_form.myinsert')