[IMP] website_forum: cleaning before merging
[odoo/odoo.git] / addons / website_forum / controllers / main.py
1 # -*- coding: utf-8 -*-
2
3 import werkzeug.urls
4 import simplejson
5
6 from openerp import tools
7 from openerp import SUPERUSER_ID
8 from openerp.addons.web import http
9 from openerp.tools import html2plaintext
10
11 from openerp.tools.translate import _
12 from datetime import datetime, timedelta
13 from openerp.addons.web.http import request
14
15 from dateutil.relativedelta import relativedelta
16 from openerp.addons.website.controllers.main import Website as controllers
17 from openerp.addons.website.models.website import slug
18 from openerp.addons.web.controllers.main import login_redirect
19
20 controllers = controllers()
21
22
23 class WebsiteForum(http.Controller):
24
25     @http.route(['/forum'], type='http', auth="public", website=True, multilang=True)
26     def forum(self, **searches):
27         cr, uid, context = request.cr, request.uid, request.context
28         Forum = request.registry['website.forum']
29         obj_ids = Forum.search(cr, uid, [], context=context)
30         forums = Forum.browse(cr, uid, obj_ids, context=context)
31         return request.website.render("website_forum.forum_index", {'forums': forums})
32
33     @http.route('/forum/new', type='http', auth="user", multilang=True, website=True)
34     def forum_create(self, forum_name="New Forum", **kwargs):
35         forum_id = request.registry['website.forum'].create(request.cr, request.uid, {
36             'name': forum_name,
37         }, context=request.context)
38         return request.redirect("/forum/%s" % slug(forum_id))
39
40     @http.route(['/forum/<model("website.forum"):forum>',
41                  '/forum/<model("website.forum"):forum>/page/<int:page>',
42                  '/forum/<model("website.forum"):forum>/tag/<model("website.forum.tag"):tag>/questions'
43                  ], type='http', auth="public", website=True, multilang=True)
44     def questions(self, forum, tag='', page=1, filters='', sorting='', **searches):
45         cr, uid, context = request.cr, request.uid, request.context
46         Forum = request.registry['website.forum.post']
47         user = request.registry['res.users'].browse(cr, uid, uid, context=context)
48
49         order = "id desc"
50
51         domain = [('forum_id', '=', forum.id), ('parent_id', '=', False)]
52         if searches.get('search'):
53             domain += ['|', ('name', 'ilike', searches['search']), ('content', 'ilike', searches['search'])]
54
55         #filter questions for tag.
56         if tag:
57             if not filters:
58                 filters = 'tag'
59             domain += [('tag_ids', 'in', tag.id)]
60
61         if not filters:
62             filters = 'all'
63         if filters == 'unanswered':
64             domain += [ ('child_ids', '=', False) ]
65         if filters == 'followed':
66             domain += [ ('message_follower_ids', '=', user.partner_id.id) ]
67
68         # Note: default sorting should be based on last activity
69         if not sorting or sorting == 'date':
70             sorting = 'date'
71             order = 'write_date desc'
72         if sorting == 'answered':
73             order = 'child_count desc'
74         if sorting == 'vote':
75             order = 'vote_count desc'
76
77         step = 10
78         question_count = Forum.search(cr, uid, domain, count=True, context=context)
79         pager = request.website.pager(url="/forum/%s" % slug(forum), total=question_count, page=page, step=step, scope=10)
80
81         obj_ids = Forum.search(cr, uid, domain, limit=step, offset=pager['offset'], order=order, context=context)
82         question_ids = Forum.browse(cr, uid, obj_ids, context=context)
83
84         values = {
85             'uid': request.session.uid,
86             'total_questions': question_count,
87             'question_ids': question_ids,
88             'notifications': self._get_notifications(),
89             'forum': forum,
90             'pager': pager,
91             'tag': tag,
92             'filters': filters,
93             'sorting': sorting,
94             'searches': searches,
95         }
96
97         return request.website.render("website_forum.index", values)
98
99     def _get_notifications(self, **kwargs):
100         cr, uid, context = request.cr, request.uid, request.context
101         Message = request.registry['mail.message']
102         BadgeUser = request.registry['gamification.badge.user']
103         #notification to user.
104         badgeuser_ids = BadgeUser.search(cr, uid, [('user_id', '=', uid)], context=context)
105         notification_ids = Message.search(cr, uid, [('res_id', 'in', badgeuser_ids), ('model', '=', 'gamification.badge.user'), ('to_read', '=', True)], context=context)
106         notifications = Message.browse(cr, uid, notification_ids, context=context)
107         user = request.registry['res.users'].browse(cr, uid, uid, context=context)
108         return {"user": user, "notifications": notifications}
109
110     @http.route('/forum/notification_read', type='json', auth="user", multilang=True, methods=['POST'], website=True)
111     def notification_read(self, **kwarg):
112         request.registry['mail.message'].set_message_read(request.cr, request.uid, [int(kwarg.get('notification_id'))], read=True, context=request.context)
113         return True
114
115     @http.route(['/forum/<model("website.forum"):forum>/faq'], type='http', auth="public", website=True, multilang=True)
116     def faq(self, forum, **post):
117         values = { 
118             'searches': {},
119             'forum':forum,
120             'notifications': self._get_notifications(),
121         }
122         return request.website.render("website_forum.faq", values)
123
124     @http.route(['/forum/<model("website.forum"):forum>/question/<model("website.forum.post"):question>'], type='http', auth="public", website=True, multilang=True)
125     def question(self, forum, question, **post):
126         cr, uid, context = request.cr, request.uid, request.context
127
128         #maintain total views on post.
129         # Statistics = request.registry['website.forum.post.statistics']
130         # post_obj = request.registry['website.forum.post']
131         # if request.session.uid:
132         #     view_ids = Statistics.search(cr, uid, [('user_id', '=', request.session.uid), ('post_id', '=', question.id)], context=context)
133         #     if not view_ids:
134         #         Statistics.create(cr, SUPERUSER_ID, {'user_id': request.session.uid, 'post_id': question.id }, context=context)
135         # else:
136         #     request.session[request.session_id] = request.session.get(request.session_id, [])
137         #     if not (question.id in request.session[request.session_id]):
138         #         request.session[request.session_id].append(question.id)
139         #         post_obj._set_view_count(cr, SUPERUSER_ID, [question.id], 'views', 1, {}, context=context)
140
141         #Check that user have answered question or not.
142         answer_done = False
143         for answer in question.child_ids:
144             if answer.user_id.id == request.uid:
145                 answer_done = True
146
147         #Check that user is following question or not
148         partner_id = request.registry['res.users'].browse(cr, uid, request.uid, context=context).partner_id.id
149         message_follower_ids = [follower.id for follower in question.message_follower_ids]
150         following = True if partner_id in message_follower_ids else False
151
152         filters = 'question'
153         user = request.registry['res.users'].browse(cr, uid, uid, context=None)
154         values = {
155             'question': question,
156             'question_data': True,
157             'notifications': self._get_notifications(),
158             'searches': post,
159             'filters': filters,
160             'following': following,
161             'answer_done': answer_done,
162             'reversed': reversed,
163             'forum': forum,
164             'user': user,
165         }
166         return request.website.render("website_forum.post_description_full", values)
167
168     @http.route(['/forum/<model("website.forum"):forum>/comment'], type='http', auth="public", methods=['POST'], website=True)
169     def post_comment(self, forum, post_id, **kwargs):
170         if not request.session.uid:
171             return login_redirect()
172         cr, uid, context = request.cr, request.uid, request.context
173         if kwargs.get('comment'):
174             user = request.registry['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
175             group_ids = user.groups_id
176             group_id = request.registry["ir.model.data"].get_object_reference(cr, uid, 'website_mail', 'group_comment')[1]
177             if group_id in [group.id for group in group_ids]:
178                 Post = request.registry['website.forum.post']
179                 Post.message_post(
180                     cr, uid, int(post_id),
181                     body=kwargs.get('comment'),
182                     type='comment',
183                     subtype='mt_comment',
184                     content_subtype='plaintext',
185                     author_id=user.partner_id.id,
186                     context=dict(context, mail_create_nosubcribe=True))
187
188         post = request.registry['website.forum.post'].browse(cr, uid, int(post_id), context=context)
189         question_id = post.parent_id.id if post.parent_id else post.id
190         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),question_id))
191
192     @http.route(['/forum/<model("website.forum"):forum>/user/<model("res.users"):user>'], type='http', auth="public", website=True, multilang=True)
193     def open_user(self, forum, user, **post):
194         cr, uid, context = request.cr, request.uid, request.context
195         User = request.registry['res.users']
196         Post = request.registry['website.forum.post']
197         Vote = request.registry['website.forum.post.vote']
198         Activity = request.registry['mail.message']
199         Followers = request.registry['mail.followers']
200         Data = request.registry["ir.model.data"]
201
202         #questions and answers by user.
203         user_questions, user_answers = [], []
204         user_post_ids = Post.search(cr, uid, [('forum_id', '=', forum.id), ('user_id', '=', user.id),
205                                              '|', ('active', '=', False), ('active', '=', True)], context=context)
206         user_posts = Post.browse(cr, uid, user_post_ids, context=context)
207         for record in user_posts:
208             if record.parent_id:
209                 user_answers.append(record)
210             else:
211                 user_questions.append(record)
212
213         #showing questions which user following
214         obj_ids = Followers.search(cr, SUPERUSER_ID, [('res_model', '=', 'website.forum.post'),('partner_id' , '=' , user.partner_id.id)], context=context)
215         post_ids = [follower.res_id for follower in Followers.browse(cr, SUPERUSER_ID, obj_ids, context=context)]
216         que_ids = Post.search(cr, uid, [('id', 'in', post_ids), ('forum_id', '=', forum.id), ('parent_id', '=', False)], context=context)
217         followed = Post.browse(cr, uid, que_ids, context=context)
218
219         #showing Favourite questions of user.
220         fav_que_ids = Post.search(cr, uid, [('favourite_ids', '=', user.id), ('forum_id', '=', forum.id), ('parent_id', '=', False)], context=context)
221         favourite = Post.browse(cr, uid, fav_que_ids, context=context)
222
223         #votes which given on users questions and answers.
224         data = Vote.read_group(cr, uid, [('post_id.forum_id', '=', forum.id), ('post_id.user_id', '=', user.id)], ["vote"], groupby=["vote"], context=context)
225         up_votes, down_votes = 0, 0
226         for rec in data:
227             if rec['vote'] == '1':
228                 up_votes = rec['vote_count']
229             elif rec['vote'] == '-1':
230                 down_votes = rec['vote_count']
231         total_votes = up_votes + down_votes
232
233         #Votes which given by users on others questions and answers.
234         post_votes = Vote.search(cr, uid, [('user_id', '=', user.id)], context=context)
235         vote_ids = Vote.browse(cr, uid, post_votes, context=context)
236
237         #activity by user.
238         model, comment = Data.get_object_reference(cr, uid, 'mail', 'mt_comment')
239         activity_ids = Activity.search(cr, uid, [('res_id', 'in', user_post_ids), ('model', '=', 'website.forum.post'), '|', ('subtype_id', '!=', comment), ('subtype_id', '=', False)], context=context)
240         activities = Activity.browse(cr, uid, activity_ids, context=context)
241
242         posts = {}
243         for act in activities:
244             posts[act.res_id] = True
245         posts_ids = Post.browse(cr, uid, posts.keys(), context=context)
246         posts = dict(map(lambda x: (x.id, (x.parent_id or x, x.parent_id and x or False)), posts_ids))
247
248         post['users'] = 'True'
249
250         values = {
251             'uid': uid,
252             'user': user,
253             'main_object': user,
254             'searches': post,
255             'forum': forum,
256             'questions': user_questions,
257             'answers': user_answers,
258             'followed': followed,
259             'favourite': favourite,
260             'total_votes': total_votes,
261             'up_votes': up_votes,
262             'down_votes': down_votes,
263             'activities': activities,
264             'posts': posts,
265             'vote_post': vote_ids,
266             'notifications': self._get_notifications(),
267         }
268         return request.website.render("website_forum.user_detail_full", values)
269
270     @http.route(['/forum/<model("website.forum"):forum>/ask'], type='http', auth="public", website=True, multilang=True)
271     def question_ask(self, forum, **post):
272         if not request.session.uid:
273             return login_redirect()
274         user = request.registry['res.users'].browse(request.cr, request.uid, request.uid, context=request.context)
275         values = {
276             'searches': {},
277             'forum': forum,
278             'user': user,
279             'ask_question': True,
280             'notifications': self._get_notifications(),
281         }
282         return request.website.render("website_forum.ask_question", values)
283
284     @http.route('/forum/<model("website.forum"):forum>/question/ask', type='http', auth="user", multilang=True, methods=['POST'], website=True)
285     def register_question(self, forum, **question):
286         cr, uid, context = request.cr, request.uid, request.context
287         create_context = dict(context)
288
289         Tag = request.registry['website.forum.tag']
290         question_tags = []
291         if question.get('question_tags').strip('[]'):
292             tags = question.get('question_tags').strip('[]').replace('"','').split(",")
293             for tag in tags:
294                 tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context)
295                 if tag_ids:
296                     question_tags.append((4,tag_ids[0]))
297                 else:
298                     question_tags.append((0,0,{'name' : tag,'forum_id' : forum.id}))
299     
300         new_question_id = request.registry['website.forum.post'].create(
301             request.cr, request.uid, {
302                 'user_id': uid,
303                 'forum_id': forum.id,
304                 'name': question.get('question_name'),
305                 'content': question.get('content'),
306                 'tag_idss' : question_tags,
307                 'state': 'active',
308                 'active': True,
309             }, context=create_context)
310         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),new_question_id))
311
312     @http.route('/forum/<model("website.forum"):forum>/question/postanswer', type='http', auth="public", multilang=True, methods=['POST'], website=True)
313     def post_answer(self, forum , post_id, **question):
314         if not request.session.uid:
315             return login_redirect()
316
317         cr, uid, context = request.cr, request.uid, request.context
318         request.registry['res.users'].write(cr, SUPERUSER_ID, uid, {'forum': True}, context=context)
319
320         create_context = dict(context)
321         new_question_id = request.registry['website.forum.post'].create(
322             cr, uid, {
323                 'forum_id': forum.id,
324                 'user_id': uid,
325                 'parent_id': post_id,
326                 'content': question.get('content'),
327                 'state': 'active',
328                 'active': True,
329             }, context=create_context)
330         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post_id))
331
332     @http.route(['/forum/<model("website.forum"):forum>/question/<model("website.forum.post"):question>/editanswer']
333                 , type='http', auth="user", website=True, multilang=True)
334     def edit_answer(self, forum, question, **kwargs):
335         for record in question.child_ids:
336             if record.user_id.id == request.uid:
337                 answer = record
338         return werkzeug.utils.redirect("/forum/%s/question/%s/edit/%s" % (slug(forum), question.id, answer.id))
339  
340     @http.route(['/forum/<model("website.forum"):forum>/edit/question/<model("website.forum.post"):question>',
341                  '/forum/<model("website.forum"):forum>/question/<model("website.forum.post"):question>/edit/<model("website.forum.post"):answer>']
342                 , type='http', auth="user", website=True, multilang=True)
343     def edit_post(self, forum, question, answer=None, **kwargs):
344         cr, uid, context = request.cr, request.uid, request.context
345
346         history_obj = request.registry['website.forum.post.history']
347         User = request.registry['res.users']
348         User.write(cr, SUPERUSER_ID, uid, {'forum': True}, context=context)
349         user = User.browse(cr, uid, uid, context=context)
350
351         post_id = answer.id if answer else question.id
352         history_ids = history_obj.search(cr, uid, [('post_id', '=', post_id)], order = "id desc", context=context)
353         post_history = history_obj.browse(cr, uid, history_ids, context=context)
354
355         tags = ""
356         for tag_name in question.tags:
357             tags += tag_name.name + ","
358
359         values = {
360             'question': question,
361             'user': user,
362             'tags': tags,
363             'answer': answer,
364             'is_answer': True if answer else False,
365             'notifications': self._get_notifications(),
366             'forum': forum,
367             'post_history': post_history,
368             'searches': kwargs
369         }
370         return request.website.render("website_forum.edit_post", values)
371
372     @http.route('/forum/<model("website.forum"):forum>/post/save', type='http', auth="user", multilang=True, methods=['POST'], website=True)
373     def save_edited_post(self, forum, **post):
374         cr, uid, context = request.cr, request.uid, request.context
375         request.registry['res.users'].write(cr, SUPERUSER_ID, uid, {'forum': True}, context=context)
376         vals = {
377             'content': post.get('content'),
378         }
379         question_tags = []
380         if post.get('question_tag').strip('[]'):
381             Tag = request.registry['website.forum.tag']
382             tags = post.get('question_tag').strip('[]').replace('"','').split(",")
383             for tag in tags:
384                 tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context)
385                 if tag_ids:
386                     question_tags += tag_ids
387                 else:
388                     new_tag = Tag.create(cr, uid, {'name' : tag,'forum_id' : forum.id}, context=context)
389                     question_tags.append(new_tag)
390         vals.update({'tag_ids': [(6, 0, question_tags)], 'name': post.get('question_name')})
391
392         post_id = post.get('answer_id') if post.get('answer_id') else post.get('question_id')
393         new_question_id = request.registry['website.forum.post'].write( cr, uid, [int(post_id)], vals, context=context)
394         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('question_id')))
395
396     @http.route(['/forum/<model("website.forum"):forum>/tag'], type='http', auth="public", website=True, multilang=True)
397     def tags(self, forum, page=1, **searches):
398         cr, uid, context = request.cr, request.uid, request.context
399         Tag = request.registry['website.forum.tag']
400         obj_ids = Tag.search(cr, uid, [('forum_id', '=', forum.id)], limit=None, context=context)
401         tags = Tag.browse(cr, uid, obj_ids, context=context)
402         values = {
403             'tags': tags,
404             'notifications': self._get_notifications(),
405             'forum': forum,
406             'searches': {'tags': True}
407         }
408         return request.website.render("website_forum.tag", values)
409
410     @http.route(['/forum/<model("website.forum"):forum>/badge'], type='http', auth="public", website=True, multilang=True)
411     def badges(self, forum, **searches):
412         cr, uid, context = request.cr, request.uid, request.context
413         Badge = request.registry['gamification.badge']
414         badge_ids = Badge.search(cr, uid, [('level', '!=', False)], context=context)
415         badges = Badge.browse(cr, uid, badge_ids, context=context)
416         values = {
417             'badges': badges,
418             'notifications': {},
419             'forum': forum,
420             'searches': {'badges': True}
421         }
422         return request.website.render("website_forum.badge", values)
423
424     @http.route(['/forum/<model("website.forum"):forum>/badge/<model("gamification.badge"):badge>'], type='http', auth="public", website=True, multilang=True)
425     def badge_users(self, forum, badge, **kwargs):
426         users = [badge_user.user_id for badge_user in badge.owner_ids]
427         kwargs['badges'] = 'True'
428
429         values = {
430             'badge': badge,
431             'notifications': {},
432             'users': users,
433             'forum': forum,
434             'searches': kwargs
435         }
436         return request.website.render("website_forum.badge_user", values)
437
438     @http.route(['/forum/<model("website.forum"):forum>/users', '/forum/users/page/<int:page>'], type='http', auth="public", website=True, multilang=True)
439     def users(self, forum, page=1, **searches):
440         cr, uid, context = request.cr, request.uid, request.context
441         User = request.registry['res.users']
442
443         step = 30
444         tag_count = User.search(cr, uid, [('karma', '>', 1)], count=True, context=context)
445         pager = request.website.pager(url="/forum/users", total=tag_count, page=page, step=step, scope=30)
446
447         obj_ids = User.search(cr, uid, [('karma', '>', 1)], limit=step, offset=pager['offset'], context=context)
448         users = User.browse(cr, uid, obj_ids, context=context)
449         searches['users'] = 'True'
450
451         values = {
452             'users': users,
453             'notifications': self._get_notifications(),
454             'pager': pager,
455             'forum': forum,
456             'searches': searches,
457         }
458
459         return request.website.render("website_forum.users", values)
460
461     @http.route('/forum/post_vote', type='json', auth="public", multilang=True, methods=['POST'], website=True)
462     def post_vote(self, **post):
463         if not request.session.uid:
464             return {'error': 'anonymous_user'}
465         cr, uid, context, post_id = request.cr, request.uid, request.context, int(post.get('post_id'))
466         return request.registry['website.forum.post'].vote(cr, uid, [post_id], post.get('vote'), context)
467
468     @http.route('/forum/post_delete', type='json', auth="user", multilang=True, methods=['POST'], website=True)
469     def delete_answer(self, **kwarg):
470         request.registry['website.forum.post'].unlink(request.cr, request.uid, [int(kwarg.get('post_id'))], context=request.context)
471         return True
472
473     @http.route('/forum/<model("website.forum"):forum>/delete/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
474     def delete_question(self, forum, post, **kwarg):
475         #instead of unlink record just change 'active' to false so user can undelete it.
476         request.registry['website.forum.post'].write( request.cr, request.uid, [post.id], {
477             'active': False,
478         }, context=request.context)
479         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id))
480
481     @http.route('/forum/<model("website.forum"):forum>/undelete/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
482     def undelete_question(self, forum, post, **kwarg):
483         request.registry['website.forum.post'].write( request.cr, request.uid, [post.id], {
484             'active': True,
485         }, context=request.context)
486         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id))
487
488     @http.route('/forum/message_delete', type='json', auth="user", multilang=True, methods=['POST'], website=True)
489     def delete_comment(self, **kwarg):
490         request.registry['mail.message'].unlink(request.cr, SUPERUSER_ID, [int(kwarg.get('message_id'))], context=request.context)
491         return True
492
493     @http.route('/forum/selecthistory', type='json', auth="user", multilang=True, methods=['POST'], website=True)
494     def post_history(self, **kwarg):
495         cr, uid, context = request.cr, request.uid, request.context
496         post_history = request.registry['website.forum.post.history'].browse(cr, uid, int(kwarg.get('history_id')), context=context)
497         tags = ""
498         for tag_name in post_history.tags:
499             tags += tag_name.name + ","
500         data = {
501             'name': post_history.post_name,
502             'content': post_history.content,
503             'tags': tags,
504         }
505         return data
506
507     @http.route('/forum/correct_answer', type='json', auth="public", multilang=True, methods=['POST'], website=True)
508     def correct_answer(self, **kwarg):
509         cr, uid, context = request.cr, request.uid, request.context
510         if not request.session.uid:
511             return {'error': 'anonymous_user'}
512
513         Post = request.registry['website.forum.post']
514         post = Post.browse(cr, uid, int(kwarg.get('post_id')), context=context)
515         user = request.registry['res.users'].browse(cr, uid, uid, context=None)
516
517         #if user have not access to accept answer then reise warning
518         if not (post.parent_id.user_id.id == uid or user.karma >= 500):
519             return {'error': 'user'}
520
521         #Note: only one answer can be right.
522         correct = False if post.correct else True
523         for child in post.parent_id.child_ids:
524             if child.correct and child.id != post.id:
525                 Post.write( cr, uid, [child.id], { 'correct': False }, context=context)
526         Post.write( cr, uid, [post.id, post.parent_id.id], { 'correct': correct }, context=context)
527         return correct
528
529     @http.route('/forum/favourite_question', type='json', auth="user", multilang=True, methods=['POST'], website=True)
530     def favourite_question(self, **kwarg):
531         cr, uid, context = request.cr, request.uid, request.context
532         Post = request.registry['website.forum.post']
533         post = Post.browse(cr, uid, int(kwarg.get('post_id')), context=context)
534         favourite = False if post.user_favourite else True
535         favourite_ids = [(4, uid)]
536         if post.user_favourite:
537             favourite_ids = [(3, uid)]
538         Post.write( cr, uid, [post.id], { 'favourite_ids': favourite_ids }, context=context)
539         return favourite
540
541     @http.route('/forum/<model("website.forum"):forum>/close/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
542     def close_question(self, forum, post, **kwarg):
543         cr, uid, context = request.cr, request.uid, request.context
544         Reason = request.registry['website.forum.post.reason']
545         reason_ids = Reason.search(cr, uid, [], context=context)
546         reasons = Reason.browse(cr, uid, reason_ids, context)
547
548         values = {
549             'post': post,
550             'forum': forum,
551             'searches': kwarg,
552             'reasons': reasons,
553             'notifications': self._get_notifications(),
554         }
555         return request.website.render("website_forum.close_question", values)
556
557     @http.route('/forum/<model("website.forum"):forum>/question/close', type='http', auth="user", multilang=True, methods=['POST'], website=True)
558     def close(self, forum, **post):
559         request.registry['website.forum.post'].write( request.cr, request.uid, [int(post.get('post_id'))], {
560             'state': 'close',
561             'closed_by': request.uid,
562             'closed_date': datetime.today().strftime(tools.DEFAULT_SERVER_DATETIME_FORMAT),
563             'reason_id': post.get('reason'),
564         }, context=request.context)
565         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.get('post_id')))
566
567     @http.route('/forum/<model("website.forum"):forum>/reopen/question/<model("website.forum.post"):post>', type='http', auth="user", multilang=True, website=True)
568     def reopen(self, forum, post, **kwarg):
569         request.registry['website.forum.post'].write( request.cr, request.uid, [post.id], {
570             'state': 'active',
571         }, context=request.context)
572         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id))
573
574     @http.route('/forum/<model("website.forum"):forum>/edit/profile/<model("res.users"):user>', type='http', auth="user", multilang=True, website=True)
575     def edit_profile(self, forum, user, **kwarg):
576         cr,context = request.cr, request.context
577         country = request.registry['res.country']
578         country_ids = country.search(cr, SUPERUSER_ID, [], context=context)
579         countries = country.browse(cr, SUPERUSER_ID, country_ids, context)
580         values = {
581             'user': user,
582             'forum': forum,
583             'searches': kwarg,
584             'countries': countries,
585             'notifications': self._get_notifications(),
586         }
587         return request.website.render("website_forum.edit_profile", values)
588
589     @http.route('/forum/<model("website.forum"):forum>/save/profile', type='http', auth="user", multilang=True, website=True)
590     def save_edited_profile(self, forum, **post):
591         cr, uid, context = request.cr, request.uid, request.context
592         user = request.registry['res.users'].browse(cr, uid, int(post.get('user_id')),context=context)
593         request.registry['res.partner'].write( cr, uid, [user.partner_id.id], {
594             'name': post.get('name'),
595             'website': post.get('website'),
596             'email': post.get('email'),
597             'city': post.get('city'),
598             'country_id': post.get('country'),
599             'website_description': post.get('description'), 
600         }, context=context)
601         return werkzeug.utils.redirect("/forum/%s/user/%s" % (slug(forum),post.get('user_id')))
602
603     @http.route('/forum/<model("website.forum"):forum>/post/<model("website.forum.post"):post>/commet/<model("mail.message"):comment>/converttoanswer', type='http', auth="public", multilang=True, website=True)
604     def convert_to_answer(self, forum, post, comment, **kwarg):
605         values = {
606             'content': comment.body,
607         }
608         request.registry['mail.message'].unlink(request.cr, request.uid, [comment.id], context=request.context)
609         return self.post_answer(forum, post.parent_id and post.parent_id.id or post.id, **values)
610
611     @http.route('/forum/<model("website.forum"):forum>/post/<model("website.forum.post"):post>/converttocomment', type='http', auth="user", multilang=True, website=True)
612     def convert_to_comment(self, forum, post, **kwarg):
613         values = {
614             'comment': html2plaintext(post.content),
615         }
616         question = post.parent_id.id
617         request.registry['website.forum.post'].unlink(request.cr, SUPERUSER_ID, [post.id], context=request.context)
618         return self.post_comment(forum, question, **values)
619
620     @http.route('/forum/get_tags', type='http', auth="public", multilang=True, methods=['GET'], website=True)
621     def tag_read(self, **kwarg):
622         tags = request.registry['website.forum.tag'].search_read(request.cr, request.uid, [], ['name'], context=request.context)
623         data = [tag['name'] for tag in tags]
624         return simplejson.dumps(data)
625
626     @http.route('/forum/<model("website.forum"):forum>/question/<model("website.forum.post"):post>/subscribe', type='http', auth="public", multilang=True, website=True)
627     def subscribe(self, forum, post, **kwarg):
628         cr, uid, context = request.cr, request.uid, request.context
629         if not request.session.uid:
630             return login_redirect()
631         partner_id = request.registry['res.users'].browse(cr, uid, request.uid, context=context).partner_id.id
632         post_ids = [child.id for child in post.child_ids]
633         post_ids.append(post.id)
634         request.registry['website.forum.post'].message_subscribe( cr, uid, post_ids, [partner_id], context=context)
635         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id))
636
637     @http.route('/forum/<model("website.forum"):forum>/question/<model("website.forum.post"):post>/unsubscribe', type='http', auth="user", multilang=True, website=True)
638     def unsubscribe(self, forum, post, **kwarg):
639         cr, uid, context = request.cr, request.uid, request.context
640         partner_id = request.registry['res.users'].browse(cr, uid, request.uid, context=context).partner_id.id
641         post_ids = [child.id for child in post.child_ids]
642         post_ids.append(post.id)
643         request.registry['website.forum.post'].message_unsubscribe( cr, uid, post_ids, [partner_id], context=context)
644         return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum),post.id))