1 # -*- coding: utf-8 -*-
3 from datetime import datetime
5 import werkzeug.wrappers
9 from urllib2 import urlopen
11 from openerp import tools
12 from openerp import SUPERUSER_ID
13 from openerp.addons.web import http
14 from openerp.addons.web.controllers.main import login_redirect
15 from openerp.addons.web.http import request
16 from openerp.addons.website.controllers.main import Website as controllers
17 from openerp.addons.website.models.website import slug
18 from openerp.tools.translate import _
20 controllers = controllers()
23 class WebsiteForum(http.Controller):
27 def _get_notifications(self):
28 cr, uid, context = request.cr, request.uid, request.context
29 Message = request.registry['mail.message']
30 badge_st_id = request.registry['ir.model.data'].xmlid_to_res_id(cr, uid, 'gamification.mt_badge_granted')
32 msg_ids = Message.search(cr, uid, [('subtype_id', '=', badge_st_id), ('to_read', '=', True)], context=context)
33 msg = Message.browse(cr, uid, msg_ids, context=context)
38 def _prepare_forum_values(self, forum=None, **kwargs):
39 user = request.registry['res.users'].browse(request.cr, request.uid, request.uid, context=request.context)
40 values = {'user': user,
41 'is_public_user': user.id == request.website.user_id.id,
42 'notifications': self._get_notifications(),
43 'header': kwargs.get('header', dict()),
44 'searches': kwargs.get('searches', dict()),
45 'no_introduction_message': request.httprequest.cookies.get('no_introduction_message', False),
48 values['forum'] = forum
49 elif kwargs.get('forum_id'):
50 values['forum'] = request.registry['forum.forum'].browse(request.cr, request.uid, kwargs.pop('forum_id'), context=request.context)
55 # --------------------------------------------------
57 @http.route(['/forum'], type='http', auth="public", website=True)
58 def forum(self, **kwargs):
59 cr, uid, context = request.cr, request.uid, request.context
60 Forum = request.registry['forum.forum']
61 obj_ids = Forum.search(cr, uid, [], context=context)
62 forums = Forum.browse(cr, uid, obj_ids, context=context)
63 return request.website.render("website_forum.forum_all", {'forums': forums})
65 @http.route('/forum/new', type='http', auth="user", methods=['POST'], website=True)
66 def forum_create(self, forum_name="New Forum", **kwargs):
67 forum_id = request.registry['forum.forum'].create(request.cr, request.uid, {
69 }, context=request.context)
70 return request.redirect("/forum/%s" % forum_id)
72 @http.route('/forum/notification_read', type='json', auth="user", methods=['POST'], website=True)
73 def notification_read(self, **kwargs):
74 request.registry['mail.message'].set_message_read(request.cr, request.uid, [int(kwargs.get('notification_id'))], read=True, context=request.context)
77 @http.route(['/forum/<model("forum.forum"):forum>',
78 '/forum/<model("forum.forum"):forum>/page/<int:page>',
79 '''/forum/<model("forum.forum"):forum>/tag/<model("forum.tag", "[('forum_id','=',forum[0])]"):tag>/questions''',
80 '''/forum/<model("forum.forum"):forum>/tag/<model("forum.tag", "[('forum_id','=',forum[0])]"):tag>/questions/page/<int:page>''',
81 ], type='http', auth="public", website=True)
82 def questions(self, forum, tag=None, page=1, filters='all', sorting=None, search='', post_type='all', **post):
83 cr, uid, context = request.cr, request.uid, request.context
84 Post = request.registry['forum.post']
85 user = request.registry['res.users'].browse(cr, uid, uid, context=context)
87 domain = [('forum_id', '=', forum.id), ('parent_id', '=', False), ('state', '=', 'active')]
89 domain += ['|', ('name', 'ilike', search), ('content', 'ilike', search)]
91 domain += [('tag_ids', 'in', tag.id)]
92 if filters == 'unanswered':
93 domain += [('child_ids', '=', False)]
94 elif filters == 'followed':
95 domain += [('message_follower_ids', '=', user.partner_id.id)]
99 if post_type == 'link':
100 domain += [('type', '=', 'link')]
101 elif post_type == 'question':
102 domain += [('type', '=', 'question')]
103 elif post_type == 'discussion':
104 domain += [('type', '=', 'discussion')]
107 sorting = forum.default_order
109 question_count = Post.search(cr, uid, domain, count=True, context=context)
111 url = "/forum/%s/tag/%s/questions" % (slug(forum), slug(tag))
113 url = "/forum/%s" % slug(forum)
119 url_args['search'] = search
121 url_args['filters'] = filters
122 pager = request.website.pager(url=url, total=question_count, page=page,
123 step=self._post_per_page, scope=self._post_per_page,
126 obj_ids = Post.search(cr, uid, domain, limit=self._post_per_page, offset=pager['offset'], order=sorting, context=context)
127 question_ids = Post.browse(cr, uid, obj_ids, context=context)
129 values = self._prepare_forum_values(forum=forum, searches=post)
131 'main_object': tag or forum,
132 'question_ids': question_ids,
133 'question_count': question_count,
139 'post_type': post_type,
141 return request.website.render("website_forum.forum_index", values)
143 @http.route(['/forum/<model("forum.forum"):forum>/faq'], type='http', auth="public", website=True)
144 def forum_faq(self, forum, **post):
145 values = self._prepare_forum_values(forum=forum, searches=dict(), header={'is_guidelines': True}, **post)
146 return request.website.render("website_forum.faq", values)
148 @http.route('/forum/get_tags', type='http', auth="public", methods=['GET'], website=True)
149 def tag_read(self, **post):
150 tags = request.registry['forum.tag'].search_read(request.cr, request.uid, [], ['name'], context=request.context)
151 data = [tag['name'] for tag in tags]
152 return simplejson.dumps(data)
154 @http.route(['/forum/<model("forum.forum"):forum>/tag'], type='http', auth="public", website=True)
155 def tags(self, forum, page=1, **post):
156 cr, uid, context = request.cr, request.uid, request.context
157 Tag = request.registry['forum.tag']
158 obj_ids = Tag.search(cr, uid, [('forum_id', '=', forum.id), ('posts_count', '>', 0)], limit=None, order='posts_count DESC', context=context)
159 tags = Tag.browse(cr, uid, obj_ids, context=context)
160 values = self._prepare_forum_values(forum=forum, searches={'tags': True}, **post)
163 'main_object': forum,
165 return request.website.render("website_forum.tag", values)
168 # --------------------------------------------------
170 @http.route('/forum/get_url_title', type='json', auth="user", methods=['POST'], website=True)
171 def get_url_title(self, **kwargs):
172 arch = lxml.html.parse(urlopen(kwargs.get('url')))
173 return arch.find(".//title").text
175 @http.route(['''/forum/<model("forum.forum"):forum>/question/<model("forum.post", "[('forum_id','=',forum[0]),('parent_id','=',False)]"):question>'''], type='http', auth="public", website=True)
176 def question(self, forum, question, **post):
177 cr, uid, context = request.cr, request.uid, request.context
178 # increment view counter
179 request.registry['forum.post'].set_viewed(cr, SUPERUSER_ID, [question.id], context=context)
181 if question.parent_id:
182 redirect_url = "/forum/%s/question/%s" % (slug(forum), slug(question.parent_id))
183 return werkzeug.utils.redirect(redirect_url, 301)
186 values = self._prepare_forum_values(forum=forum, searches=post)
188 'main_object': question,
189 'question': question,
190 'header': {'question_data': True},
192 'reversed': reversed,
194 return request.website.render("website_forum.post_description_full", values)
196 @http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/toggle_favourite', type='json', auth="user", methods=['POST'], website=True)
197 def question_toggle_favorite(self, forum, question, **post):
198 if not request.session.uid:
199 return {'error': 'anonymous_user'}
200 # TDE: add check for not public
201 favourite = False if question.user_favourite else True
203 favourite_ids = [(4, request.uid)]
205 favourite_ids = [(3, request.uid)]
206 request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'favourite_ids': favourite_ids}, context=request.context)
209 @http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/ask_for_close', type='http', auth="user", methods=['POST'], website=True)
210 def question_ask_for_close(self, forum, question, **post):
211 cr, uid, context = request.cr, request.uid, request.context
212 Reason = request.registry['forum.post.reason']
213 reason_ids = Reason.search(cr, uid, [], context=context)
214 reasons = Reason.browse(cr, uid, reason_ids, context)
216 values = self._prepare_forum_values(**post)
218 'question': question,
219 'question': question,
223 return request.website.render("website_forum.close_post", values)
225 @http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/edit_answer', type='http', auth="user", website=True)
226 def question_edit_answer(self, forum, question, **kwargs):
227 for record in question.child_ids:
228 if record.create_uid.id == request.uid:
231 return werkzeug.utils.redirect("/forum/%s/post/%s/edit" % (slug(forum), slug(answer)))
233 @http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/close', type='http', auth="user", methods=['POST'], website=True)
234 def question_close(self, forum, question, **post):
235 request.registry['forum.post'].close(request.cr, request.uid, [question.id], reason_id=int(post.get('reason_id', False)), context=request.context)
236 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
238 @http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/reopen', type='http', auth="user", methods=['POST'], website=True)
239 def question_reopen(self, forum, question, **kwarg):
240 request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'state': 'active'}, context=request.context)
241 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
243 @http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/delete', type='http', auth="user", methods=['POST'], website=True)
244 def question_delete(self, forum, question, **kwarg):
245 request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'active': False}, context=request.context)
246 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
248 @http.route('/forum/<model("forum.forum"):forum>/question/<model("forum.post"):question>/undelete', type='http', auth="user", methods=['POST'], website=True)
249 def question_undelete(self, forum, question, **kwarg):
250 request.registry['forum.post'].write(request.cr, request.uid, [question.id], {'active': True}, context=request.context)
251 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
254 # --------------------------------------------------
256 @http.route(['/forum/<model("forum.forum"):forum>/<post_type>'], type='http', auth="public", website=True)
257 def forum_post(self, forum, post_type, **post):
258 if not request.session.uid:
259 return login_redirect()
260 cr, uid, context = request.cr, request.uid, request.context
261 user = request.registry['res.users'].browse(cr, SUPERUSER_ID, uid, context=context)
262 if not user.email or not tools.single_email_re.match(user.email):
263 return werkzeug.utils.redirect("/forum/%s/user/%s/edit?email_required=1" % (slug(forum), uid))
264 values = self._prepare_forum_values(forum=forum, searches={}, header={'ask_hide': True})
265 return request.website.render("website_forum.%s" % post_type, values)
267 @http.route(['/forum/<model("forum.forum"):forum>/<post_type>/new',
268 '/forum/<model("forum.forum"):forum>/<model("forum.post"):post_parent>/reply']
269 , type='http', auth="public", methods=['POST'], website=True)
270 def post_create(self, forum, post_parent='', post_type='', **post):
271 cr, uid, context = request.cr, request.uid, request.context
272 if not request.session.uid:
273 return login_redirect()
276 Tag = request.registry['forum.tag']
277 if post.get('post_tags', False) and post.get('post_tags').strip('[]'):
278 tags = post.get('post_tags').strip('[]').replace('"', '').split(",")
280 tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context)
282 post_tag_ids.append((4, tag_ids[0]))
284 post_tag_ids.append((0, 0, {'name': tag, 'forum_id': forum.id}))
286 new_question_id = request.registry['forum.post'].create(cr, uid, {
287 'forum_id': forum.id,
288 'name': post.get('post_name', ''),
289 'content': post.get('content'),
290 'content_link': post.get('content_link', False),
291 'parent_id': post_parent and post_parent.id or False,
292 'tag_ids': post_tag_ids,
293 'type': post_parent and post_parent.type or post_type,
295 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), post_parent and slug(post_parent) or new_question_id))
297 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/comment', type='http', auth="public", methods=['POST'], website=True)
298 def post_comment(self, forum, post, **kwargs):
299 if not request.session.uid:
300 return login_redirect()
301 question = post.parent_id if post.parent_id else post
302 cr, uid, context = request.cr, request.uid, request.context
303 if kwargs.get('comment') and post.forum_id.id == forum.id:
304 # TDE FIXME: check that post_id is the question or one of its answers
305 request.registry['forum.post'].message_post(
307 body=kwargs.get('comment'),
309 subtype='mt_comment',
310 context=dict(context, mail_create_nosubcribe=True))
311 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
313 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/toggle_correct', type='json', auth="public", website=True)
314 def post_toggle_correct(self, forum, post, **kwargs):
315 cr, uid, context = request.cr, request.uid, request.context
316 if post.parent_id is False:
317 return request.redirect('/')
318 if not request.session.uid:
319 return {'error': 'anonymous_user'}
321 # set all answers to False, only one can be accepted
322 request.registry['forum.post'].write(cr, uid, [c.id for c in post.parent_id.child_ids if not c.id == post.id], {'is_correct': False}, context=context)
323 request.registry['forum.post'].write(cr, uid, [post.id], {'is_correct': not post.is_correct}, context=context)
324 return post.is_correct
326 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/delete', type='http', auth="user", methods=['POST'], website=True)
327 def post_delete(self, forum, post, **kwargs):
328 question = post.parent_id
329 request.registry['forum.post'].unlink(request.cr, request.uid, [post.id], context=request.context)
331 werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
332 return werkzeug.utils.redirect("/forum/%s" % slug(forum))
334 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/edit', type='http', auth="user", website=True)
335 def post_edit(self, forum, post, **kwargs):
337 for tag_name in post.tag_ids:
338 tags += tag_name.name + ","
339 values = self._prepare_forum_values(forum=forum)
343 'is_answer': bool(post.parent_id),
346 return request.website.render("website_forum.edit_post", values)
348 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/save', type='http', auth="user", methods=['POST'], website=True)
349 def post_save(self, forum, post, **kwargs):
350 cr, uid, context = request.cr, request.uid, request.context
352 if kwargs.get('post_tag') and kwargs.get('post_tag').strip('[]'):
353 Tag = request.registry['forum.tag']
354 tags = kwargs.get('post_tag').strip('[]').replace('"', '').split(",")
356 tag_ids = Tag.search(cr, uid, [('name', '=', tag)], context=context)
360 new_tag = Tag.create(cr, uid, {'name': tag, 'forum_id': forum.id}, context=context)
361 post_tags.append(new_tag)
363 'tag_ids': [(6, 0, post_tags)],
364 'name': kwargs.get('post_name'),
365 'content': kwargs.get('content'),
367 request.registry['forum.post'].write(cr, uid, [post.id], vals, context=context)
368 question = post.parent_id if post.parent_id else post
369 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
371 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/upvote', type='json', auth="public", website=True)
372 def post_upvote(self, forum, post, **kwargs):
373 if not request.session.uid:
374 return {'error': 'anonymous_user'}
375 if request.uid == post.create_uid.id:
376 return {'error': 'own_post'}
377 upvote = True if not post.user_vote > 0 else False
378 return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=upvote, context=request.context)
380 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/downvote', type='json', auth="public", website=True)
381 def post_downvote(self, forum, post, **kwargs):
382 if not request.session.uid:
383 return {'error': 'anonymous_user'}
384 if request.uid == post.create_uid.id:
385 return {'error': 'own_post'}
386 upvote = True if post.user_vote < 0 else False
387 return request.registry['forum.post'].vote(request.cr, request.uid, [post.id], upvote=upvote, context=request.context)
390 # --------------------------------------------------
392 @http.route(['/forum/<model("forum.forum"):forum>/users',
393 '/forum/<model("forum.forum"):forum>/users/page/<int:page>'],
394 type='http', auth="public", website=True)
395 def users(self, forum, page=1, **searches):
396 cr, uid, context = request.cr, request.uid, request.context
397 User = request.registry['res.users']
400 tag_count = User.search(cr, SUPERUSER_ID, [('karma', '>', 1), ('website_published', '=', True)], count=True, context=context)
401 pager = request.website.pager(url="/forum/%s/users" % slug(forum), total=tag_count, page=page, step=step, scope=30)
403 obj_ids = User.search(cr, SUPERUSER_ID, [('karma', '>', 1), ('website_published', '=', True)], limit=step, offset=pager['offset'], order='karma DESC', context=context)
404 # put the users in block of 3 to display them as a table
405 users = [[] for i in range(len(obj_ids)/3+1)]
406 for index, user in enumerate(User.browse(cr, SUPERUSER_ID, obj_ids, context=context)):
407 users[index/3].append(user)
408 searches['users'] = 'True'
410 values = self._prepare_forum_values(forum=forum, searches=searches)
413 'main_object': forum,
414 'notifications': self._get_notifications(),
418 return request.website.render("website_forum.users", values)
420 @http.route(['/forum/<model("forum.forum"):forum>/partner/<int:partner_id>'], type='http', auth="public", website=True)
421 def open_partner(self, forum, partner_id=0, **post):
422 cr, uid, context = request.cr, request.uid, request.context
424 partner = request.registry['res.partner'].browse(cr, SUPERUSER_ID, partner_id, context=context)
425 if partner.exists() and partner.user_ids:
426 return werkzeug.utils.redirect("/forum/%s/user/%d" % (slug(forum), partner.user_ids[0].id))
427 return werkzeug.utils.redirect("/forum/%s" % slug(forum))
429 @http.route(['/forum/user/<int:user_id>/avatar'], type='http', auth="public", website=True)
430 def user_avatar(self, user_id=0, **post):
431 cr, uid, context = request.cr, request.uid, request.context
432 response = werkzeug.wrappers.Response()
433 User = request.registry['res.users']
434 Website = request.registry['website']
435 user = User.browse(cr, SUPERUSER_ID, user_id, context=context)
436 if not user.exists() or (user_id != request.session.uid and user.karma < 1):
437 return Website._image_placeholder(response)
438 return Website._image(cr, SUPERUSER_ID, 'res.users', user.id, 'image', response)
440 @http.route(['/forum/<model("forum.forum"):forum>/user/<int:user_id>'], type='http', auth="public", website=True)
441 def open_user(self, forum, user_id=0, **post):
442 cr, uid, context = request.cr, request.uid, request.context
443 User = request.registry['res.users']
444 Post = request.registry['forum.post']
445 Vote = request.registry['forum.post.vote']
446 Activity = request.registry['mail.message']
447 Followers = request.registry['mail.followers']
448 Data = request.registry["ir.model.data"]
450 user = User.browse(cr, SUPERUSER_ID, user_id, context=context)
451 if not user.exists() or user.karma < 1:
452 return werkzeug.utils.redirect("/forum/%s" % slug(forum))
453 values = self._prepare_forum_values(forum=forum, **post)
454 if user_id != request.session.uid and not user.website_published:
455 return request.website.render("website_forum.private_profile", values)
456 # questions and answers by user
457 user_questions, user_answers = [], []
458 user_question_ids = Post.search(cr, uid, [
459 ('parent_id', '=', False),
460 ('forum_id', '=', forum.id), ('create_uid', '=', user.id),
461 ], order='create_date desc', context=context)
462 count_user_questions = len(user_question_ids)
463 # displaying only the 20 most recent questions
464 user_questions = Post.browse(cr, uid, user_question_ids[:20], context=context)
466 user_answer_ids = Post.search(cr, uid, [
467 ('parent_id', '!=', False),
468 ('forum_id', '=', forum.id), ('create_uid', '=', user.id),
469 ], order='create_date desc', context=context)
470 count_user_answers = len(user_answer_ids)
471 # displaying only the 20 most recent answers
472 user_answers = Post.browse(cr, uid, user_answer_ids[:20], context=context)
474 # showing questions which user following
475 obj_ids = Followers.search(cr, SUPERUSER_ID, [('res_model', '=', 'forum.post'), ('partner_id', '=', user.partner_id.id)], context=context)
476 post_ids = [follower.res_id for follower in Followers.browse(cr, SUPERUSER_ID, obj_ids, context=context)]
477 que_ids = Post.search(cr, uid, [('id', 'in', post_ids), ('forum_id', '=', forum.id), ('parent_id', '=', False)], context=context)
478 followed = Post.browse(cr, uid, que_ids, context=context)
480 #showing Favourite questions of user.
481 fav_que_ids = Post.search(cr, uid, [('favourite_ids', '=', user.id), ('forum_id', '=', forum.id), ('parent_id', '=', False)], context=context)
482 favourite = Post.browse(cr, uid, fav_que_ids, context=context)
484 #votes which given on users questions and answers.
485 data = Vote.read_group(cr, uid, [('forum_id', '=', forum.id), ('recipient_id', '=', user.id)], ["vote"], groupby=["vote"], context=context)
486 up_votes, down_votes = 0, 0
488 if rec['vote'] == '1':
489 up_votes = rec['vote_count']
490 elif rec['vote'] == '-1':
491 down_votes = rec['vote_count']
493 #Votes which given by users on others questions and answers.
494 post_votes = Vote.search(cr, uid, [('user_id', '=', user.id)], context=context)
495 vote_ids = Vote.browse(cr, uid, post_votes, context=context)
498 model, comment = Data.get_object_reference(cr, uid, 'mail', 'mt_comment')
499 activity_ids = Activity.search(cr, uid, [('res_id', 'in', user_question_ids+user_answer_ids), ('model', '=', 'forum.post'), ('subtype_id', '!=', comment)], order='date DESC', limit=100, context=context)
500 activities = Activity.browse(cr, uid, activity_ids, context=context)
503 for act in activities:
504 posts[act.res_id] = True
505 posts_ids = Post.browse(cr, uid, posts.keys(), context=context)
506 posts = dict(map(lambda x: (x.id, (x.parent_id or x, x.parent_id and x or False)), posts_ids))
508 # TDE CLEANME MASTER: couldn't it be rewritten using a 'menu' key instead of one key for each menu ?
510 post['my_profile'] = True
519 'questions': user_questions,
520 'count_questions': count_user_questions,
521 'answers': user_answers,
522 'count_answers': count_user_answers,
523 'followed': followed,
524 'favourite': favourite,
525 'up_votes': up_votes,
526 'down_votes': down_votes,
527 'activities': activities,
529 'vote_post': vote_ids,
531 return request.website.render("website_forum.user_detail_full", values)
533 @http.route('/forum/<model("forum.forum"):forum>/user/<model("res.users"):user>/edit', type='http', auth="user", website=True)
534 def edit_profile(self, forum, user, **kwargs):
535 country = request.registry['res.country']
536 country_ids = country.search(request.cr, SUPERUSER_ID, [], context=request.context)
537 countries = country.browse(request.cr, SUPERUSER_ID, country_ids, context=request.context)
538 values = self._prepare_forum_values(forum=forum, searches=kwargs)
540 'email_required': kwargs.get('email_required'),
541 'countries': countries,
542 'notifications': self._get_notifications(),
544 return request.website.render("website_forum.edit_profile", values)
546 @http.route('/forum/<model("forum.forum"):forum>/user/<model("res.users"):user>/save', type='http', auth="user", methods=['POST'], website=True)
547 def save_edited_profile(self, forum, user, **kwargs):
549 'name': kwargs.get('name'),
550 'website': kwargs.get('website'),
551 'email': kwargs.get('email'),
552 'city': kwargs.get('city'),
553 'country_id': int(kwargs.get('country')) if kwargs.get('country') else False,
554 'website_description': kwargs.get('description'),
556 if request.uid == user.id: # the controller allows to edit only its own privacy settings; use partner management for other cases
557 values['website_published'] = kwargs.get('website_published') == 'True'
558 request.registry['res.users'].write(request.cr, request.uid, [user.id], values, context=request.context)
559 return werkzeug.utils.redirect("/forum/%s/user/%d" % (slug(forum), user.id))
562 # --------------------------------------------------
564 @http.route('/forum/<model("forum.forum"):forum>/badge', type='http', auth="public", website=True)
565 def badges(self, forum, **searches):
566 cr, uid, context = request.cr, request.uid, request.context
567 Badge = request.registry['gamification.badge']
568 badge_ids = Badge.search(cr, SUPERUSER_ID, [('challenge_ids.category', '=', 'forum')], context=context)
569 badges = Badge.browse(cr, uid, badge_ids, context=context)
570 badges = sorted(badges, key=lambda b: b.stat_count_distinct, reverse=True)
571 values = self._prepare_forum_values(forum=forum, searches={'badges': True})
575 return request.website.render("website_forum.badge", values)
577 @http.route(['''/forum/<model("forum.forum"):forum>/badge/<model("gamification.badge"):badge>'''], type='http', auth="public", website=True)
578 def badge_users(self, forum, badge, **kwargs):
579 user_ids = [badge_user.user_id.id for badge_user in badge.owner_ids]
580 users = request.registry['res.users'].browse(request.cr, SUPERUSER_ID, user_ids, context=request.context)
581 values = self._prepare_forum_values(forum=forum, searches={'badges': True})
586 return request.website.render("website_forum.badge_user", values)
589 # --------------------------------------------------
591 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/comment/<model("mail.message"):comment>/convert_to_answer', type='http', auth="user", methods=['POST'], website=True)
592 def convert_comment_to_answer(self, forum, post, comment, **kwarg):
593 new_post_id = request.registry['forum.post'].convert_comment_to_answer(request.cr, request.uid, comment.id, context=request.context)
595 return werkzeug.utils.redirect("/forum/%s" % slug(forum))
596 post = request.registry['forum.post'].browse(request.cr, request.uid, new_post_id, context=request.context)
597 question = post.parent_id if post.parent_id else post
598 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
600 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/convert_to_comment', type='http', auth="user", methods=['POST'], website=True)
601 def convert_answer_to_comment(self, forum, post, **kwarg):
602 question = post.parent_id
603 new_msg_id = request.registry['forum.post'].convert_answer_to_comment(request.cr, request.uid, post.id, context=request.context)
605 return werkzeug.utils.redirect("/forum/%s" % slug(forum))
606 return werkzeug.utils.redirect("/forum/%s/question/%s" % (slug(forum), slug(question)))
608 @http.route('/forum/<model("forum.forum"):forum>/post/<model("forum.post"):post>/comment/<model("mail.message"):comment>/delete', type='json', auth="user", website=True)
609 def delete_comment(self, forum, post, comment, **kwarg):
610 if not request.session.uid:
611 return {'error': 'anonymous_user'}
612 return request.registry['forum.post'].unlink_comment(request.cr, request.uid, post.id, comment.id, context=request.context)