1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
import json
from operator import attrgetter
from trytond.model import fields
from trytond.pool import Pool, PoolMeta
from trytond.tools import slugify
from .web import ShopVSFIdentifierMixin
class _ProductEntityMixin:
__slots__ = ()
vsf_sku = fields.Function(
fields.Char("SKU"), 'get_vsf_sku', searcher='search_vsf_sku')
def get_vsf_sku(self, name):
return self.code
@classmethod
def search_vsf_sku(cls, name, clause):
return [('code',) + tuple(clause[1:])]
@property
def vsf_image(self):
try:
return self.get_image_url(_external=True, web_shop=1)
except AttributeError:
pass
@property
def vsf_type_id(self):
return 'virtual' if self.type == 'service' else 'simple'
@property
def vsf_quantity(self):
pool = Pool()
Uom = pool.get('product.uom')
quantity = self.forecast_quantity
if quantity < 0:
quantity = 0
return Uom.compute_qty(self.default_uom, quantity, self.sale_uom)
def get_vsf_entity(self, shop, price, tax):
categories = [
c for c in self.categories_all
if c in shop.categories]
status = 1
if not self.salable:
status = 2
quantity = self.vsf_quantity
if quantity <= 0:
status = 3
return {
'id': self.vsf_identifier.id,
'name': self.name,
'image': self.vsf_image,
'sku': self.vsf_sku,
'url_key': slugify(self.name.lower()),
'url_path': '%s/%s' % (
self.vsf_identifier.id, slugify(self.name.lower())),
'type_id': self.vsf_type_id,
'price': float(price) if price is not None else None,
'price_tax': float(tax) if tax is not None else None,
'price_incl_tax': (
float(price + tax)
if price is not None and tax is not None else None),
'status': status,
'visibility': 4,
'category_ids': [c.vsf_identifier.id for c in categories],
'category': [c.get_vsf_entity_product(shop) for c in categories],
'stock': [{
'is_in_stock': quantity > 0,
'qty': quantity,
}],
}
def get_vsf_stock(self):
quantity = self.vsf_quantity
return {
'product_id': self.vsf_identifier.id,
'qty': quantity,
'is_in_stock': quantity > 0,
}
class Product(_ProductEntityMixin, ShopVSFIdentifierMixin, metaclass=PoolMeta):
__name__ = 'product.product'
def vsf_is_configurable(self, shop):
return False
class Template(
_ProductEntityMixin, ShopVSFIdentifierMixin, metaclass=PoolMeta):
__name__ = 'product.template'
@property
def vsf_type_id(self):
return 'configurable'
def get_vsf_products(self, shop):
return [p for p in self.products if shop in p.web_shops]
class Category(ShopVSFIdentifierMixin, metaclass=PoolMeta):
__name__ = 'product.category'
def get_vsf_entity(self, shop):
pool = Pool()
Product = pool.get('product.product')
count = Product.search([
('categories_all', '=', self.id),
('web_shops', '=', shop.id),
], count=True)
paths = self.get_vsf_paths(shop)
def children(category):
return [{
'id': c.vsf_identifier.id,
'children_data': children(c),
} for c in category.childs
if c in shop.categories]
return {
'id': self.vsf_identifier.id,
'name': self.name,
'parent_id': (self.parent.vsf_identifier.id
if self.parent in shop.categories else None),
'path': '/'.join([str(p.vsf_identifier.id) for p in paths]),
'url_key': slugify(self.name.lower()),
'url_path': '/'.join(
map(slugify, map(str.lower, map(attrgetter('name'), paths)))),
'is_active': True,
'position': self.id,
'level': len(paths),
'product_count': count,
'children_data': children(self),
}
def get_vsf_entity_product(self, shop):
return {
'category_id': self.vsf_identifier.id,
'name': self.name,
'slug': slugify(self.name),
'path': '/'.join(
map(slugify, map(attrgetter('name'),
self.get_vsf_paths(shop)))),
}
def get_vsf_paths(self, shop):
category = self
paths = [category]
while category.parent:
if category.parent not in shop.categories:
break
category = category.parent
paths.append(category)
return list(reversed(paths))
class ProductAttribute(metaclass=PoolMeta):
__name__ = 'product.product'
def get_vsf_entity(self, shop, price, tax):
entity = super().get_vsf_entity(shop, price, tax)
if self.attribute_set:
for attribute in self.attribute_set.attributes:
if attribute not in shop.attributes:
continue
name = attribute.name
value = self.attributes.get(name)
options = {
o['name']: o['value']
for o in attribute.get_vsf_options()}
entity[name] = options.get(value, value)
return entity
def vsf_is_configurable(self, shop):
configurable = super().vsf_is_configurable(shop)
if self.template.attribute_set:
template_products = self.template.get_vsf_products(shop)
if len(template_products) > 1:
names = [
a.name for a in self.template.attribute_set.attributes
if a in shop.attributes]
values = {filter(
lambda k: k in names, sorted(p.attributes.keys()))
for p in template_products}
configurable = len(values) == len(template_products)
return configurable
class TemplateAttribute(metaclass=PoolMeta):
__name__ = 'product.template'
def get_vsf_entity(self, shop, price, tax):
entity = super().get_vsf_entity(shop, price, tax)
if self.attribute_set:
for attribute in self.attribute_set.attributes:
if attribute not in shop.attributes:
continue
name = attribute.name
p_attr_values = {
p.attributes[name]
for p in self.products
if name in p.attributes}
if attribute.type_ == 'multiselection':
p_attr_values = set(sum(p_attr_values, []))
options = [
o['value'] for o in attribute.get_vsf_options()
if o['name'] in p_attr_values]
entity[name + '_options'] = options
config_options = entity.setdefault('configurable_options', [])
config_options.append(
attribute.get_vsf_entity_template(self, p_attr_values))
return entity
class Attribute(ShopVSFIdentifierMixin, metaclass=PoolMeta):
__name__ = 'product.attribute'
def get_vsf_options(self):
return [{
'value': i,
'name': n,
'label': s,
} for i, (n, s) in enumerate(
json.loads(self.selection_json), 1)]
def get_vsf_entity(self, shop):
return {
'id': self.vsf_identifier.id,
'attribute_id': self.vsf_identifier.id,
'attribute_code': self.name,
'frontend_input': self.type_,
'frontend_label': self.string,
'is_comparable': True,
'is_user_defined': True,
'is_visible': True,
'is_visible_on_front': True,
'options': self.get_vsf_options(),
}
def get_vsf_entity_template(self, template, attribute_values):
values = [{
'value_index': o['value'],
'label': o['label'],
} for o in self.get_vsf_options()
if o['name'] in attribute_values]
return {
'attribute_id': self.vsf_identifier.id,
'attribute_code': self.name,
'label': self.string,
'product_id': template.vsf_identifier.id,
'values': values,
}
|