[IMP] improved code.
authorTurkesh Patel (Open ERP) <tpa@tinyerp.com>
Wed, 2 Apr 2014 12:04:22 +0000 (17:34 +0530)
committerTurkesh Patel (Open ERP) <tpa@tinyerp.com>
Wed, 2 Apr 2014 12:04:22 +0000 (17:34 +0530)
bzr revid: tpa@tinyerp.com-20140402120422-qp02ywvki17ruzcp

addons/website_forum/controllers/main.py
addons/website_forum/models/forum.py
addons/website_forum/views/website_forum.xml

index 19ee726..cc63e1f 100644 (file)
@@ -324,7 +324,7 @@ class website_forum(http.Controller):
         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),new_question_id))
 
     @http.route('/forum/<model("website.forum"):forum>/question/postanswer/', type='http', auth="public", multilang=True, methods=['POST'], website=True)
-    def post_answer(self, forum ,post_id, **question):
+    def post_answer(self, forum , post_id, **question):
         if not request.session.uid:
             return login_redirect()
 
@@ -343,41 +343,68 @@ class website_forum(http.Controller):
             }, context=create_context)
         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post_id))
 
-    @http.route(['/forum/<model("website.forum"):forum>/question/<model("website.forum.post"):post>/editanswer',
-                 '/forum/<model("website.forum"):forum>/answer/<model("website.forum.post"):post>/edit/<model("website.forum.post"):answer>']
+    @http.route(['/forum/<model("website.forum"):forum>/question/<model("website.forum.post"):question>/editanswer']
                 , type='http', auth="user", website=True, multilang=True)
-    def edit_answer(self, forum, post, answer='', **kwargs):
-        cr, uid, context = request.cr, request.uid, request.context
-        request.registry['res.users'].write(cr, SUPERUSER_ID, uid, {'forum': True}, context=context)
-        user = request.registry['res.users'].browse(cr, uid, uid, context=context)
-        for record in post.child_ids:
-            if record.user_id.id == request.uid and not answer:
+    def edit_answer(self, forum, question, **kwargs):
+        for record in question.child_ids:
+            if record.user_id.id == request.uid:
                 answer = record
+        return werkzeug.utils.redirect("/forum/%s/question/%s/edit/%s" % (slug(forum), question.id, answer.id))
+    @http.route(['/forum/<model("website.forum"):forum>/edit/question/<model("website.forum.post"):question>',
+                 '/forum/<model("website.forum"):forum>/question/<model("website.forum.post"):question>/edit/<model("website.forum.post"):answer>']
+                , type='http', auth="user", website=True, multilang=True)
+    def edit_post(self, forum, question, answer=None, **kwargs):
+        cr, uid, context = request.cr, request.uid, request.context
 
         history_obj = request.registry['website.forum.post.history']
-        history_ids = history_obj.search(cr, uid, [('post_id','=', answer.id)], order = "id desc", context=context)
+        User = request.registry['res.users']
+        User.write(cr, SUPERUSER_ID, uid, {'forum': True}, context=context)
+        user = User.browse(cr, uid, uid, context=context)
+
+        post_id = answer.id if answer else question.id
+        history_ids = history_obj.search(cr, uid, [('post_id', '=', post_id)], order = "id desc", context=context)
         post_history = history_obj.browse(cr, uid, history_ids, context=context)
 
+        tags = ""
+        for tag_name in question.tags:
+            tags += tag_name.name + ","
+
         values = {
-            'post': post,
+            'question': question,
             'user': user,
-            'post_answer': answer,
+            'tags': tags,
+            'answer': answer,
+            'is_answer': True if answer else False,
             'notifications': self._get_notifications(),
             'forum': forum,
             'post_history': post_history,
             'searches': kwargs
         }
-        return request.website.render("website_forum.edit_answer", values)
+        return request.website.render("website_forum.edit_post", values)
 
-    @http.route('/forum/<model("website.forum"):forum>/question/saveanswer/', type='http', auth="user", multilang=True, methods=['POST'], website=True)
-    def save_edited_answer(self, forum, **post):
+    @http.route('/forum/<model("website.forum"):forum>/post/save', type='http', auth="user", multilang=True, methods=['POST'], website=True)
+    def save_edited_post(self, forum, **post):
         cr, uid, context = request.cr, request.uid, request.context
         request.registry['res.users'].write(cr, SUPERUSER_ID, uid, {'forum': True}, context=context)
-        answer_id = int(post.get('answer_id'))
-        new_question_id = request.registry['website.forum.post'].write( cr, uid, [answer_id], {
-                'content': post.get('content'),
-            }, context=context)
-        return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('post_id')))
+        vals = {
+            'content': post.get('content'),
+        }
+        if post.get('question_tag'):
+            Tag = request.registry['website.forum.tag']
+            tags = post.get('question_tag').strip('[]').replace('"','').split(",")
+            question_tags = []
+            for tag in tags:
+                tag_ids = Tag.search(cr, uid, [('name', 'like', tag)], context=context)
+                if tag_ids:
+                    question_tags.append((6, 0, tag_ids))
+                else:
+                    question_tags.append((0,0,{'name' : tag,'forum_id' : forum.id}))
+            vals.update({'tags': question_tags, 'name': post.get('question_name')})
+
+        post_id = post.get('answer_id') if post.get('answer_id') else post.get('question_id')
+        new_question_id = request.registry['website.forum.post'].write( cr, uid, [int(post_id)], vals, context=context)
+        return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('question_id')))
 
     @http.route(['/forum/<model("website.forum"):forum>/tag'], type='http', auth="public", website=True, multilang=True)
     def tags(self, forum, page=1, **searches):
