Skip to content

Commit

Permalink
Merge branch 'release/3.4.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Prakash Pandey committed Mar 17, 2015
2 parents f8cbed2 + a02b006 commit 6557b11
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 75 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
Version 3.4.2.0
===============

This release intrdouces Product Media model to store media
items on product and template. Also provide safe migration
from product.image_set.

Version 3.4.0.3
===============

Expand Down
9 changes: 5 additions & 4 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
nereid_catalog
:copyright: (c) 2010-2013 by Openlabs Technologies & Consulting (P) Ltd.
:copyright: (c) 2010-2015 by Openlabs Technologies & Consulting (P) Ltd.
:license: GPLv3, see LICENSE for more details
'''
from trytond.pool import Pool
from product import (
Product, ProductsRelated, ProductTemplate, StaticFile
Product, ProductsRelated, ProductTemplate, ProductMedia
)
from website import WebSite

Expand All @@ -18,7 +18,8 @@ def register():
Pool.register(
Product,
ProductTemplate,
ProductMedia,
ProductsRelated,
StaticFile,
WebSite,
module='nereid_catalog', type_='model')
module='nereid_catalog', type_='model'
)
105 changes: 48 additions & 57 deletions product.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Products catalogue display
:copyright: (c) 2010-2014 by Openlabs Technologies & Consulting (P) Ltd.
:copyright: (c) 2010-2015 by Openlabs Technologies & Consulting (P) Ltd.
:license: GPLv3, see LICENSE for more details
'''
Expand All @@ -20,14 +20,14 @@
from werkzeug.exceptions import NotFound
from flask.ext.babel import format_currency

from trytond.model import ModelSQL, fields
from trytond.model import ModelSQL, ModelView, fields
from trytond.pyson import Eval, Not, Bool
from trytond.transaction import Transaction
from trytond.pool import Pool, PoolMeta
from trytond import backend

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

Expand All @@ -38,42 +38,51 @@
}


class StaticFile:
__name__ = 'nereid.static.file'
class ProductMedia(ModelSQL, ModelView):
"Product Media"
__name__ = "product.media"

product = fields.Many2One(
'product.product', 'Product', select=True
)
template = fields.Many2One(
'product.template', 'Template', select=True
)
sequence = fields.Integer("Sequence", required=True, select=True)
static_file = fields.Many2One(
"nereid.static.file", "Static File", required=True, select=True)
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)
super(ProductMedia, cls).__register__(module_name)

sf_table = Table('nereid_static_file')
media_table = cls.__table__()

if TableHandler.table_exist(cursor, 'product_product_imageset'):
# Migrate data from ProductImageSet table to StaticFile table
# Migrate data from ProductImageSet table to ProductMedia 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)
cursor.execute(*media_table.insert(
columns=[
media_table.product, media_table.template,
media_table.static_file,
],
values=[
imageset_table.select(
imageset_table.product, imageset_table.template,
imageset_table.image
)
]
))

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

@staticmethod
def default_sequence():
return 10


class ProductTemplate:
__name__ = "product.template"
Expand All @@ -83,28 +92,21 @@ class ProductTemplate:
'get_products_displayed_on_eshop'
)
description = fields.Text("Description")
static_files = fields.One2Many(
'nereid.static.file', 'template', 'Static Files',
add_remove=[
('product', 'in', Eval('products')),
],
depends=['products'],
order=[
('sequence', 'ASC'),
]
media = fields.One2Many("product.media", "template", "Media")
images = fields.Function(
fields.One2Many('nereid.static.file', None, 'Images'),
getter='get_template_images'
)
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
))
template_images = []
for media in self.media:
if 'image' in media.static_file.mimetype:
template_images.append(media.static_file.id)
return template_images

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

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'),
]
media = fields.One2Many("product.media", "product", "Media")
images = fields.Function(
fields.One2Many('nereid.static.file', None, 'Images'),
getter='get_product_images'
)
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 Down Expand Up @@ -463,10 +453,11 @@ 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
))
product_images = []
for media in self.media:
if 'image' in media.static_file.mimetype:
product_images.append(media.static_file.id)
return product_images

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

