diff --git a/addons/website_mail/controllers/main.py b/addons/website_mail/controllers/main.py index a6b400ec4a1..49caf20f7ff 100644 --- a/addons/website_mail/controllers/main.py +++ b/addons/website_mail/controllers/main.py @@ -63,6 +63,7 @@ class WebsiteMail(http.Controller): @http.route(['/website_mail/is_follower'], type='json', auth="public", website=True) def call(self, model, id, **post): + id = int(id) cr, uid, context = request.cr, request.uid, request.context partner_obj = request.registry.get('res.partner') @@ -76,17 +77,28 @@ class WebsiteMail(http.Controller): partner_id = users_obj.browse(cr, SUPERUSER_ID, uid, context).partner_id elif request.session.get('partner_id'): partner_id = partner_obj.browse(cr, SUPERUSER_ID, request.session.get('partner_id'), context) - - email = "" - is_follower = False - if partner_id: - email = partner_id and partner_id.email - is_follower = partner_id.id in [ - fol.id for fol in obj.browse(cr, SUPERUSER_ID, id, context).message_follower_ids] + email = partner_id and partner_id.email or "" - return { + values = { 'is_user': uid != public_id, 'email': email, - 'is_follower': is_follower + 'is_follower': False, } + if not obj: + return values + obj_ids = obj.exists(cr, SUPERUSER_ID, [id], context=context) + if obj_ids: + if partner_id: + values['is_follower'] = len( + request.registry['mail.followers'].search( + cr, SUPERUSER_ID, [ + ('res_model', '=', 'mail.group'), + ('res_id', '=', obj_ids[0]), + ('partner_id', '=', partner_id.id) + ], context=context)) == 1 + if post.get('fields'): + record = obj.read(cr, SUPERUSER_ID, obj_ids[0], fields=post.get('fields'), context=context) + values.update(record) + + return values diff --git a/addons/website_mail/static/src/js/website_mail.js b/addons/website_mail/static/src/js/website_mail.js index bd34b526ddd..54e355def53 100644 --- a/addons/website_mail/static/src/js/website_mail.js +++ b/addons/website_mail/static/src/js/website_mail.js @@ -7,17 +7,21 @@ selector: ".js_follow", start: function (editable_mode) { var self = this; + this.is_user = false; - // set value and display button - self.$target.find("input").removeClass("hidden"); openerp.jsonRpc('/website_mail/is_follower', 'call', { model: this.$target.data('object'), - id: +this.$target.data('id'), + id: this.$target.data('id'), + fields: ['name', 'alias_id'], }).always(function (data) { + console.log(data); + self.is_user = data.is_user; + self.$target.find('.js_mg_email').attr('href', 'mailto:' + data.alias_id[1]); + self.$target.find('.js_mg_link').attr('href', '/groups/' + data.id); + self.toggle_subscription(data.is_follower); self.$target.find('input.js_follow_email') .val(data.email ? data.email : "") - .attr("disabled", data.is_follower && data.email.length ? "disabled" : false); - self.$target.attr("data-follow", data.is_follower ? 'on' : 'off'); + .attr("disabled", data.is_follower || (data.email.length && self.is_user) ? "disabled" : false); self.$target.removeClass("hidden"); }); @@ -30,10 +34,11 @@ self.on_click(); }); } + return; }, on_click: function () { var self = this; - var $email = this.$target.find(".js_follow_email:visible"); + var $email = this.$target.find(".js_follow_email"); if ($email.length && !$email.val().match(/.+@.+/)) { this.$target.addClass('has-error'); @@ -47,13 +52,27 @@ 'message_is_follower': this.$target.attr("data-follow") || "off", 'email': $email.length ? $email.val() : false, }).then(function (follow) { - if (follow) { - self.$target.find(".js_follow_email, .input-group-btn").addClass("hidden"); - self.$target.find(".alert").removeClass("hidden"); - } - self.$target.find('input.js_follow_email').attr("disabled", follow ? "disabled" : false); - self.$target.attr("data-follow", follow ? 'on' : 'off'); + self.toggle_subscription(follow); }); }, + toggle_subscription: function(follow) { + if (follow) { + this.$target.find(".js_mg_follow_form").addClass("hidden"); + this.$target.find(".js_mg_details").removeClass("hidden"); + } + else { + this.$target.find(".js_mg_follow_form").removeClass("hidden"); + this.$target.find(".js_mg_details").addClass("hidden"); + } + this.$target.find('input.js_follow_email').attr("disabled", follow || this.is_user ? "disabled" : false); + this.$target.attr("data-follow", follow ? 'on' : 'off'); + }, + }); + + $(document).ready(function () { + $('.js_follow_btn').on('click', function (ev) { + var email = $(ev.currentTarget).parents('.js_mg_follow_form').first().find('.js_follow_email').val(); + $(document).find('.js_follow_email').val(email); + }); }); })(); diff --git a/addons/website_mail/views/snippets.xml b/addons/website_mail/views/snippets.xml index c9f76ce4273..ef6e484365f 100644 --- a/addons/website_mail/views/snippets.xml +++ b/addons/website_mail/views/snippets.xml @@ -503,20 +503,26 @@ Discussion Group -