"
- % (rating.sudo().partner_id.name, _('rated it'), rate),
- subtype='mail.mt_comment',
- author_id=rating.partner_id and rating.partner_id.id or None # None will set the default author in mail_thread.py
- )
- return rating
+ access_token = fields.Char('Security Token', default=new_access_token, help="Access token to set the rating of the value")
+ consumed = fields.Boolean(string="Filled Rating", help="Enabled if the rating has been filled.")
@api.multi
def reset(self):
for record in self:
record.write({
- 'rating': -1,
+ 'rating': 0,
'access_token': record.new_access_token(),
- 'feedback' : False
+ 'feedback': False,
+ 'consumed': False,
})
-
class RatingMixin(models.AbstractModel):
-
_name = 'rating.mixin'
_description = "Rating Mixin"
rating_ids = fields.One2many('rating.rating', 'res_id', string='Rating', domain=lambda self: [('res_model', '=', self._name)])
+ rating_last_value = fields.Float('Last Value', related='rating_ids.rating', store=True)
+ rating_count = fields.Integer('Rating count', compute="_compute_rating_count")
@api.multi
- def rating_send_request(self, template, partner_id, rated_partner_id, reuse_rating=True):
- """ This method create (empty) rating objects for the current recordsets
- and send this request by mail (given email template) with the given
- rated_partner_id and partner_id as recipient and sender of the email.
- :param template : the email template to send. The res_model of the
- template must be 'rating.rating'.
- :type template : mail.template
- :param res_model : model name of the object to rated_partner_id
- :type res_model : string
- :param res_id : id the record to rate
- :type res_id : int
- :param partner_id : the recipient partner
- :type partner : res.partner
- :param rated_partner_id : the sender partner
- :type rated_partner_id : res.partner
- :param reuse_rating : if True, the rating of the current objects will
- be reset. Otherwise a new rating will be create
- :type reuse_rating : boolean
- """
- if not rated_partner_id.email or not partner_id.email:
- return False
- Rating = self.env['rating.rating']
- res_model = self._name
+ def _compute_rating_count(self):
+ read_group_res = self.env['rating.rating'].read_group(
+ [('res_model', '=', self._name), ('res_id', 'in', self.ids), ('consumed', '=', True)],
+ ['res_id'], groupby=['res_id'])
+ result = dict.fromkeys(self.ids, 0)
+ for data in read_group_res:
+ result[data['res_id']] += data['res_id_count']
for record in self:
- res_id = record.id
- values = {
- 'res_model': res_model,
- 'res_id': res_id,
- 'partner_id': partner_id.id,
- 'rated_partner_id': rated_partner_id.id
- }
- if reuse_rating:
- # search the existing rating for the given res_model/res_id
- rating = Rating.search([('res_id', '=', res_id), ('res_model', '=', res_model), ('partner_id', '=', partner_id.id)], limit=1)
- if rating: # reset the rating
- rating.reset()
- else: # create a new one
- rating = Rating.create(values)
- else:
- rating = Rating.create(values)
- # send the mail
- template.send_mail(rating.id, force_send=True)
+ record.rating_count = result[record.id]
+
+ def rating_get_partner_id(self):
+ if hasattr(self, 'partner_id') and self.partner_id:
+ return self.partner_id
+ return self.env['res.partner']
+
+ def rating_get_rated_partner_id(self):
+ if hasattr(self, 'user_id') and self.user_id.partner_id:
+ return self.user_id.partner_id
+ return self.env['res.partner']
+
+ def rating_get_access_token(self, partner=None):
+ if not partner:
+ partner = self.rating_get_partner_id()
+ ratings = self.rating_ids.filtered(lambda x: x.partner_id == partner.id and not x.consumed)
+ if not ratings:
+ rating = self.env['rating.rating'].create({'partner_id': partner.id, 'res_model': self._name, 'res_id': self.id})
+ else:
+ rating = ratings[0]
+ return rating.access_token
+
+ @api.multi
+ def rating_send_request(self, template, partner=None, rated_partner=None, reuse_rating=True, force_send=True):
+ """ This method send rating request by email, using a template given
+ in parameter. """
+ for record in self:
+ template.send_mail(record.id, force_send=force_send)
+
+ @api.multi
+ def rating_apply(self, rate, token=None, feedback=None, subtype=None):
+ """ Apply a rating given a token. If the current model inherits from
+ mail.thread mixing, a message is posted on its chatter.
+ :param rate : the rating value to apply
+ :type rate : float
+ :param token : access token
+ :param feedback : additional feedback
+ :type feedback : string
+ :param subtype : subtype for mail
+ :type subtype : string
+ :returns rating.rating record
+ """
+ Rating, rating = self.env['rating.rating'], None
+ if token:
+ rating = self.env['rating.rating'].search([('access_token', '=', token), ('consumed', '=', False)], limit=1)
+ else:
+ rating = Rating.search([('res_model', '=', self._name), ('res_id', '=', self.ids[0]), ('consumed', '=', False)], limit=1)
+ if rating:
+ rating.write({'rating': rate, 'feedback': feedback, 'consumed': True})
+ if hasattr(self, 'message_post'):
+ self.message_post(
+ body="
%s"
+ % (rate, 'Your vote has been taken into account.
-It was
There is no rating for this object at the moment.
diff --git a/addons/rating_project/data/project_data.xml b/addons/rating_project/data/project_data.xml
index 5fa22af596f..1332a726e39 100644
--- a/addons/rating_project/data/project_data.xml
+++ b/addons/rating_project/data/project_data.xml
@@ -1,51 +1,109 @@
-
If you have any more questions or comments, do not hesitate to reply to this e-mail.
-
Please, help us to improve ourself by rating the service we provide.
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- Thank you
- ${object.rated_partner_id.name or ''}
-