1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as
9 # published by the Free Software Foundation, either version 3 of the
10 # License, or (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 ##############################################################################
22 from openerp import SUPERUSER_ID
23 from openerp.addons.web import http
24 from openerp.addons.web.http import request
27 class WebsiteMail(http.Controller):
29 @http.route(['/website_mail/follow'], type='json', auth="public", website=True)
30 def website_message_subscribe(self, id=0, object=None, message_is_follower="on", email=False, **post):
31 cr, uid, context = request.cr, request.uid, request.context
33 partner_obj = request.registry['res.partner']
34 user_obj = request.registry['res.users']
35 website = request.registry['website']
38 _message_is_follower = message_is_follower == 'on'
39 _object = request.registry[object]
42 public_id = website.get_public_user(cr, uid, context)
44 partner_ids = [user_obj.browse(cr, uid, uid, context).partner_id.id]
47 partner_ids = _object._find_partner_from_emails(
48 cr, SUPERUSER_ID, _id, [email], context=context, check_followers=True)
49 if not partner_ids or not partner_ids[0]:
50 partner_ids = [partner_obj.create(cr, SUPERUSER_ID, {'name': email, 'email': email}, context=context)]
52 # add or remove follower
53 if _message_is_follower:
54 _object.check_access_rule(cr, uid, [_id], 'read', context)
55 _object.message_unsubscribe(cr, SUPERUSER_ID, [_id], partner_ids, context=context)
58 _object.check_access_rule(cr, uid, [_id], 'read', context)
59 # add partner to session
60 request.session['partner_id'] = partner_ids[0]
61 _object.message_subscribe(cr, SUPERUSER_ID, [_id], partner_ids, context=context)
64 @http.route(['/website_mail/is_follower'], type='json', auth="public", website=True)
65 def call(self, model, id, **post):
67 cr, uid, context = request.cr, request.uid, request.context
69 partner_obj = request.registry.get('res.partner')
70 users_obj = request.registry.get('res.users')
71 obj = request.registry.get(model)
72 website = request.registry['website']
75 public_id = website.get_public_user(cr, uid, context)
77 partner_id = users_obj.browse(cr, SUPERUSER_ID, uid, context).partner_id
78 elif request.session.get('partner_id'):
79 partner_id = partner_obj.browse(cr, SUPERUSER_ID, request.session.get('partner_id'), context)
80 email = partner_id and partner_id.email or ""
83 'is_user': uid != public_id,
91 obj_ids = obj.exists(cr, SUPERUSER_ID, [id], context=context)
94 values['is_follower'] = len(
95 request.registry['mail.followers'].search(
97 ('res_model', '=', 'mail.group'),
98 ('res_id', '=', obj_ids[0]),
99 ('partner_id', '=', partner_id.id)
100 ], context=context)) == 1