f9a7f0718dcf7c45aad93585c377a0dedad6500a
[odoo/odoo.git] / addons / website_mail / controllers / main.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 from openerp import SUPERUSER_ID
23 from openerp.addons.web import http
24 from openerp.addons.web.http import request
25 from openerp.addons.website.models import website
26
27
28 class WebsiteMail(http.Controller):
29
30     def _find_or_create_partner(self, email, context=None):
31         # TDE TODO: FIXME: use mail_thread method
32         partner_obj = request.registry['res.partner']
33         user_obj = request.registry['res.users']
34         partner_ids = []
35         if request.context['is_public_user'] and email and email != u'false':  # post contains stringified booleans
36             partner_ids = partner_obj.search(request.cr, SUPERUSER_ID, [("email", "=", email)], context=request.context)
37             if not partner_ids:
38                 partner_ids = [partner_obj.name_create(request.cr, SUPERUSER_ID, email, request.context)]
39         else:
40             partner_ids = [user_obj.browse(request.cr, request.uid, request.uid, request.context).partner_id.id]
41         return partner_ids
42
43     @website.route(['/website_mail/follow/'], type='json', auth="public")
44     def website_message_subscribe(self, id=0, object=None, message_is_follower="on", email=False, **post):
45         _id = int(id)
46         _message_is_follower = message_is_follower == 'on'
47         _object = request.registry[object]
48         partner_ids = self._find_or_create_partner(email, request.context)
49
50         if _message_is_follower:
51             _object.check_access_rule(request.cr, request.uid, [_id], 'read', request.context)
52             _object.message_unsubscribe(request.cr, SUPERUSER_ID, [_id], partner_ids, context=request.context)
53         else:
54             _object.check_access_rule(request.cr, request.uid, [_id], 'read', request.context)
55             _object.message_subscribe(request.cr, SUPERUSER_ID, [_id], partner_ids, context=request.context)
56         obj = _object.browse(request.cr, request.uid, _id)
57
58         return obj.message_is_follower and 1 or 0