summaryrefslogtreecommitdiff
path: root/addons/mail/controllers
diff options
context:
space:
mode:
authorRémy Voet (ryv) <ryv@odoo.com>2020-01-08 10:09:20 +0000
committerThibault Delavallée <tde@odoo.com>2020-01-21 16:40:06 +0000
commit15c934a1abfc72c965a6b46618b7b2ae6fad49cc (patch)
tree6494f8b926cdf8a45e47d67f4ef6389e3bd3721a /addons/mail/controllers
parente3ff7a49ae261c22336e6fdfcc01c33b7e9b2229 (diff)
[IMP] (website_)mail: improve routes and management of mail followers
Purpose of this commit is to improve model of followers, notably management code and its use in routes. Indeed it is quite an old model and code had to be cleaned a bit to improve code readability and maintenance. In this commit we * remove unnecessary code examples in gamification about followers: using that model as example of code for goals is probably not a good idea as it is technical; * rewrite routes called by JS are simplified to better match JS implementation; * introduce computed fields to fetch related partner or channel name, email (partner only) and active status; LINKS Task 1933771 Task 2078313 closes odoo/odoo#39808 Signed-off-by: Thibault Delavallee (tde) <tde@openerp.com> Co-authored-by: Remy Voet <ryv@odoo.com> Co-authored-by: jgi-odoo <jgi@odoo.com> Co-authored-by: Xavier-Do <xdo@odoo.com>
Diffstat (limited to 'addons/mail/controllers')
-rw-r--r--addons/mail/controllers/main.py57
1 files changed, 34 insertions, 23 deletions
diff --git a/addons/mail/controllers/main.py b/addons/mail/controllers/main.py
index 2dbc9c09288..b8a2387dbed 100644
--- a/addons/mail/controllers/main.py
+++ b/addons/mail/controllers/main.py
@@ -141,55 +141,66 @@ class MailController(http.Controller):
return True
@http.route('/mail/read_followers', type='json', auth='user')
- def read_followers(self, follower_ids, res_model):
- followers = []
- # When editing the followers, the "pencil" icon that leads to the edition of subtypes
- # should be always be displayed and not only when "debug" mode is activated.
- is_editable = True
- partner_id = request.env.user.partner_id
- follower_id = None
+ def read_followers(self, follower_ids):
+ request.env['mail.followers'].check_access_rights("read")
follower_recs = request.env['mail.followers'].sudo().browse(follower_ids)
res_ids = follower_recs.mapped('res_id')
+ res_models = set(follower_recs.mapped('res_model'))
+ if len(res_models) > 1:
+ raise AccessError(_("Can't read followers with different targeted model"))
+ res_model = res_models.pop()
+ request.env[res_model].check_access_rights("read")
request.env[res_model].browse(res_ids).check_access_rule("read")
+
+ followers = []
+ follower_id = None
for follower in follower_recs:
- is_uid = partner_id == follower.partner_id
- follower_id = follower.id if is_uid else follower_id
+ if follower.partner_id == request.env.user.partner_id:
+ follower_id = follower.id
followers.append({
'id': follower.id,
- 'name': follower.partner_id.name or follower.channel_id.name,
- 'email': follower.partner_id.email if follower.partner_id else None,
- 'res_model': 'res.partner' if follower.partner_id else 'mail.channel',
- 'res_id': follower.partner_id.id or follower.channel_id.id,
- 'is_editable': is_editable,
- 'is_uid': is_uid,
- 'active': follower.partner_id.active or bool(follower.channel_id),
+ 'partner_id': follower.partner_id.id,
+ 'channel_id': follower.channel_id.id,
+ 'name': follower.name,
+ 'email': follower.email,
+ 'is_active': follower.is_active,
+ # When editing the followers, the "pencil" icon that leads to the edition of subtypes
+ # should be always be displayed and not only when "debug" mode is activated.
+ 'is_editable': True
})
return {
'followers': followers,
- 'subtypes': self.read_subscription_data(res_model, follower_id) if follower_id else None
+ 'subtypes': self.read_subscription_data(follower_id) if follower_id else None
}
@http.route('/mail/read_subscription_data', type='json', auth='user')
- def read_subscription_data(self, res_model, follower_id):
+ def read_subscription_data(self, follower_id):
""" Computes:
- message_subtype_data: data about document subtypes: which are
available, which are followed if any """
- followers = request.env['mail.followers'].browse(follower_id)
+ request.env['mail.followers'].check_access_rights("read")
+ follower = request.env['mail.followers'].sudo().browse(follower_id)
+ follower.ensure_one()
+ request.env[follower.res_model].check_access_rights("read")
+ request.env[follower.res_model].browse(follower.res_id).check_access_rule("read")
# find current model subtypes, add them to a dictionary
- subtypes = request.env['mail.message.subtype'].search(['&', ('hidden', '=', False), '|', ('res_model', '=', res_model), ('res_model', '=', False)])
+ subtypes = request.env['mail.message.subtype'].search([
+ '&', ('hidden', '=', False),
+ '|', ('res_model', '=', follower.res_model), ('res_model', '=', False)])
+ followed_subtypes_ids = set(follower.subtype_ids.ids)
subtypes_list = [{
'name': subtype.name,
'res_model': subtype.res_model,
'sequence': subtype.sequence,
'default': subtype.default,
'internal': subtype.internal,
- 'followed': subtype.id in followers.mapped('subtype_ids').ids,
+ 'followed': subtype.id in followed_subtypes_ids,
'parent_model': subtype.parent_id.res_model,
'id': subtype.id
} for subtype in subtypes]
- subtypes_list = sorted(subtypes_list, key=lambda it: (it['parent_model'] or '', it['res_model'] or '', it['internal'], it['sequence']))
- return subtypes_list
+ return sorted(subtypes_list,
+ key=lambda it: (it['parent_model'] or '', it['res_model'] or '', it['internal'], it['sequence']))
@http.route('/mail/view', type='http', auth='public')
def mail_action_view(self, model=None, res_id=None, access_token=None, **kwargs):