@@ -491,49 +518,6 @@ class website_forum(http.Controller):
         }
         return data
 
-    @http.route('/forum/<model("website.forum"):forum>/edit/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
-    def edit_question(self, forum, post, **kwarg):
-        cr, uid, context = request.cr, request.uid, request.context
-        user = request.registry['res.users'].browse(cr, uid, uid, context=context)
-        history_obj = request.registry['website.forum.post.history']
-        history_ids = history_obj.search(cr, uid, [('post_id','=', post.id)], order = "id desc", context=context)
-        post_history = history_obj.browse(cr, uid, history_ids, context=context)
-
-        tags = ""
-        for tag_name in post.tags:
-            tags += tag_name.name + ","
-
-        values = {
-            'post': post,
-            'user': user,
-            'tags': tags,
-            'forum': forum,
-            'searches': kwarg,
-            'notifications': self._get_notifications(),
-            'post_history': post_history,
-        }
-        return request.website.render("website_forum.edit_question", values)
-
-    @http.route('/forum/<model("website.forum"):forum>/question/savequestion/', type='http', auth="user", multilang=True, methods=['POST'], website=True)
-    def save_edited_question(self, forum, **post):
-        cr, uid, context = request.cr, request.uid, request.context
-        Tag = request.registry['website.forum.tag']
-        tags = post.get('question_tag').strip('[]').replace('"','').split(",")
-        question_tags = []
-        for tag in tags:
-            tag_ids = Tag.search(cr, uid, [('name', 'like', tag)], context=context)
-            if tag_ids:
-                question_tags.append((6, 0, tag_ids))
-            else:
-                question_tags.append((0,0,{'name' : tag,'forum_id' : forum.id}))
-
-        request.registry['website.forum.post'].write(cr, uid, [int(post.get('post_id'))], {
-            'content': post.get('content'),
-            'name': post.get('question_name'),
-            'tags' : question_tags,
-        }, context=context)
-        return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('post_id')))
-
     @http.route('/forum/correct_answer/', type='json', auth="public", multilang=True, methods=['POST'], website=True)
     def correct_answer(self, **kwarg):
         cr, uid, context = request.cr, request.uid, request.context
index bbcf64a..7814c24 100644 (file)
@@ -107,6 +107,8 @@ class Post(osv.Model):
         for post in self.browse(cr, uid, ids, context=context):
             if post.parent_id:
                 res[post.parent_id.id] = len(post.parent_id.child_ids)
+            else:
+                res[post.id] = len(post.child_ids)
         return res
 
     def _get_child(self, cr, uid, ids, context=None):
index 9c4e3ae..93a75aa 100644 (file)
                     <li> be clear and concise, avoid unnecessary introductions (Hi, ... Thanks...) </li>
                 </ul>
                 <form t-attf-action="/forum/#{ slug(forum) }/question/ask/" method="post" role="form" class="tag_text">
-                    <input type="text" name="question_name" required="True" t-attf-value="#{question_name or ''}"
+                    <input type="text" name="question_name" required="True" t-attf-value="#{question_name}"
                         class="form-control" placeholder="Enter your Question"/>
                     <h5 class="mt20">Please enter a descriptive question (should finish by a '?')</h5>
-                    <input type="hidden" name="karma" t-attf-value="#{user.karma or ''}" id="karma"/>
+                    <input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
                     <textarea name="content" required="True" class="form-control load_editor">
                         <t t-esc="question_content"/>
                     </textarea>
             </t>
         </template>
 
-        <template id="edit_question">
+        <template id="edit_post">
             <t t-call="website_forum.header">
