Skip to content

Commit

Permalink
[FIX] website_mail_channel : channel use member and not follower
Browse files Browse the repository at this point in the history
Since mail.channel user member instead of follower, the frontend widgets required some changes. This commit kind of duplicate follower controller from website_mail for channel member usage. These are now 2 separates widgets. Performance can be improve by removing rpc call when rendering widget, but since this commit is a fix, it does not touch this.
  • Loading branch information
jem-odoo committed Sep 23, 2015
1 parent 90714bf commit c116a35
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 17 deletions.
69 changes: 59 additions & 10 deletions addons/website_mail_channel/controllers/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,65 @@ def view(self, **post):
values = {'groups': groups, 'group_data': group_data}
return request.website.render('website_mail_channel.mail_channels', values)

@http.route(["/groups/subscription/"], type='json', auth="user")
def subscription(self, group_id=0, action=False, **post):
""" TDE FIXME: seems dead code """
cr, uid, context = request.cr, request.uid, request.context
group_obj = request.registry.get('mail.channel')
if action:
group_obj.message_subscribe_users(cr, uid, [group_id], context=context)
else:
group_obj.message_unsubscribe_users(cr, uid, [group_id], context=context)
return []
@http.route(["/groups/is_member"], type='json', auth="public", website=True)
def is_member(self, channel_id=0, **kw):
""" Determine if the current user is member of the given channel_id
:param channel_id : the channel_id to check
"""
current_user_id = request.uid
session_partner_id = request.session.get('partner_id')
public_id = request.website.user_id.id
partner = None
# find the current partner
if current_user_id != public_id:
partner = request.env['res.users'].sudo().browse(current_user_id).partner_id
elif session_partner_id:
partner = request.env['res.partner'].sudo().browse(session_partner_id)

values = {
'is_user': current_user_id != public_id,
'email': partner and partner.email or "",
'is_member': False,
'alias_name': False,
}
# check if the current partner is member or not
channel = request.env['mail.channel'].browse(int(channel_id))
if channel.exists() and partner is not None:
values['is_member'] = bool(partner in channel.sudo().channel_partner_ids)
return values

@http.route(["/groups/subscription"], type='json', auth="public", website=True)
def subscription(self, channel_id=0, subscription="on", email='', **kw):
""" Subscribe to a mailing list : this will create a partner with its email address (if public user not
registered yet) and add it as channel member
:param channel_id : the channel id to join/quit
:param subscription : 'on' to unsubscribe the user, 'off' to subscribe
"""
subscribe = subscription == 'on'
channel = request.env['mail.channel'].browse(int(channel_id))
partner_ids = []

# search partner_id
current_user_id = request.uid
public_id = request.website.user_id.id
if current_user_id != public_id:
partner_ids = [request.env['res.users'].browse(current_user_id).partner_id.id]
else: # mail_thread method
partner_ids = channel.sudo()._find_partner_from_emails([email], check_followers=True)
if not partner_ids or not partner_ids[0]:
name = email.split('@')[0]
partner_ids = [request.env['res.partner'].sudo().create({'name': name, 'email': email}).id]

# add or remove channel members
if subscribe:
channel.check_access_rule('read')
channel.sudo().write({'channel_partner_ids': [(3, partner_id) for partner_id in partner_ids]})
return False
else: # add partner to the channel
request.session['partner_id'] = partner_ids[0]
channel.check_access_rule('read')
channel.sudo().write({'channel_partner_ids': [(4, partner_id) for partner_id in partner_ids]})
return True

@http.route([
"/groups/<model('mail.channel'):group>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ animation.registry.follow_alias = animation.Class.extend({
start: function (editable_mode) {
var self = this;
this.is_user = false;
ajax.jsonRpc('/website_mail/is_follower', 'call', {
ajax.jsonRpc('/groups/is_member', 'call', {
model: this.$target.data('object'),
id: this.$target.data('id'),
channel_id: this.$target.data('id'),
get_alias_info: true,
}).always(function (data) {
self.is_user = data.is_user;
self.email = data.email;
self.$target.find('.js_mg_link').attr('href', '/groups/' + self.$target.data('id'));
self.toggle_subscription(data.is_follower, data.email);
self.toggle_subscription(data.is_member, data.email);
self.$target.removeClass("hidden");
});

Expand All @@ -43,10 +43,10 @@ animation.registry.follow_alias = animation.Class.extend({
}
this.$target.removeClass('has-error');

ajax.jsonRpc('/website_mail/follow', 'call', {
'id': +this.$target.data('id'),
ajax.jsonRpc('/groups/subscription', 'call', {
'channel_id': +this.$target.data('id'),
'object': this.$target.data('object'),
'message_is_follower': this.$target.attr("data-follow") || "off",
'subscription': this.$target.attr("data-follow") || "off",
'email': $email.length ? $email.val() : false,
}).then(function (follow) {
self.toggle_subscription(follow, self.email);
Expand Down
25 changes: 24 additions & 1 deletion addons/website_mail_channel/views/website_mail_channel.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,30 @@
<i class='fa fa-fw fa-envelope-o'/> <t t-raw="group_data[group.id]['monthly_message_nbr']"/> messages / month
</div>
<div class="col-md-3">
<t t-call="website_mail.follow"><t t-set="object" t-value="group"/></t>
<!--<t t-call="website_mail.follow"><t t-set="object" t-value="group"/></t>-->

<div class="js_follow_alias"
t-att-data-id="group.id"
data-object="mail.channel"
data-follow="off">
<div class="input-group js_mg_follow_form">
<input
type="email"
name="email"
class="js_follow_email form-control"
placeholder="your email..."/>
<span class="input-group-btn">
<button href="#" class="btn btn-primary js_follow_btn">Subscribe</button>
</span>
</div>
<p class="js_mg_details hidden well well-sm">
<span class="js_mg_email hidden"><i class="fa fa-envelope-o"/><a href="#" class="js_mg_email"> send mail</a> - </span>
<i class="fa fa-file-o"/><a href="#" class="js_mg_link"> archives</a> -
<i class="fa fa-times"/><a href="#" class="js_unfollow_btn"> unsubscribe</a>
</p>
</div>


</div>
</div>
</div>
Expand Down

0 comments on commit c116a35

Please sign in to comment.