[ADD] website_mail: added templates, js and css to have a generic follow button.
authorThibault Delavallée <tde@openerp.com>
Mon, 23 Sep 2013 09:59:09 +0000 (11:59 +0200)
committerThibault Delavallée <tde@openerp.com>
Mon, 23 Sep 2013 09:59:09 +0000 (11:59 +0200)
Based on the same principle as publish, there is now a generic button to follow
or unfollow objects in openerp. This should replace all custom instance of
subscription in other addons.

bzr revid: tde@openerp.com-20130923095909-4p5aiswlwy14g32k

addons/website_mail/__init__.py
addons/website_mail/__openerp__.py
addons/website_mail/controllers/__init__.py [new file with mode: 0644]
addons/website_mail/controllers/main.py [new file with mode: 0644]
addons/website_mail/static/src/css/Makefile [new file with mode: 0644]
addons/website_mail/static/src/css/website_mail.css [new file with mode: 0644]
addons/website_mail/static/src/css/website_mail.sass [new file with mode: 0644]
addons/website_mail/static/src/js/website_mail.js [new file with mode: 0644]
addons/website_mail/views/website_mail.xml [new file with mode: 0644]

index f452f9f..4eb031a 100644 (file)
@@ -19,4 +19,5 @@
 #
 ##############################################################################
 
+import controllers
 import mail_message
index df01cb6..bf4620d 100644 (file)
     'category': 'Website',
     'summary': 'Glue Module',
     'version': '0.1',
-    'description': """Glue module holding mail improvements for website.
-        """,
+    'description': """Glue module holding mail improvements for website.""",
     'author': 'OpenERP SA',
     'depends': ['website', 'mail'],
-    'data': [],
-    'qweb': [],
+    'data': [
+        'views/website_mail.xml',
+    ],
+    'css': [
+        'static/src/css/website_mail.css',
+    ],
+    'js': [
+        'static/src/js/website_mail.js',
+    ],
+    'qweb': [
+        'static/src/xml/website_mail.xml'
+    ],
     'installable': True,
     'auto_install': True,
 }
