[FIX] website_mail, websit_mail_group: moved discussion group snippet to the right...
[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
26
27 class WebsiteMail(http.Controller):
28
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
32
33         partner_obj = request.registry['res.partner']
34         user_obj = request.registry['res.users']
35         website = request.registry['website']
36
37         _id = int(id)
38         _message_is_follower = message_is_follower == 'on'
39         _object = request.registry[object]
40
41         # search partner_id
42         public_id = website.get_public_user(cr, uid, context)
43         if uid != public_id:
44             partner_ids = [user_obj.browse(cr, uid, uid, context).partner_id.id]
45         else:
46             # mail_thread method
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)]
51
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)
56             return False
57         else:
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)
62             return True
63
64     @http.route(['/website_mail/is_follower'], type='json', auth="public", website=True)
65     def call(self, model, id, **post):
66         id = int(id)
67         cr, uid, context = request.cr, request.uid, request.context
68
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']
73
74         partner_id = None
75         public_id = website.get_public_user(cr, uid, context)
76         if uid != public_id:
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 ""
81
82         values = {
83             'is_user': uid != public_id,
84             'email': email,
85             'is_follower': False,
86             'alias_name': False,
87         }
88
89         if not obj:
90             return values
91         obj_ids = obj.exists(cr, SUPERUSER_ID, [id], context=context)
92         if obj_ids:
93             if partner_id:
94                 values['is_follower'] = len(
95                     request.registry['mail.followers'].search(
96                         cr, SUPERUSER_ID, [
97                             ('res_model', '=', 'mail.group'),
98                             ('res_id', '=', obj_ids[0]),
99                             ('partner_id', '=', partner_id.id)
100                         ], context=context)) == 1
101
102         return values