Skip to content

Commit

Permalink
Merge branch 'release/3.4.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakash Pandey committed Mar 10, 2015
2 parents 757b158 + b375ba3 commit 090b080
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 327 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Version 3.4.0.3
===============

* Removed all traces of the ProductImageSet model in favour of linking Static
Files to Products/Templates as One2Many field.
* product_product_imageset table is migrated to nereid_static_file table if the
former is present.

Version 3.2.1.0
===============

Expand Down
6 changes: 3 additions & 3 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
'''
from trytond.pool import Pool
from product import (
Product, ProductsImageSet, ProductsRelated, ProductCategory,
ProductTemplate
Product, ProductsRelated, ProductCategory,
ProductTemplate, StaticFile
)
from website import WebSite


def register():
Pool.register(
ProductsImageSet,
Product,
ProductTemplate,
ProductsRelated,
ProductCategory,
StaticFile,
WebSite,
module='nereid_catalog', type_='model')
194 changes: 88 additions & 106 deletions product.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
'''
from collections import deque
from sql import Table

from nereid import render_template, route
from nereid.globals import session, request, current_app
Expand All @@ -26,8 +27,8 @@
from trytond import backend

__all__ = [
'Product', 'ProductsImageSet', 'ProductsRelated', 'ProductCategory',
'ProductTemplate',
'Product', 'ProductsRelated', 'ProductCategory', 'ProductTemplate',
'StaticFile'
]
__metaclass__ = PoolMeta

Expand All @@ -38,6 +39,43 @@
}


class StaticFile:
__name__ = 'nereid.static.file'

product = fields.Many2One(
'product.product', 'Product', select=True
)
template = fields.Many2One(
'product.template', 'Template', select=True
)

@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().cursor

super(StaticFile, cls).__register__(module_name)

sf_table = Table('nereid_static_file')

if TableHandler.table_exist(cursor, 'product_product_imageset'):
# Migrate data from ProductImageSet table to StaticFile table
imageset_table = Table('product_product_imageset')

query = sf_table.update(
columns=[sf_table.template, sf_table.product],
values=[imageset_table.template, imageset_table.product],
from_=[imageset_table],
where=(sf_table.id == imageset_table.image)
)
cursor.execute(*query)

TableHandler.drop_table(
cursor, 'product.product.imageset', 'product_product_imageset',
cascade=True
)


class ProductTemplate:
__name__ = "product.template"

