main.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import http, _
  4. from odoo.http import request
  5. from odoo.addons.im_livechat.controllers.main import LivechatController
  6. class WebsiteLivechat(LivechatController):
  7. @http.route('/livechat', type='http', auth="public", website=True, sitemap=True)
  8. def channel_list(self, **kw):
  9. # display the list of the channel
  10. channels = request.env['im_livechat.channel'].search([('website_published', '=', True)])
  11. values = {
  12. 'channels': channels
  13. }
  14. return request.render('website_livechat.channel_list_page', values)
  15. @http.route('/livechat/channel/<model("im_livechat.channel"):channel>', type='http', auth='public', website=True, sitemap=True)
  16. def channel_rating(self, channel, **kw):
  17. # get the last 100 ratings and the repartition per grade
  18. domain = [
  19. ('res_model', '=', 'mail.channel'), ('res_id', 'in', channel.sudo().channel_ids.ids),
  20. ('consumed', '=', True), ('rating', '>=', 1),
  21. ]
  22. ratings = request.env['rating.rating'].sudo().search(domain, order='create_date desc', limit=100)
  23. repartition = channel.sudo().channel_ids.rating_get_grades(domain=domain)
  24. # compute percentage
  25. percentage = dict.fromkeys(['great', 'okay', 'bad'], 0)
  26. for grade in repartition:
  27. percentage[grade] = round(repartition[grade] * 100.0 / sum(repartition.values()), 1) if sum(repartition.values()) else 0
  28. # filter only on the team users that worked on the last 100 ratings and get their detailed stat
  29. ratings_per_partner = {partner_id: dict(great=0, okay=0, bad=0)
  30. for partner_id in ratings.mapped('rated_partner_id.id')}
  31. total_ratings_per_partner = dict.fromkeys(ratings.mapped('rated_partner_id.id'), 0)
  32. # keep 10 for backward compatibility
  33. rating_texts = {10: 'great', 5: 'great', 3: 'okay', 1: 'bad'}
  34. for rating in ratings:
  35. partner_id = rating.rated_partner_id.id
  36. if partner_id:
  37. ratings_per_partner[partner_id][rating_texts[rating.rating]] += 1
  38. total_ratings_per_partner[partner_id] += 1
  39. for partner_id, rating in ratings_per_partner.items():
  40. for k, v in ratings_per_partner[partner_id].items():
  41. ratings_per_partner[partner_id][k] = round(100 * v / total_ratings_per_partner[partner_id], 1)
  42. # the value dict to render the template
  43. values = {
  44. 'main_object': channel,
  45. 'channel': channel,
  46. 'ratings': ratings,
  47. 'team': channel.sudo().user_ids,
  48. 'percentage': percentage,
  49. 'ratings_per_user': ratings_per_partner
  50. }
  51. return request.render("website_livechat.channel_page", values)
  52. @http.route('/im_livechat/get_session', type="json", auth='public', cors="*")
  53. def get_session(self, channel_id, anonymous_name, previous_operator_id=None, chatbot_script_id=None, persisted=True, **kwargs):
  54. """ Override to use visitor name instead of 'Visitor' whenever a visitor start a livechat session. """
  55. visitor_sudo = request.env['website.visitor']._get_visitor_from_request()
  56. if visitor_sudo:
  57. anonymous_name = visitor_sudo.with_context(lang=visitor_sudo.lang_id.code).display_name
  58. return super(WebsiteLivechat, self).get_session(channel_id, anonymous_name, previous_operator_id=previous_operator_id, chatbot_script_id=chatbot_script_id, persisted=persisted, **kwargs)
  59. def _livechat_templates_get(self):
  60. return super(WebsiteLivechat, self)._livechat_templates_get() + [
  61. 'website_livechat/static/src/legacy/widgets/public_livechat_floating_text_view/public_livechat_floating_text_view.xml',
  62. ]