Skip to content

Commit

Permalink
[FIX] website_blog: Correct migration (#755)
Browse files Browse the repository at this point in the history
Take into account empty covers and background_image contains an URL,
not the image itself.
  • Loading branch information
pedrobaeza authored Feb 7, 2017
1 parent 6075c98 commit 1894735
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 23 deletions.
39 changes: 21 additions & 18 deletions addons/website_blog/migrations/9.0.1.0/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,32 @@


def convert_blog_post_cover(env):
"""Upload image as attachment and change cover_properties for using it."""
attachment_obj = env['ir.attachment']
base_url = env['ir.config_parameter'].get_param('web.base.url')
for post in env['blog.post'].search([]):
env.cr.execute(
"""SELECT {} FROM blog_post WHERE id=%s""".format(
openupgrade.get_legacy_name('background_image')
), (post.id, )
"""Put default value for posts without cover image and put URL of the
cover in field cover_properties for using it for the rest."""
post_obj = env['blog.post']
# Without cover image
env.cr.execute(
"SELECT id FROM blog_post WHERE {} IS NULL".format(
openupgrade.get_legacy_name('background_image')
)
row = env.cr.fetchone()
attachment = attachment_obj.create({
'name': 'blog_post_{}_cover'.format(post.id),
'type': 'binary',
'db_datas': row[0],
'res_model': 'ir.ui.view',
'public': True,
})
)
posts = post_obj.browse([x[0] for x in env.cr.fetchall()])
posts.write({'cover_properties': post_obj._defaults['cover_properties']})
# With cover image
env.cr.execute(
"SELECT id, {0} FROM blog_post WHERE {0} IS NOT NULL".format(
openupgrade.get_legacy_name('background_image')
)
)
for row in env.cr.fetchall():
post = post_obj.browse(row[0])
post.cover_properties = (
'{{"background-image": "url({}/web/image/{})",'
'{{"background-image": "url({})",'
' "opacity": "1",'
' "background-color": "oe_none",'
' "resize_class": "cover cover_full"}}'
).format(base_url, attachment.id)
).format(row[1])
break


@openupgrade.migrate(use_env=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ def test_blog_post_cover(self):
('name', '=', 'The Future of Emails'),
])
self.assertTrue(post.cover_properties)
self.assertIn('/web/image', post.cover_properties)
attachment = self.env['ir.attachment'].search([
('name', '=', 'blog_post_{}_cover'.format(post.id)),
])
self.assertTrue(attachment)
self.assertIn('url(', post.cover_properties)

0 comments on commit 1894735

Please sign in to comment.