<!--Product Media View-->
<record model="ir.ui.view" id="product_media_tree">
<field name="model">product.media</field>
<field name="type">tree</field>
<field name="name">product_media_tree</field>
</record>
<record model="ir.ui.view" id="product_media_form">
<field name="model">product.media</field>
<field name="type">form</field>
<field name="name">product_media_form</field>
</record>
</data>
</tryton>
17 changes: 11 additions & 6 deletions tests/test_catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'''
Catalog test suite
:copyright: (c) 2010-2013 by Openlabs Technologies & Consulting (P) Ltd.
:copyright: (c) 2010-2015 by Openlabs Technologies & Consulting (P) Ltd.
:license: GPLv3, see LICENSE for more details
'''
Expand Down Expand Up @@ -386,6 +386,7 @@ def test_0100_product_images(self):
Product = POOL.get('product.product')
StaticFolder = POOL.get("nereid.static.folder")
StaticFile = POOL.get("nereid.static.file")
Media = POOL.get('product.media')

with Transaction().start(DB_NAME, USER, CONTEXT):
self.setup_defaults()
Expand All @@ -395,15 +396,19 @@ def test_0100_product_images(self):
'folder_name': 'Test'
}])
file_buffer = buffer('test-content')
file = StaticFile.create([{
file, = StaticFile.create([{
'name': 'test.png',
'folder': folder.id,
'file_binary': file_buffer
}])[0]
}])

product, = Product.search([], limit=1)

product = Product.search([])[0]
file.product = product
file.save()
Media.create([{
'product': product.id,
'template': product.template.id,
'static_file': file.id,
}])

app = self.get_app()
with app.test_request_context('/'):
Expand Down
12 changes: 9 additions & 3 deletions tests/test_product.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,19 @@ def test0030_get_variant_images(self):
'cost_price': Decimal('5'),
'default_uom': uom.id,
'description': 'Description of template',
'static_files': [('add', [file.id])],
'media': [('create', [{
'static_file': file.id,
}])],
'products': [('create', self.Template.default_products())]
}])

product, = product_template.products
file1.product = product
file1.save()

Product.write([product], {
'media': [('create', [{
'static_file': file1.id,
}])]
})

self.assertEqual(product.get_images()[0].id, file.id)
Product.write([product], {
Expand Down
2 changes: 1 addition & 1 deletion tryton.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[tryton]
version=3.4.1.0
version=3.4.2.0
depends:
product
nereid
Expand Down
4 changes: 2 additions & 2 deletions view/product_form_nereid.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ this repository contains the full copyright notices and license terms. -->
id="sepr_cross_sells"/>
<field name="cross_sells" colspan="4" />
</page>
<page string="Static Files" id="static_files"
<page string="Media" id="product_media"
states="{'invisible': Bool(Eval('use_template_images'))}">
<field name="static_files" colspan="4"/>
<field name="media" colspan="4"/>
</page>
</notebook>
</page>
Expand Down
7 changes: 7 additions & 0 deletions view/product_media_form.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<form string="Media">
<label name="sequence"/>
<field name="sequence"/>
<label name="static_file"/>
<field name="static_file"/>
</form>
5 changes: 5 additions & 0 deletions view/product_media_tree.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0"?>
<tree string="Media">
<field name="sequence"/>
<field name="static_file"/>
</tree>
4 changes: 2 additions & 2 deletions view/template_form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ this repository contains the full copyright notices and license terms. -->
<page string="Description" id="template-description">
<field name="description"/>
</page>
<page string="Static Files" id="template-static-files">
<field name="static_files" colspan="4"/>
<page string="Media" id="template-media">
<field name="media" colspan="4"/>
</page>
</xpath>
</data>

0 comments on commit 6557b11

Please sign in to comment.