[IMP] website_form: various improvements

- Fixed the module name and category in manifest
- Fixed website_form_blacklisted being ignored
- Unauthorized readonly and magic fields
- Reset the form on successfull submit
- Added missing date field
- Added date and datetime validation
This commit is contained in:
David Monjoie
2015-09-15 14:00:36 +02:00
parent 0902554953
commit d210744ef0
4 changed files with 66 additions and 17 deletions
+4 -9
View File
@@ -1,14 +1,9 @@
{
'name': 'Website Form Builder',
'category': 'Website',
'summary': 'Build custom web forms using the website builder',
'name': 'Website Form Controller',
'category': 'Hidden',
'summary': 'Generic controller for website forms',
'description': 'Generic controller for website forms',
'version': '1.0',
'description': """
Odoo Form Builder
====================
Allows you to build web forms on the website using the website builder.
""",
'depends': ['website', 'mail'],
'data': [
'views/assets.xml',
+2 -2
View File
@@ -96,7 +96,7 @@ class WebsiteForm(http.Controller):
'custom': '', # Custom fields values
}
authorized_fields = model.get_authorized_fields();
authorized_fields = model.sudo().get_authorized_fields();
error_fields = []
for field_name, field_value in kwargs.items():
@@ -181,7 +181,7 @@ class WebsiteForm(http.Controller):
def insert_attachment(self, model, id_record, files):
orphan_attachment_ids = []
record = model.env[model.model].browse(id_record)
authorized_fields = model.get_authorized_fields()
authorized_fields = model.sudo().get_authorized_fields()
for file in files:
custom_field = file.field_name not in authorized_fields
attachment_value = {
+7 -2
View File
@@ -1,6 +1,7 @@
from openerp import tools
from openerp import models, fields, api
MAGIC_FIELDS = ["id", "create_uid", "create_date", "write_uid", "write_date", "__last_update"]
class website_form_config(models.Model):
_inherit = 'website'
@@ -23,9 +24,8 @@ class website_form_model(models.Model):
def get_authorized_fields(self):
model = self.env[self.model]
fields_get = model.fields_get()
domain = [('model_id', 'in', self.all_inherited_model_ids()), ('website_form_blacklisted', '=', False)]
for elem in self.env['ir.model.fields'].search(domain):
for elem in self.env['ir.model.fields'].search([('model_id', 'in', self.all_inherited_model_ids())]):
if elem.website_form_blacklisted:
fields_get.pop(elem.name, None)
for key, val in model._inherits.iteritems():
@@ -36,6 +36,11 @@ class website_form_model(models.Model):
for field in [f for f in fields_get if f in default_values]:
fields_get[field]['required'] = False
# Remove readonly and magic fields
for field in fields_get.keys():
if fields_get[field]['readonly'] or field in MAGIC_FIELDS:
del fields_get[field]
return fields_get
@@ -20,9 +20,7 @@ odoo.define('website_form.animation', function (require) {
// Initialize datetimepickers
var l10n = _t.database.parameters;
this.$target.find('.o_website_form_datetime').datetimepicker({
pickTime: true,
useSeconds: true,
var datepickers_options = {
startDate: moment({ y: 1900 }),
endDate: moment().add(200, "y"),
calendarWeeks: true,
@@ -34,7 +32,13 @@ odoo.define('website_form.animation', function (require) {
},
language : moment.locale(),
format : time.strftime_to_moment_format(l10n.date_format +' '+ l10n.time_format),
});
}
this.$target.find('.o_website_form_datetime').datetimepicker(datepickers_options);
// Adapt options to date-only pickers
datepickers_options.pickTime = false;
datepickers_options.format = time.strftime_to_moment_format(l10n.date_format);
this.$target.find('.o_website_form_date').datetimepicker(datepickers_options);
},
stop: function() {
@@ -114,6 +118,9 @@ odoo.define('website_form.animation', function (require) {
else {
self.update_status('success');
}
// Reset the form
self.$target[0].reset();
}
})
.fail(function(result_data){
@@ -122,6 +129,7 @@ odoo.define('website_form.animation', function (require) {
},
check_error_fields: function(error_fields) {
var self = this;
var form_valid = true;
// Loop on all fields
this.$target.find('.form-field').each(function(k, field){
@@ -146,6 +154,13 @@ odoo.define('website_form.animation', function (require) {
return input.required && input.type == 'checkbox'
})
return !_.any(checkboxes, function(checkbox){return checkbox.checked})
// Special cases for dates and datetimes
} else if ($(input).hasClass('o_website_form_date')) {
return !self.is_datetime_valid(input.value, 'date');
} else if ($(input).hasClass('o_website_form_datetime')) {
return !self.is_datetime_valid(input.value, 'datetime');
} else {
return !input.checkValidity();
}
@@ -161,6 +176,40 @@ odoo.define('website_form.animation', function (require) {
return form_valid;
},
is_datetime_valid: function(value, type_of_date) {
if (value === "") {
return true;
} else {
try {
this.parse_date(value, type_of_date);
return true;
} catch(e) {
return false;
}
}
},
// This is a stripped down version of format.js parse_value function
parse_date: function (value, type_of_date, value_if_empty) {
var date_pattern = time.strftime_to_moment_format(_t.database.parameters.date_format),
time_pattern = time.strftime_to_moment_format(_t.database.parameters.time_format);
var date_pattern_wo_zero = date_pattern.replace('MM','M').replace('DD','D'),
time_pattern_wo_zero = time_pattern.replace('HH','H').replace('mm','m').replace('ss','s');
switch (type_of_date) {
case 'datetime':
var datetime = moment(value, [date_pattern + ' ' + time_pattern, date_pattern_wo_zero + ' ' + time_pattern_wo_zero], true);
if (datetime.isValid())
return time.datetime_to_str(datetime.toDate());
throw new Error(_.str.sprintf(_t("'%s' is not a correct datetime"), value));
case 'date':
var date = moment(value, [date_pattern, date_pattern_wo_zero], true);
if (date.isValid())
return time.date_to_str(date.toDate());
throw new Error(_.str.sprintf(_t("'%s' is not a correct date"), value));
}
return value;
},
update_status: function(status) {
this.$target.find('#o_website_form_result').replaceWith(qweb.render("website_form.status_" + status))
},