[FIX] website_blog: fix depends for post_date

Before this commit, create a blog post since the backend raise
an error because the post_date is False.

It should never arrive, since it is a compute field that is the
publish date or fallback on the create_date.

But because set post_date, change the published_date, the depend from
publish date cannot be triggered again since it change the post_date.

So _set_post_date don't trigger _compute_post_date. And call it manually
will make 'max recursion depth python'.

The 'hack' to update publish date, without retrigger the _compute_post_date
and set post_date manually, is to use the _write method that don't trigger
all depends fields.

Courtesy of @rco-odoo

This commit closes #13829
This commit is contained in:
Jeremy Kersten
2016-10-17 17:32:42 +02:00
parent 5da0ae9b04
commit 74e1d6940d
+6 -2
View File
@@ -101,11 +101,13 @@ class BlogPost(models.Model):
blog_post.website_url = "/blog/%s/post/%s" % (slug(blog_post.blog_id), slug(blog_post))
@api.multi
@api.depends('post_date', 'visits')
def _compute_ranking(self):
res = {}
for blog_post in self:
age = datetime.now() - fields.Datetime.from_string(blog_post.post_date)
res[blog_post.id] = blog_post.visits * (0.5 + random.random()) / max(3, age.days)
if blog_post.id: # avoid to rank one post not yet saved and so withtout post_date in case of an onchange.
age = datetime.now() - fields.Datetime.from_string(blog_post.post_date)
res[blog_post.id] = blog_post.visits * (0.5 + random.random()) / max(3, age.days)
return res
def _default_content(self):
@@ -181,6 +183,8 @@ class BlogPost(models.Model):
def _set_post_date(self):
for blog_post in self:
blog_post.published_date = blog_post.post_date
if not blog_post.published_date:
blog_post._write(dict(post_date=blog_post.create_date)) # dont trigger inverse function
def _check_for_publication(self, vals):
if vals.get('website_published'):