-                <h3>Edit question</h3>
-                <form t-attf-action="/forum/#{ slug(forum) }/question/savequestion/" method="post" role="form" class="tag_text">
+                <h3 t-if="not is_answer">Edit question</h3>
+                <h3 t-if="is_answer">Edit answer</h3>
+                <form t-attf-action="/forum/#{ slug(forum) }/post/save" method="post" role="form" class="tag_text">
                     <select class="form-control post_history">
                         <t t-foreach="post_history" t-as="history">
                             <option t-att-value="history.id"><t t-esc="history.name"/></option>
                         </t>
-                     </select>
-                    <input type="text" name="question_name" id="question_name" required="True" t-attf-value="#{post.name or ''}"
-                        class="form-control" placeholder="Edit your Question"/>
-                    <h5 class="mt20">Please enter a descriptive question (should finish by a '?')</h5>
-                    <input type="hidden" name="karma" t-attf-value="#{user.karma or ''}" id="karma"/>
+                    </select>
+                    <div t-if="not is_answer">
+                        <input type="text" name="question_name" id="question_name" required="True"
+                            t-attf-value="#{question.name}" class="form-control" placeholder="Edit your Question"/>
+                        <h5 class="mt20">Please enter a descriptive question (should finish by a '?')</h5>
+                        <input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
+                    </div>
                     <textarea name="content" required="True" class="form-control load_editor">
-                        <t t-esc="post.content"/>
+                        <t t-if="is_answer"> <t t-esc="answer.content"/></t>
+                        <t t-if="not is_answer"><t t-esc="question.content"/></t>
                     </textarea>
-                    <br/>
-                    <input type="text" name="question_tag" class="form-control col-md-9 load_tags" placeholder="Tags" t-attf-value="#{tags or ''}"/>
-                    <br/>
-                    <input name="post_id" t-att-value="post.id" type="hidden"/>
+                    <input name="question_id" t-att-value="question.id" type="hidden"/>
+                    <div t-if="is_answer">
+                        <input name="answer_id" t-att-value="answer.id" type="hidden"/>
+                    </div>
+                    <div t-if="not is_answer">
+                        <br/>
+                        <input type="text" name="question_tag" class="form-control col-md-9 load_tags" placeholder="Tags" t-attf-value="#{tags}"/>
+                        <br/>
+                    </div>
                     <button class="btn btn-primary btn-lg">Save</button>
                 </form>
                 <script type="text/javascript">
                 - it really helps to select the best questions and answers!
             </p>
             <form t-attf-action="/forum/#{ slug(forum) }/question/postanswer/" method="post" role="form">
-                <input type="hidden" name="karma" t-attf-value="#{user.karma or ''}" id="karma"/>
+                <input type="hidden" name="karma" t-attf-value="#{user.karma}" id="karma"/>
                 <textarea name="content" class="form-control load_editor" required="True"/>
                 <input name="post_id" t-att-value="question.id" type="hidden"/>
                 <button class="btn btn-primary" id="btn_ask_your_question">Post Your Answer</button>
             </script>
         </template>
 
-        <template id="edit_answer">
-            <t t-call="website_forum.header">
-                <h3>Edit answer</h3>
-                <form t-attf-action="/forum/#{ slug(forum) }/question/saveanswer/" method="post" role="form">
-                    <select class="form-control post_history">
-                        <t t-foreach="post_history" t-as="history">
-                            <option t-att-value="history.id"><t t-esc="history.name"/></option>
-                        </t>
-                    </select>
-                    <input type="hidden" name="karma" t-attf-value="#{user.karma or ''}" id="karma"/>
-                    <textarea name="content" required="true" class="form-control load_editor">
-                        <t t-esc="post_answer.content"/>
-                    </textarea>
-                    <input name="post_id" t-att-value="post.id" type="hidden"/>
-                    <input name="answer_id" t-att-value="post_answer.id" type="hidden"/>
-                    <button class="btn btn-primary btn-lg">Save</button>
-                </form>
-                <script type="text/javascript">
-                    CKEDITOR.replace("content");
-                </script>
-            </t>
-        </template>
-
         <template id="vote">
             <div t-attf-class="box oe_grey">
                 <a t-attf-class="fa fa-thumbs-up #{post.user_vote == 1 and 'text-success' or ''}" 
                                     </a>
                                 </li>
                                 <li t-if="user.id == answer.user_id.id or user.karma&gt;=300">
-                                    <a class="text-muted fa fa-edit" t-attf-href="/forum/#{ slug(forum) }/answer/#{ question.id }/edit/#{ answer.id }">edit</a>
+                                    <a class="text-muted fa fa-edit" t-attf-href="/forum/#{ slug(forum) }/question/#{ question.id }/edit/#{ answer.id }">edit</a>
                                 </li>
                                 <li t-if="user.id == answer.user_id.id or user.karma&gt;=1000">
                                     <a class="text-muted delete fa fa-trash-o" href="" t-attf-id="#{answer.id}">delete</a>