diff --git a/addons/website_mail/controllers/__init__.py b/addons/website_mail/controllers/__init__.py
new file mode 100644 (file)
index 0000000..8ee9bae
--- /dev/null
@@ -0,0 +1 @@
+import main
diff --git a/addons/website_mail/controllers/main.py b/addons/website_mail/controllers/main.py
new file mode 100644 (file)
index 0000000..f1b91ed
--- /dev/null
@@ -0,0 +1,58 @@
+# -*- coding: utf-8 -*-
+##############################################################################
+#
+#    OpenERP, Open Source Management Solution
+#    Copyright (C) 2013-Today OpenERP SA (<http://www.openerp.com>).
+#
+#    This program is free software: you can redistribute it and/or modify
+#    it under the terms of the GNU Affero General Public License as
+#    published by the Free Software Foundation, either version 3 of the
+#    License, or (at your option) any later version.
+#
+#    This program is distributed in the hope that it will be useful,
+#    but WITHOUT ANY WARRANTY; without even the implied warranty of
+#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#    GNU Affero General Public License for more details.
+#
+#    You should have received a copy of the GNU Affero General Public License
+#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+##############################################################################
+
+from openerp import SUPERUSER_ID
+from openerp.addons.web import http
+from openerp.addons.web.http import request
+from openerp.addons.website import website
+
+
+class WebsiteMail(http.Controller):
+
+    def _find_or_create_partner(self, email, context=None):
+        # TDE TODO: FIXME: use mail_thread method
+        partner_obj = request.registry['res.partner']
+        user_obj = request.registry['res.users']
+        partner_ids = []
+        if request.context['is_public_user'] and email:
+            partner_ids = partner_obj.search(request.cr, SUPERUSER_ID, [("email", "=", email)], context=request.context)
+            if not partner_ids:
+                partner_ids = [partner_obj.create(request.cr, SUPERUSER_ID, {"email": email, "name": email}, request.context)]
+        else:
+            partner_ids = [user_obj.browse(request.cr, request.uid, request.uid, request.context).partner_id.id]
+        return partner_ids
+
+    @website.route(['/website_mail/follow/'], type='http', auth="public")
+    def website_message_subscribe(self, **post):
+        _id = int(post['id'])
+        _message_is_follower = post['message_is_follower'] == 'on'
+        _object = request.registry[post['object']]
+        partner_ids = self._find_or_create_partner(post.get('email'), request.context)
+
+        if _message_is_follower:
+            _object.check_access_rule(request.cr, request.uid, [_id], 'read', request.context)
+            _object.message_unsubscribe(request.cr, SUPERUSER_ID, [_id], partner_ids, context=request.context)
+        else:
+            _object.check_access_rule(request.cr, request.uid, [_id], 'read', request.context)
+            _object.message_subscribe(request.cr, SUPERUSER_ID, [_id], partner_ids, context=request.context)
+        obj = _object.browse(request.cr, request.uid, _id)
+
+        return obj.message_is_follower and "1" or "0"
diff --git a/addons/website_mail/static/src/css/Makefile b/addons/website_mail/static/src/css/Makefile
new file mode 100644 (file)
index 0000000..d7c1ea7
--- /dev/null
@@ -0,0 +1,2 @@
+sass:
+       sass -t expanded --unix-newlines --watch website_mail.sass:website_mail.css&
diff --git a/addons/website_mail/static/src/css/website_mail.css b/addons/website_mail/static/src/css/website_mail.css
new file mode 100644 (file)
index 0000000..a93199a
--- /dev/null
@@ -0,0 +1,40 @@
+/* ---- FOLLOW ---- */
+a[data-follow] {
+  text-decoration: none !important;
+  z-index: 2;
+}
+a[data-follow] .label {
+  padding: 5px 8px;
+}
+a[data-follow] .css_unfollow, a[data-follow] .css_follow, a[data-follow] .css_unfollowed, a[data-follow] .css_followed {
+  display: none;
+}
+a[data-follow][data-follow='off'] .css_unfollowed, a[data-follow][data-follow='off']:hover .css_follow {
+  display: inline;
+}
+a[data-follow][data-follow='off']:hover .css_unfollowed {
+  display: none;
+}
+a[data-follow][data-follow='on'] .css_followed, a[data-follow][data-follow='on']:hover .css_unfollow {
+  display: inline;
+}
+a[data-follow][data-follow='on']:hover .css_followed {
+  display: none;
+}
+
+[data-follow='off']:not(a) > :not([data-follow]) {
+  opacity: 0.5;
+}
+
+[data-follow]:not(a) {
+  position: relative;
+  overflow: visible;
+  /*&:hover > [data-follow] */
+  /*     display: block */
+}
+[data-follow]:not(a) > [data-follow] {
+  position: absolute;
+  right: -6px;
+  top: -10px;
+  display: none;
+}
diff --git a/addons/website_mail/static/src/css/website_mail.sass b/addons/website_mail/static/src/css/website_mail.sass
new file mode 100644 (file)
index 0000000..b648270
--- /dev/null
@@ -0,0 +1,33 @@
+
+/* ---- FOLLOW ---- */
+a[data-follow]
+    text-decoration: none !important
+    z-index: 2
+    .label
+        padding: 5px 8px
+    .css_unfollow, .css_follow, .css_unfollowed, .css_followed
+        display: none
+    &[data-follow='off']
+        .css_unfollowed, &:hover .css_follow
+            display: inline
+        &:hover .css_unfollowed
+            display: none
+    &[data-follow='on']
+        .css_followed, &:hover .css_unfollow
+            display: inline
+        &:hover .css_followed
+            display: none
+
+[data-follow='off']:not(a)
+    >:not([data-follow])
+        opacity: 0.5
+[data-follow]:not(a)
+    position: relative
+    overflow: visible
+    >[data-follow]
+        position: absolute
+        right: -6px
+        top: -10px
+        display: none
+    /*&:hover > [data-follow]*/
+    /*     display: block*/
diff --git a/addons/website_mail/static/src/js/website_mail.js b/addons/website_mail/static/src/js/website_mail.js
new file mode 100644 (file)
index 0000000..e40715c
--- /dev/null
@@ -0,0 +1,27 @@
+$(document).ready(function () {
+
+    /* ----- FOLLOWING STUFF ---- */
+    $('[data-follow]:has([data-follow])').each(function () {
+        var $pub = $("[data-follow]", this);
+        if($pub.size())
+            $(this).attr("data-follow", $pub.attr("data-follow"));
+        else
+            $(this).removeAttr("data-follow");
+    });
+
+    $(document).on('click', '.js_follow', function (ev) {
+        console.log(ev);
+        ev.preventDefault();
+        var $data = $(":first", this).parents("[data-follow]");
+        var message_is_follower = $data.first().attr("data-follow");
+        $data.attr("data-follow", message_is_follower == 'off' ? 'on' : 'off');
+        $.post('/website_mail/follow', {
+            'id': $(this).data('id'),
+            'object': $(this).data('object'),
+            'message_is_follower': message_is_follower,
+        }, function (result) {
+            $data.attr("data-follow", + result ? 'on' : 'off');
+        });
+    });
+
+});
diff --git a/addons/website_mail/views/website_mail.xml b/addons/website_mail/views/website_mail.xml
new file mode 100644 (file)
index 0000000..7a55352
--- /dev/null
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<openerp>
+    <data>
+
+        <template id="follow">
+            <a href="#" t-att-data-id="object.id" t-att-data-object="object._name" t-att-data-follow="object.id and object.message_is_follower and 'on' or 'off'" class="pull-right js_follow" t-if="editable" t-ignore="true">
+                <span t-attf-class="label label-success css_follow">Follow</span>
+                <span t-attf-class="label label-danger css_unfollow">Unfollow</span>
+                <span t-attf-class="label label-success css_followed">Followed</span>
+                <span t-attf-class="label label-danger css_unfollowed">Unfollowed</span>
+            </a>
+        </template>
+
+        <template id="website.layout" inherit_id="website.layout" inherit_option_id="website.layout">
+            <xpath expr="//head" position="inside">
+                <link rel='stylesheet' href='/website_mail/static/src/css/website_mail.css'/>
+                <script type="text/javascript" src="/website_mail/static/src/js/website_mail.js"></script>
+            </xpath>
+        </template>
+
+    </data>
+</openerp>