[ADD] added functionality to make question favourite, and improved some code
authorTurkesh Patel (Open ERP) <tpa@tinyerp.com>
Mon, 7 Apr 2014 07:20:32 +0000 (12:50 +0530)
committerTurkesh Patel (Open ERP) <tpa@tinyerp.com>
Mon, 7 Apr 2014 07:20:32 +0000 (12:50 +0530)
bzr revid: tpa@tinyerp.com-20140407072032-tf6wjo51ibb95nw0

addons/website_forum/controllers/main.py
addons/website_forum/models/forum.py
addons/website_forum/static/src/css/website_forum.css
addons/website_forum/static/src/css/website_forum.sass
addons/website_forum/static/src/js/website_forum.js
addons/website_forum/views/website_forum.xml

index f6cabb4..b052762 100644 (file)
@@ -542,6 +542,18 @@ class website_forum(http.Controller):
         Post.write( cr, uid, [post.id, post.parent_id.id], { 'correct': correct }, context=context)
         return correct
 
+    @http.route('/forum/favourite_question', type='json', auth="user", multilang=True, methods=['POST'], website=True)
+    def favourite_question(self, **kwarg):
+        cr, uid, context = request.cr, request.uid, request.context
+        Post = request.registry['website.forum.post']
+        post = Post.browse(cr, uid, int(kwarg.get('post_id')), context=context)
+        favourite = False if post.user_favourite else True
+        favourite_ids = [(4, uid)]
+        if post.user_favourite:
+            favourite_ids = [(3, uid)]
+        Post.write( cr, uid, [post.id], { 'favourite_ids': favourite_ids }, context=context)
+        return favourite
+
     @http.route('/forum/<model("website.forum"):forum>/close/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
     def close_question(self, forum, post, **kwarg):
         cr, uid, context = request.cr, request.uid, request.context
index 0e6ac90..4e8ee6f 100644 (file)
@@ -71,23 +71,6 @@ class Post(osv.Model):
     _description = "Question"
     _inherit = ['mail.thread', 'website.seo.metadata']
 
-    def _get_votes(self, cr, uid, ids, field_name, arg, context):
-        res = dict.fromkeys(ids, 0)
-        # Note: read_group is not returning all fields which we passed in list.when it will work uncomment this code and remove remaining code 
-        #Vote = self.pool['website.forum.post.vote']
-        #data = Vote.read_group(cr, uid, [('post_id','in', ids), ('user_id', '=', uid)], [ "post_id", "vote"], groupby=["post_id"], context=context)
-        #for rec in data:
-        #    res[rec[post_id][0]] = rec['vote']
-        for post in self.browse(cr, uid, ids, context=context):
-            if post.vote_ids:
-                for vote in post.vote_ids:
-                    if vote.user_id.id == uid:
-                        if vote.vote == '1':
-                            res[post.id] = 1
-                        elif vote.vote == '-1':
-                            res[post.id] = -1
-        return res
-
     def _get_vote_count(self, cr, uid, ids, field_name, arg, context):
         res = dict.fromkeys(ids, 0)
         for post in self.browse(cr, uid, ids, context=context):
@@ -134,6 +117,30 @@ class Post(osv.Model):
             result[statistic.post_id.id] = True
         return result.keys()
 
+    def _get_user_vote(self, cr, uid, ids, field_name, arg, context):
+        res = dict.fromkeys(ids, 0)
+        # Note: read_group is not returning all fields which we passed in list.when it will work uncomment this code and remove remaining code 
+        #Vote = self.pool['website.forum.post.vote']
+        #data = Vote.read_group(cr, uid, [('post_id','in', ids), ('user_id', '=', uid)], [ "post_id", "vote"], groupby=["post_id"], context=context)
+        #for rec in data:
+        #    res[rec[post_id][0]] = rec['vote']
+        for post in self.browse(cr, uid, ids, context=context):
+            for vote in post.vote_ids:
+                if vote.user_id.id == uid:
+                    if vote.vote == '1':
+                        res[post.id] = 1
+                    elif vote.vote == '-1':
+                        res[post.id] = -1
+        return res
+
+    def _get_user_favourite(self, cr, uid, ids, field_name, arg, context):
+        res = dict.fromkeys(ids, False)
+        user = self.pool['res.users'].browse(cr, uid, uid, context=context)
+        for post in self.browse(cr, uid, ids, context=context):
+            if user in post.favourite_ids:
+                res[post.id] = True
+        return res
+
     _columns = {
         'name': fields.char('Title', size=128),
         'forum_id': fields.many2one('website.forum', 'Forum', required=True),
@@ -144,9 +151,11 @@ class Post(osv.Model):
         'write_uid': fields.many2one('res.users', 'Update by', select=True, readonly=True),
 
         'tags': fields.many2many('website.forum.tag', 'forum_tag_rel', 'forum_id', 'forum_tag_id', 'Tag'),
-        'vote_ids':fields.one2many('website.forum.post.vote', 'post_id', 'Vote'),
+        'vote_ids':fields.one2many('website.forum.post.vote', 'post_id', 'Votes'),
+        'user_vote':fields.function(_get_user_vote, string="My Vote", type='integer'),
 
         'favourite_ids': fields.many2many('res.users', 'forum_favourite_rel', 'forum_id', 'user_id', 'Favourite'),
+        'user_favourite':fields.function(_get_user_favourite, string="My Favourite", type='boolean'),
 
         'state': fields.selection([('active', 'Active'), ('close', 'Close'),('offensive', 'Offensive')], 'Status'),
         'active': fields.boolean('Active'),
@@ -177,13 +186,6 @@ class Post(osv.Model):
             help="Comments on forum post",
         ),
 
-        'user_vote':fields.function(_get_votes, string="My Vote", type='integer',
-            store={
-                'website.forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
-                'website.forum.post.vote': (_get_vote, [], 10),
-            }
-        ),
-
         'vote_count':fields.function(_get_vote_count, string="Votes", type='integer',
             store={
                 'website.forum.post': (lambda self, cr, uid, ids, c={}: ids, ['vote_ids'], 10),
index c81ab4f..6548e8d 100644 (file)
   color: #999999;
 }
 
+.forum_favourite_question {
+  color: #ffcc00 !important;
+}
+
 .load_tags {
   width: 845px !important;
 }
 
-.fa-thumbs-up, .fa-thumbs-down, .oe_answer_true, .oe_answer_false {
+a.no-decoration {
   cursor: pointer;
+  text-decoration: none !important;
 }
 
 .faq-question:hover {
   box-shadow: 0px 4px 6px 2px #eeeeee;
 }
 
+.forum_user_flag {
+  max-width: 25px;
+  display: inline-block;
+  margin-left: 2px;
+}
+
 .tag_text .text-core .text-wrap textarea, .tag_text .text-core .text-wrap input, .tag_text .text-core .text-wrap .text-dropdown, .tag_text .text-core .text-wrap .text-prompt {
   font: 1.2em "Helvetica Neue", Helvetica, Arial, sans-serif !important;
 }
@@ -71,9 +82,3 @@
   font: 1.2em "Helvetica Neue", Helvetica, Arial, sans-serif !important;
   height: 1.2em !important;
 }
-
-.forum_user_flag {
-  max-width: 25px;
-  display: inline-block;
-  margin-left: 2px;
-}
\ No newline at end of file
index e1aeee3..19c0ef1 100644 (file)
 .oe_answer_false
   color: #999999
 
+.forum_favourite_question
+  color: #ffcc00 !important
+
 .load_tags
   width: 845px !important
 
-.fa-thumbs-up, .fa-thumbs-down, .oe_answer_true, .oe_answer_false
+a.no-decoration
   cursor: pointer
+  text-decoration: none !important
 
 .faq-question:hover
   text-decoration: none !important
   -webkit-box-shadow: 0px 4px 6px 2px #eeeeee
   box-shadow: 0px 4px 6px 2px #eeeeee
 
+.forum_user_flag
+  max-width: 25px
+  display: inline-block
+  margin-left: 2px
+
 .tag_text .text-core .text-wrap
   textarea, input, .text-dropdown, .text-prompt
-    font: 1.2em "Helvetica Neue",Helvetica,Arial,sans-serif !important
+    font: 1.2em "Helvetica Neue", Helvetica, Arial, sans-serif !important
   .text-tags .text-tag .text-button
-    font: 1.2em "Helvetica Neue",Helvetica,Arial,sans-serif !important
+    font: 1.2em "Helvetica Neue", Helvetica, Arial, sans-serif !important
     height: 1.2em !important
\ No newline at end of file
index 08a67d5..0cf1439 100644 (file)
@@ -95,6 +95,21 @@ $(document).ready(function () {
         return true;
     });
 
+    $('.favourite_question').on('click', function (ev) {
+        ev.preventDefault();
+        var $link = $(ev.currentTarget);
+        openerp.jsonRpc("/forum/favourite_question", 'call', {
+            'post_id': $link.attr("id")})
+            .then(function (data) {
+                if (data) {
+                    $link.addClass("forum_favourite_question")
+                } else {
+                    $link.removeClass("forum_favourite_question")
+                }
+            });
+        return true;
+    });
+
     $('.comment_delete').on('click', function (ev) {
         ev.preventDefault();
         var $link = $(ev.currentTarget);
index 9a3c087..8918ba8 100644 (file)
 
         <template id="vote">
             <div t-attf-class="box oe_grey">
-                <a t-attf-class="vote_up fa fa-thumbs-up #{post.user_vote == 1 and 'text-success' or ''}" 
+                <a t-attf-class="vote_up fa fa-thumbs-up no-decoration #{post.user_vote == 1 and 'text-success' or ''}" 
                     t-attf-id="#{post.id}" t-attf-value="1"/>
                 <span id="vote_count" t-esc="post.vote_count"/>
-                <a t-attf-class="vote_down fa fa-thumbs-down #{post.user_vote == -1 and 'text-warning' or ''}" 
+                <a t-attf-class="vote_down fa fa-thumbs-down no-decoration #{post.user_vote == -1 and 'text-warning' or ''}" 
                     t-attf-id="#{post.id}" t-attf-value="-1"/>
                 <div>
                     votes
                             <span t-if="question.child_count&gt;1">Answers</span>
                             <span t-if="question.child_count&lt;=1">Answer</span>
                         </div>
+                        <div>
+                            <a t-attf-id="#{question.id}" t-attf-class="favourite_question no-decoration fa fa-2x fa-star #{question.user_favourite and 'forum_favourite_question' or ''}"/>
+                        </div>
                     </div>
                     <div style="margin-left: 95px;">
                         <h1 class="mt0">
                                 <t t-set="post" t-value="answer"/>
                             </t>
                             <div class="text-muted">
-                                <a t-attf-id="#{answer.id}" t-if="answer.correct" class="accept_answer fa fa-2x fa-check-circle oe_answer_true"/>
-                                <a t-attf-id="#{answer.id}" t-if="not answer.correct" class="accept_answer fa fa-2x fa-check-circle oe_answer_false"/>
+                                <a t-attf-id="#{answer.id}" t-if="answer.correct" class="accept_answer fa fa-2x fa-check-circle no-decoration oe_answer_true"/>
+                                <a t-attf-id="#{answer.id}" t-if="not answer.correct" class="accept_answer fa fa-2x fa-check-circle no-decoration oe_answer_false"/>
                             </div>
                         </div>
                         <div style="margin-left: 95px;" class="clearfix">
                             <td>
                                 <span class="fa fa-thumbs-up"/>
                                 <span t-esc="up_votes"/>
-                                <span class="fa fa-thumbs-down "/>
+                                <span class="fa fa-thumbs-down"/>
                                 <span t-esc="down_votes"/>
                             </td>
                         </tr>