Expand All @@ -46,12 +84,28 @@ class ProductTemplate:
'get_products_displayed_on_eshop'
)
description = fields.Text("Description")
image_sets = fields.One2Many(
'product.product.imageset', 'template', 'Images',
)
default_image_set = fields.Many2One(
'product.product.imageset', 'Default Image Set', readonly=True
static_files = fields.One2Many(
'nereid.static.file', 'template', 'Static Files',
add_remove=[
('product', 'in', Eval('products')),
],
depends=['products'],
order=[
('sequence', 'ASC'),
]
)
images = fields.Function(fields.One2Many(
'nereid.static.file', None, 'Images'
), getter='get_template_images')

def get_template_images(self, name=None):
"""
Getter for `images` function field
"""
return map(int, filter(
lambda static_file: 'image' in static_file.mimetype,
self.static_files
))

def get_products_displayed_on_eshop(self, name=None):
"""
Expand Down Expand Up @@ -88,12 +142,23 @@ class Product:
)
displayed_on_eshop = fields.Boolean('Displayed on E-Shop?', select=True)

image_sets = fields.One2Many(
'product.product.imageset', 'product',
'Image Sets', states={
static_files = fields.One2Many(
'nereid.static.file', 'product', 'Static Files',
states={
'invisible': Bool(Eval('use_template_images')),
}
},
add_remove=[
('template', '=', Eval('template')),
],
depends=['template'],
order=[
('sequence', 'ASC'),
]
)
images = fields.Function(fields.One2Many(
'nereid.static.file', None, 'Images'
), getter='get_product_images')

up_sells = fields.Many2Many(
'product.product-product.product',
'product', 'up_sell', 'Up-Sells', states=DEFAULT_STATE
Expand All @@ -105,9 +170,6 @@ class Product:
default_image = fields.Function(
fields.Many2One('nereid.static.file', 'Image'), 'get_default_image',
)
default_image_set = fields.Many2One(
'product.product.imageset', 'Default Image Set', readonly=True,
)
use_template_description = fields.Boolean("Use template's description")
use_template_images = fields.Boolean("Use template's images")

Expand All @@ -120,8 +182,7 @@ def get_default_image(self, name):
"""
Returns default product image if any.
"""
images = self.get_images()
return images[0].id if images else None
return self.images[0].id if self.images else None

@classmethod
def __setup__(cls):
Expand Down Expand Up @@ -401,103 +462,24 @@ def get_description(self):
return Markup(self.template.description)
return Markup(self.description)

def get_product_images(self, name=None):
"""
Getter for `images` function field
"""
return map(int, filter(
lambda static_file: 'image' in static_file.mimetype,
self.static_files
))

def get_images(self):
"""
Get images of product variant.
If the product is set to use the template's images, then
the template images is sent back.
"""
if self.use_template_images:
return map(lambda x: x.image, self.template.image_sets)
return map(lambda x: x.image, self.image_sets)


class ProductsImageSet(ModelSQL, ModelView):
"Images for Product"
__name__ = 'product.product.imageset'

name = fields.Char("Name", required=True)
product = fields.Many2One(
'product.product', 'Product',
ondelete='CASCADE', select=True)
template = fields.Many2One(
'product.template', 'Template',
ondelete='CASCADE', select=True)
image = fields.Many2One(
'nereid.static.file', 'Image',
ondelete='CASCADE', select=True, required=True
)
image_preview = fields.Function(
fields.Binary('Image Preview'), 'get_image_preview'
)

@property
def large(self):
"""
Return large image
"""
return self.resize(1024, 1024)

@property
def medium(self):
"""
Return medium image
"""
return self.resize(500, 500)

@property
def thumbnail(self):
"""
Return thumbnail image
"""
return self.resize(100, 100)

def resize(self, width, height):
"""
Return image with user specified dimensions
"""
return self.image.transform_command().resize(width, height)

def get_image_preview(self, name=None):
return self.image.file_binary if self.image else None

@classmethod
def __register__(cls, module_name):
TableHandler = backend.get('TableHandler')
cursor = Transaction().cursor

table = TableHandler(cursor, cls, module_name)
if not table.column_exist('image'):
table.column_rename('large_image', 'image')

super(ProductsImageSet, cls).__register__(module_name)

@classmethod
def __setup__(cls):
super(ProductsImageSet, cls).__setup__()
cls._buttons.update({
'set_default': {},
})

@classmethod
@ModelView.button
def set_default(cls, image_sets):
"""
Sets the image set as default image set
"""
Product = Pool().get('product.product')
ProductTemplate = Pool().get('product.template')

for image_set in image_sets:
if image_set.product:
Product.write([image_set.product], {
'default_image_set': image_set.id,
})
elif image_set.template:
ProductTemplate.write([image_set.template], {
'default_image_set': image_set.id,
})
return self.template.images
return self.images


class ProductsRelated(ModelSQL):
Expand Down
11 changes: 0 additions & 11 deletions product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,5 @@
<field name="name">nereid_website_product</field>
</record>

<record model="ir.ui.view" id="product_image_form">
<field name="model">product.product.imageset</field>
<field name="type">form</field>
<field name="name">product_image_form</field>
</record>
<record model="ir.ui.view" id="product_image_tree">
<field name="model">product.product.imageset</field>
<field name="type">tree</field>
<field name="name">product_image_tree</field>
</record>

</data>
</tryton>
31 changes: 8 additions & 23 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ def setUp(self):
'home.jinja':
'''
{{request.nereid_website.get_currencies()}}
{{ product.image_sets[0].thumbnail }}
{{ product.image_sets[0].large }}
{{ product.image_sets[0].medium }}
{% for image in product.images %}
{{ image.name }}
{% endfor %}
''',
'login.jinja':
'{{ login_form.errors }} {{get_flashed_messages()}}',
Expand Down Expand Up @@ -442,11 +442,10 @@ def test_0100_products_displayed_on_eshop(self):
self.assertEqual(len(template1.products_displayed_on_eshop), 2)
self.assertEqual(len(template1.products), 3)

def test_0110_product_image_set(self):
def test_0110_product_images(self):
"""
Test for adding product image set
Test for adding product images
"""
ProductImageSet = POOL.get('product.product.imageset')
Product = POOL.get('product.product')
StaticFolder = POOL.get("nereid.static.folder")
StaticFile = POOL.get("nereid.static.file")
Expand All @@ -466,27 +465,13 @@ def test_0110_product_image_set(self):
}])[0]

product = Product.search([])[0]
file.product = product
file.save()

image, = ProductImageSet.create([{
'name': 'test_image',
'product': product,
'image': file
}])
app = self.get_app()
with app.test_request_context('/'):
home_template = render_template('home.jinja', product=product)
self.assertTrue(
'/static-file-transform/1/resize%2Cw_100%2Ch_100%2Cm_n.png'
in home_template
)
self.assertTrue(
'/static-file-transform/1/resize%2Cw_500%2Ch_500%2Cm_n.png'
in home_template
)
self.assertTrue(
'/static-file-transform/1/resize%2Cw_1024%2Ch_1024%2Cm_n'
'.png' in home_template
)
self.assertTrue(file.name in home_template)


def suite():
Expand Down

0 comments on commit 090b080

Please sign in to comment.