entity_html_form.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. from openerp import models, fields, api
  2. from openerp.http import request
  3. import logging
  4. _logger = logging.getLogger(__name__)
  5. import cgi
  6. class EhtmlFormgen(models.Model):
  7. _name = "ehtml.formgen"
  8. def _default_return_url(self):
  9. return request.httprequest.host_url + "form/thankyou"
  10. name = fields.Char(string="Form Name", required=True)
  11. model_id = fields.Many2one('ir.model', string="Model", required=True)
  12. fields_ids = fields.One2many('ehtml.fieldentry', 'html_id', string="HTML Fields")
  13. output_html = fields.Text(string='Embed Code', readonly=True)
  14. required_fields = fields.Text(readonly=True, string="Required Fields")
  15. defaults_values = fields.One2many('ehtml.fielddefault', 'html_id', string="Default Values", help="Sets the value of an field before it gets inserted into the database")
  16. return_url = fields.Char(string="Return URL", default=_default_return_url, help="The URL that the user will be redirected to after submitting the form", required=True)
  17. form_type = fields.Selection([('reg','Plain'),('odoo','Odoo Website')], default="odoo", string="Form Type")
  18. @api.onchange('model_id')
  19. @api.one
  20. def change_model(self):
  21. #delete all existing fields
  22. for field_entry in self.fields_ids:
  23. field_entry.unlink()
  24. required_string = ""
  25. for model_field in self.env['ir.model.fields'].search([('model_id','=', self.model_id.id),('required','=',True) ]):
  26. required_string += model_field.field_description.encode("utf-8") + " (" + model_field.name.encode("utf-8") + ")\n"
  27. self.required_fields = required_string
  28. @api.one
  29. def generate_form(self):
  30. if self.form_type == 'reg':
  31. self.generate_form_reg()
  32. elif self.form_type == 'odoo':
  33. self.generate_form_odoo()
  34. else:
  35. self.generate_form_optimize()
  36. @api.one
  37. def generate_form_reg(self):
  38. html_output = ""
  39. html_output += "<link href=\"http://code.jquery.com/ui/1.10.4/themes/ui-lightness/jquery-ui.css\" rel=\"stylesheet\">\n";
  40. html_output += "<script src=\"http://code.jquery.com/jquery-1.10.2.js\"></script>\n";
  41. html_output += "<script src=\"http://code.jquery.com/ui/1.10.4/jquery-ui.js\"></script>\n";
  42. html_output += '<div id="ehtml_form">' + "\n"
  43. html_output += '<form method="POST" action="' + request.httprequest.host_url + 'form/myinsert">' + "\n"
  44. for fe in self.fields_ids:
  45. html_output += '<label for="' + fe.html_name + '">' + fe.field_id.field_description
  46. if fe.field_id.required == True:
  47. html_output += ' *'
  48. html_output += '</label><br/>\n'
  49. if fe.html_field_type == "text":
  50. html_output += '<input type="text" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  51. if fe.field_id.size > 0:
  52. html_output += ' maxlength="' + fe.field_id.size + '"'
  53. if fe.field_id.required == True:
  54. html_output += ' required'
  55. html_output += '/><br>\n'
  56. if fe.html_field_type == "textarea":
  57. html_output += '<textarea id="' + fe.html_name + '" name="' + fe.html_name + '"'
  58. if fe.field_id.required == True:
  59. html_output += ' required'
  60. html_output += '></textarea><br>\n'
  61. if fe.html_field_type == "number":
  62. html_output += '<input type="number" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  63. if fe.field_id.required == True:
  64. html_output += ' required'
  65. html_output += '/><br>\n'
  66. if fe.html_field_type == "search":
  67. html_output += "<script>\n"
  68. html_output += "$(document).ready(function() {\n"
  69. html_output += ' $("#' + fe.html_name + '").autocomplete({' + "\n"
  70. html_output += " source: function( request, response ) {\n"
  71. html_output += ' $.ajax({url: "' + request.httprequest.host_url + 'form/autocomplete?callback=?",dataType: "jsonp",' + "\n"
  72. html_output += ' data: {'+ "\n"
  73. html_output += " q: request.term\n"
  74. html_output += " },\n"
  75. html_output += " success: function( data ) {\n"
  76. html_output += " response( data );\n"
  77. html_output += " }});\n"
  78. html_output += " }\n"
  79. html_output += " });\n"
  80. html_output += "});\n"
  81. html_output += "</script>\n"
  82. html_output += '<input type="search" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  83. if fe.field_id.required == True:
  84. html_output += ' required'
  85. html_output += '/><br>\n'
  86. html_output += "<br>\n"
  87. html_output += '<input type="hidden" name="form_id" value="' + str(self.id) + '"/>' + "\n"
  88. html_output += '<input type="submit" value="Submit Form"/>' + "\n"
  89. html_output += "</form>\n"
  90. html_output += "</div>"
  91. self.output_html = html_output
  92. @api.one
  93. def generate_form_odoo(self):
  94. html_output = ""
  95. html_output += "<section id=\"ehtml_form\" class=\"oe_snippet_body ehtml_form container\">\n"
  96. html_output += ' <form method="POST" action="' + request.httprequest.host_url + 'form/myinsert" enctype=\"multipart/form-data\">' + "\n"
  97. html_output += " <h1>" + self.name.encode("utf-8") + "</h1>\n"
  98. html_output += " <div id=\"ehtml_fields\" class=\"oe_structure\">\n"
  99. for fe in self.fields_ids:
  100. html_output += " <section class=\"oe_snippet_body ehtml_form_field\">\n"
  101. if fe.html_field_type == "text":
  102. html_output += " <div class=\"form-group\">\n"
  103. html_output += " <label class=\"control-label\" for=\"" + fe.html_name + "\""
  104. if fe.field_id.required == False:
  105. html_output += ' style="font-weight: normal"'
  106. html_output += ">" + fe.field_label + "</label>\n"
  107. html_output += ' <input type="text" class="form-control" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  108. if fe.field_id.size > 0:
  109. html_output += ' maxlength="' + fe.field_id.size + '"'
  110. if fe.field_id.required == True:
  111. html_output += ' required="required"'
  112. html_output += "/>\n"
  113. html_output += " </div>\n"
  114. if fe.html_field_type == "textarea":
  115. html_output += " <div class=\"form-group\">\n"
  116. html_output += " <label class=\"control-label\" for=\"" + fe.html_name + "\""
  117. if fe.field_id.required == False:
  118. html_output += ' style="font-weight: normal"'
  119. html_output += ">" + fe.field_label + "</label>\n"
  120. html_output += ' <textarea class="form-control" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  121. if fe.field_id.required == True:
  122. html_output += ' required'
  123. html_output += "></textarea>\n"
  124. html_output += " </div>\n"
  125. if fe.html_field_type == "binary":
  126. html_output += " <div class=\"form-group\">\n"
  127. html_output += " <label class=\"control-label\" for=\"" + fe.html_name + "\""
  128. if fe.field_id.required == False:
  129. html_output += ' style="font-weight: normal"'
  130. html_output += ">" + fe.field_label + "</label>\n"
  131. html_output += ' <input type="file" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  132. if fe.field_id.required == True:
  133. html_output += ' required'
  134. html_output += "/>\n"
  135. html_output += " </div>\n"
  136. if fe.html_field_type == "checkbox":
  137. html_output += " <div class=\"checkbox\">\n"
  138. html_output += " <label class=\"control-label\""
  139. if fe.field_id.required == False:
  140. html_output += ' style="font-weight: normal"'
  141. html_output += ">\n"
  142. html_output += ' <input type="checkbox" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  143. if fe.field_id.required == True:
  144. html_output += ' required'
  145. html_output += '/>' + fe.field_label + "\n"
  146. html_output += " </label>\n"
  147. html_output += " </div>\n"
  148. if fe.html_field_type == "number":
  149. html_output += " <div class=\"form-group\">\n"
  150. html_output += " <label class=\"control-label\" for=\"" + fe.html_name + "\""
  151. if fe.field_id.required == False:
  152. html_output += ' style="font-weight: normal"'
  153. html_output += ">" + fe.field_label + "</label>\n"
  154. html_output += ' <input type="number" class="form-control" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  155. if fe.field_id.required == True:
  156. html_output += ' required'
  157. html_output += "/>\n"
  158. html_output += " </div>\n"
  159. if fe.html_field_type == "selection":
  160. html_output += " <div class=\"form-group\">\n"
  161. html_output += " <label class=\"control-label\" for=\"" + fe.html_name + "\""
  162. if fe.field_id.required == False:
  163. html_output += ' style="font-weight: normal"'
  164. html_output += ">" + fe.field_label
  165. html_output += "</label>\n"
  166. html_output += ' <select class="form-control" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  167. if fe.field_id.required == True:
  168. html_output += ' required'
  169. html_output += ">\n"
  170. html_output += " <option value=\"\">Select Option</option>\n"
  171. selection_list = dict(self.env[self.model_id.model]._columns[fe.field_id.name].selection)
  172. for selection_value,selection_label in selection_list.items():
  173. html_output += " <option value=\"" + str(selection_value) + "\">" + str(selection_label) + "</option>\n"
  174. html_output += " </select>\n"
  175. html_output += " </div>\n"
  176. if fe.html_field_type == "radiobuttons":
  177. html_output += " <label class=\"control-label\""
  178. if fe.field_id.required == False:
  179. html_output += ' style="font-weight: normal"'
  180. html_output += ">" + fe.field_label + "</label>\n"
  181. selection_list = dict(self.env[self.model_id.model]._columns[fe.field_id.name].selection)
  182. for selection_value,selection_label in selection_list.items():
  183. html_output += " <div class=\"radio\">\n"
  184. html_output += " <label><input type=\"radio\" name=\"" + fe.html_name + "\" value=\"" + str(selection_value) + "\""
  185. if fe.field_id.required == True:
  186. html_output += ' required'
  187. html_output += "/>" + str(selection_label) + "</label>\n"
  188. html_output += " </div>\n"
  189. if fe.html_field_type == "dropdownstatic":
  190. html_output += " <div class=\"form-group\">\n"
  191. html_output += " <label class=\"control-label\" for=\"" + fe.html_name + "\""
  192. if fe.field_id.required == False:
  193. html_output += ' style="font-weight: normal"'
  194. html_output += ">" + fe.field_label
  195. html_output += "</label>\n"
  196. html_output += ' <select class="form-control" id="' + fe.html_name + '" name="' + fe.html_name + '"'
  197. if fe.field_id.required == True:
  198. html_output += ' required'
  199. html_output += ">\n"
  200. for reco in self.env[str(fe.field_id.relation)].search([]):
  201. html_output += " <option value=\"" + str(reco.id) + "\">" + unicode(cgi.escape(reco.name)) + "</option>\n"
  202. html_output += " </select>\n"
  203. html_output += " </div>\n"
  204. html_output += " </section>\n"
  205. html_output += ' <input type="hidden" name="form_id" value="' + str(self.id) + '"/>' + "\n"
  206. html_output += " <input type=\"submit\" class=\"btn btn-primary btn-lg\" value=\"Send\"/>\n"
  207. html_output += " </div>\n"
  208. html_output += " </form>\n"
  209. html_output += "</section>\n"
  210. self.output_html = html_output
  211. class EhtmlFieldEntry(models.Model):
  212. _name = "ehtml.fieldentry"
  213. _order = "sequence asc"
  214. sequence = fields.Integer(string="Sequence")
  215. html_id = fields.Many2one('ehtml.formgen', string="HTML Form")
  216. model_id = fields.Many2one('ir.model', string="Model", required=True)
  217. model = fields.Char(related="model_id.model", string="Related Model")
  218. field_id = fields.Many2one('ir.model.fields', domain="[('name','!=','create_date'),('name','!=','create_uid'),('name','!=','id'),('name','!=','write_date'),('name','!=','write_uid')]", string="Form Field")
  219. field_label = fields.Char(string="Field Label")
  220. html_name = fields.Char(string="HTML Field Name")
  221. html_field_type = fields.Selection((('binary','Binary'),('text','Textbox'),('textarea','Textarea'),('number','Number'),('selection','Selection'),('radiobuttons','Radiobuttons'),('checkbox','Checkbox'),('search','Search'),('dropdownstatic','Dropdown(static)') ), string="HTML Field Type")
  222. @api.model
  223. def create(self, values):
  224. sequence=self.env['ir.sequence'].get('sequence')
  225. values['sequence']=sequence
  226. return super(EhtmlFieldEntry, self).create(values)
  227. @api.onchange('field_id')
  228. def update_html_name(self):
  229. self.html_name = self.field_id.name
  230. self.field_label = self.field_id.field_description
  231. if (self.field_id.ttype == "binary"):
  232. self.html_field_type = "binary"
  233. if (self.field_id.ttype == "boolean"):
  234. self.html_field_type = "checkbox"
  235. if (self.field_id.ttype == "selection"):
  236. self.html_field_type = "selection"
  237. if (self.field_id.ttype == "char"):
  238. self.html_field_type = "text"
  239. if (self.field_id.ttype == "text"):
  240. self.html_field_type = "textarea"
  241. if (self.field_id.ttype == "integer"):
  242. self.html_field_type = "number"
  243. if (self.field_id.ttype == "many2one"):
  244. self.html_field_type = "dropdownstatic"
  245. class EhtmlFieldDefault(models.Model):
  246. _name = "ehtml.fielddefault"
  247. html_id = fields.Many2one('ehtml.formgen', string="HTML Form")
  248. model_id = fields.Many2one('ir.model', string="Model", required=True)
  249. model = fields.Char(related="model_id.model", string="Model Name")
  250. field_id = fields.Many2one('ir.model.fields', string="Form Fields")
  251. default_value = fields.Char(string="Default Value")
  252. class EhtmlHistory(models.Model):
  253. _name = "ehtml.history"
  254. html_id = fields.Many2one('ehtml.formgen', string="HTML Form", readonly=True)
  255. ref_url = fields.Char(string="Reference URL", readonly=True)
  256. record_id = fields.Integer(string="Record ID", readonly=True)
  257. form_name = fields.Char(string="Form Name", related="html_id.name")
  258. insert_data = fields.One2many('ehtml.fieldinsert', 'html_id', string="HTML Fields", readonly=True)
  259. class EhtmlFieldInsert(models.Model):
  260. _name = "ehtml.fieldinsert"
  261. html_id = fields.Many2one('ehtml.history')
  262. field_id = fields.Many2one('ir.model.fields', string="Field")
  263. insert_value = fields.Char(string="Insert Value")