49 lines
2.1 KiB
Python
49 lines
2.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import api, fields, models
|
|
|
|
|
|
class HelpdeskTicket(models.Model):
|
|
_inherit = 'helpdesk.ticket'
|
|
|
|
rating_stars_html = fields.Html(
|
|
string='Rating Stars',
|
|
compute='_compute_rating_stars_html',
|
|
help='HTML representation of rating stars'
|
|
)
|
|
|
|
@api.depends('rating_ids', 'rating_ids.rating', 'rating_ids.create_date', 'rating_count')
|
|
def _compute_rating_stars_html(self):
|
|
"""Compute HTML representation of rating stars"""
|
|
# Unicode star characters
|
|
filled_star = '★' # U+2605 BLACK STAR
|
|
empty_star = '☆' # U+2606 WHITE STAR
|
|
|
|
for ticket in self:
|
|
# Flush to ensure rating_ids is up to date
|
|
ticket.flush_recordset()
|
|
|
|
# Use rating_ids which is a One2many field that exists on helpdesk.ticket
|
|
# Filter for ratings with value > 0 and sort by create_date descending
|
|
valid_ratings = ticket.rating_ids.filtered(lambda r: r.rating > 0)
|
|
if valid_ratings:
|
|
# Get the most recent rating
|
|
rating = valid_ratings.sorted(key=lambda r: r.create_date or fields.Datetime.now(), reverse=True)[0]
|
|
|
|
# Calculate filled and empty stars
|
|
rating_int = round(rating.rating)
|
|
filled_count = rating_int
|
|
empty_count = 5 - rating_int
|
|
|
|
# Generate HTML with stars
|
|
html = '<span class="o_rating_stars">'
|
|
html += '<span class="o_rating_stars_filled">' + (filled_star * filled_count) + '</span>'
|
|
html += '<span class="o_rating_stars_empty">' + (empty_star * empty_count) + '</span>'
|
|
html += '</span>'
|
|
ticket.rating_stars_html = html
|
|
else:
|
|
# No rating or zero rating - display "Not Rated" or empty stars
|
|
ticket.rating_stars_html = '<span class="o_rating_stars o_rating_not_rated">' + \
|
|
'<span class="o_rating_stars_empty">' + (empty_star * 5) + '</span>' + \
|
|
'</span>'
|