summaryrefslogtreecommitdiff
path: root/addons/website_slides/models/slide_slide.py
diff options
context:
space:
mode:
authorRaphael Collet <rco@odoo.com>2019-08-14 07:35:49 +0000
committerDenis Ledoux <dle@odoo.com>2019-08-20 12:43:59 +0000
commit9920f20e4c7753bc17bea71dea3a90f7de687196 (patch)
tree6a3d0258c10a8ccb1d85949ddafdeb070de78121 /addons/website_slides/models/slide_slide.py
parente28d3aa4d3851d72deba08c236072d002f83216a (diff)
[IMP] models: ORM speedup
This branch is the combination of several optimizations in the ORM: * store field values once in the cache: the cache reflects more faithfully the database, only fields that explicitly depend on the context have an extra indirection in the cache; * delay recomputations by default: use method `recompute` to explicitly flush out pending recomputations; * delay updates in method `write`: updates are stored in a data structure that can be flushed efficiently to the database with method `flush` (which also flush out recomputations); * make method `modified` take advantage of inverse fields to inverse dependencies; * filter records by evaluating a domain on records in Python; * a computed field with `readonly=False` behaves like a normal field with an onchange method; * computed fields are computed in superuser mode by default. Work done by Toufik Ben Jaa, Raphael Collet, Denis Ledoux and Fabien Pinckaers. closes odoo/odoo#35659 Signed-off-by: Denis Ledoux <beledouxdenis@users.noreply.github.com>
Diffstat (limited to 'addons/website_slides/models/slide_slide.py')
-rw-r--r--addons/website_slides/models/slide_slide.py43
1 files changed, 36 insertions, 7 deletions
diff --git a/addons/website_slides/models/slide_slide.py b/addons/website_slides/models/slide_slide.py
index d4ab74afc37..e0e19f4eadd 100644
--- a/addons/website_slides/models/slide_slide.py
+++ b/addons/website_slides/models/slide_slide.py
@@ -33,6 +33,24 @@ class SlidePartnerRelation(models.Model):
completed = fields.Boolean('Completed')
quiz_attempts_count = fields.Integer('Quiz attempts count', default=0)
+ def create(self, values):
+ res = super(SlidePartnerRelation, self).create(values)
+ completed = res.filtered('completed')
+ if completed:
+ completed._completed_callback()
+ return res
+
+ def write(self, values):
+ res = super(SlidePartnerRelation, self).write(values)
+ if values.get('completed'):
+ self._completed_callback()
+ return res
+
+ def _completed_callback(self):
+ self.env['slide.channel.partner'].search([
+ ('channel_id', 'in', self.channel_id.ids),
+ ('partner_id', 'in', self.partner_id.ids),
+ ])._compute_completion()
class SlideLink(models.Model):
_name = 'slide.slide.link'
@@ -115,7 +133,7 @@ class Slide(models.Model):
string='Subscribers', groups='website.group_website_publisher')
slide_partner_ids = fields.One2many('slide.slide.partner', 'slide_id', string='Subscribers information', groups='website.group_website_publisher')
user_membership_id = fields.Many2one(
- 'slide.slide.partner', string="Subscriber information", compute='_compute_user_membership_id',
+ 'slide.slide.partner', string="Subscriber information", compute='_compute_user_membership_id', compute_sudo=False,
help="Subscriber information for the current logged in user")
# Quiz related fields
question_ids = fields.One2many("slide.question", "slide_id", string="Questions")
@@ -143,9 +161,9 @@ class Slide(models.Model):
# website
website_id = fields.Many2one(related='channel_id.website_id', readonly=True)
date_published = fields.Datetime('Publish Date')
- likes = fields.Integer('Likes', compute='_compute_user_info', store=True)
- dislikes = fields.Integer('Dislikes', compute='_compute_user_info', store=True)
- user_vote = fields.Integer('User vote', compute='_compute_user_info')
+ likes = fields.Integer('Likes', compute='_compute_user_info', store=True, compute_sudo=False)
+ dislikes = fields.Integer('Dislikes', compute='_compute_user_info', store=True, compute_sudo=False)
+ user_vote = fields.Integer('User vote', compute='_compute_user_info', compute_sudo=False)
embed_code = fields.Text('Embed Code', readonly=True, compute='_compute_embed_code')
# views
embedcount_ids = fields.One2many('slide.embed', 'slide_id', string="Embed Count")
@@ -178,9 +196,10 @@ class Slide(models.Model):
Lists are manually sorted because when adding a new browse record order
will not be correct as the added slide would actually end up at the
first place no matter its sequence."""
- channel_slides = dict.fromkeys(self.mapped('channel_id').ids, self.env['slide.slide'])
+ channel_slides = {}
for slide in self:
- channel_slides[slide.channel_id.id] += slide
+ if slide.channel_id.id not in channel_slides:
+ channel_slides[slide.channel_id.id] = slide.channel_id.slide_ids
for cid, slides in channel_slides.items():
current_category = self.env['slide.slide']
@@ -203,6 +222,7 @@ class Slide(models.Model):
record.total_views = record.slide_views + record.public_views
@api.depends('slide_partner_ids.vote')
+ @api.depends_context('uid')
def _compute_user_info(self):
slide_data = dict.fromkeys(self.ids, dict({'likes': 0, 'dislikes': 0, 'user_vote': False}))
slide_partners = self.env['slide.slide.partner'].sudo().search([
@@ -234,7 +254,10 @@ class Slide(models.Model):
@api.depends('slide_ids.slide_type', 'slide_ids.is_published', 'slide_ids.is_category')
def _compute_slides_statistics(self):
- result = dict.fromkeys(self.ids, dict())
+ # Do not use dict.fromkeys(self.ids, dict()) otherwise it will use the same dictionnary for all keys.
+ # Therefore, when updating the dict of one key, it updates the dict of all keys.
+ result = {_id: {} for _id in self.ids}
+
res = self.env['slide.slide'].read_group(
[('is_published', '=', True), ('category_id', 'in', self.ids), ('is_category', '=', False)],
['category_id', 'slide_type'], ['category_id', 'slide_type'],
@@ -261,6 +284,7 @@ class Slide(models.Model):
return result
@api.depends('slide_partner_ids.partner_id')
+ @api.depends('uid')
def _compute_user_membership_id(self):
slide_partners = self.env['slide.slide.partner'].sudo().search([
('slide_id', 'in', self.ids),
@@ -383,6 +407,11 @@ class Slide(models.Model):
if values.get('is_published'):
self.date_published = datetime.datetime.now()
self._post_publication()
+
+ if 'is_published' in values or 'active' in values:
+ # if the slide is published/unpublished, recompute the completion for the partners
+ self.slide_partner_ids._completed_callback()
+
return res
@api.returns('self', lambda value: value.id)