Skip to content

Commit

Permalink
notif form updated based on cms_form updates
Browse files Browse the repository at this point in the history
  • Loading branch information
simahawk committed May 26, 2017
1 parent 25c9e22 commit 72ea69a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
1 change: 1 addition & 0 deletions cms_notifications/models/__init__.py
Expand Up @@ -4,3 +4,4 @@
from . import notification_listing
from . import res_users
from . import test_models
from . import widget
35 changes: 14 additions & 21 deletions cms_notifications/models/notification_panel_form.py
Expand Up @@ -3,9 +3,7 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp import models, _
from openerp.addons.cms_form.utils import form_to_bool

from ..widget import NotificationSelectionWidget
from openerp.addons.cms_form.utils import string_to_bool


class CMSNotificationPanel(models.AbstractModel):
Expand All @@ -19,7 +17,11 @@ class CMSNotificationPanel(models.AbstractModel):
'notify_email',
'notify_frequency',
)
_form_sub_fields = ('notify_frequency', )
_form_sub_fields = {
'notify_email': {
'digest': ('notify_frequency', ),
}
}

@property
def form_title(self):
Expand All @@ -36,25 +38,16 @@ def form_msg_success_updated(self):
def form_next_url(self, main_object=None):
return '/my/settings/notifications'

@property
def form_widgets(self):
return {
'notify_email': 'cms.form.widget.notif_radio',
}

@property
def _super(self):
return super(CMSNotificationPanel, self)

def form_update_fields_attributes(self, _fields):
"""Override to add help messages."""
self._super.form_update_fields_attributes(_fields)

subwidgets = {
'digest': {
'notify_frequency': _fields['notify_frequency']
}
}
# help msg is included in each option
_fields['notify_email']['help'] = ''
_fields['notify_email']['widget'] = NotificationSelectionWidget(
self, 'notify_email', _fields['notify_email'],
subwidgets=subwidgets)

def _form_master_slave_info(self):
info = self._super._form_master_slave_info()
info.update({
Expand Down Expand Up @@ -102,10 +95,10 @@ def form_get_loader(self, fname, field,
return loader

def _form_load_subtype_conf_loader(
self, form, main_object, fname, value, **req_values):
self, fname, field, value, **req_values):
"""Automatically load value for subtype conf fields."""
if fname in req_values:
value = form_to_bool(self, fname, value, **req_values)
value = string_to_bool(req_values.get(fname))
else:
subtype = self.env.ref(self._form_subtype_fields[fname])
explicitly_enabled = \
Expand Down
Expand Up @@ -2,24 +2,27 @@
# Copyright 2017 Simone Orsi
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from openerp.addons.cms_form.widgets import RadioSelectionWidget
from openerp import models


class NotificationSelectionWidget(RadioSelectionWidget):
key = 'cms_notifications.field_widget_notification_selection'
class NotificationSelectionWidget(models.AbstractModel):
_name = 'cms.form.widget.notif_radio'
_inherit = 'cms.form.widget.radio'
_w_template = 'cms_notifications.field_widget_notification_selection'

help_tmpl_prefix = 'cms_notifications.notify_email_help_'

@property
def option_items(self):
def w_option_items(self):
"""Change options order and inject help text."""
items = []
sel = dict(self.field['selection'])
sel = dict(self.w_field['selection'])
for item in ('always', 'digest', 'none', ):
template = self.env.ref(
self.help_tmpl_prefix + item, raise_if_not_found=0)
_help = None
if template:
_help = template.render({'sel': sel, 'field': self.field})
_help = template.render({'sel': sel, 'field': self.w_field})
items.append({
'value': item,
'label': sel[item],
Expand Down
15 changes: 7 additions & 8 deletions cms_notifications/templates/widget.xml
Expand Up @@ -6,14 +6,13 @@
inherit_id="cms_form.field_widget_radio_selection">

<xpath expr="//div[contains(@class, 'option-item')]" position="inside">
<t t-if="widget.subwidgets.get(opt_item['value'])">
<!-- inject subwidgets by option value -->
<t t-foreach="widget.subwidgets[opt_item['value']].items()" t-as="sub">
<t t-set="subwidget" t-value="sub[1]['widget']" />
<div class="subwidget">
<t t-raw="subwidget.render()" />
</div>
</t>
<t t-set="subfields_by_value" t-value="widget.w_subfields_by_value(opt_item['value'])" />
<!-- inject subwidgets by option value -->
<t t-foreach="subfields_by_value.iterkeys()" t-as="sub">
<t t-set="subwidget" t-value="subfields_by_value[sub]['widget']" />
<div class="subwidget">
<t t-raw="subwidget.render()" />
</div>
</t>
</xpath>

Expand Down

0 comments on commit 72ea69a

Please sign in to comment.