-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcategories.py
183 lines (151 loc) · 6.53 KB
/
categories.py
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
# -*- coding: UTF-8 -*-
# Copyright (C) 2009 Sylvain Taverne <[email protected]>
# Copyright (C) 2009 Hervé Cauwelier <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from itools
from itools.core import merge_dicts
from itools.datatypes import Integer, Unicode
from itools.gettext import MSG
from itools.web import get_context
from itools.xapian import AndQuery, NotQuery, PhraseQuery
from itools.xml import xml_to_text
# Import from ikaaro
from ikaaro.forms import XHTMLBody, TextWidget, ImageSelectorWidget, RTEWidget
from ikaaro.registry import register_resource_class, register_field
from ikaaro.registry import get_register_fields
# Import from itws
from itws.views import AutomaticEditView
# Import from shop
from categories_views import Category_View, Category_BackofficeView
from categories_views import Category_Comparator, Category_BatchEdition
from categories_views import NewCategory_Form
from csv_views import Export
from datatypes import ImagePathDataType
from folder import ShopFolder
from utils import get_group_name, get_shop
from products import Product
from products.product_views import Product_NewProduct, Products_View
class Category(ShopFolder):
class_id = 'category'
class_title = MSG(u'Category')
# Edit configuration
edit_show_meta = True
edit_schema = {'data': XHTMLBody(multilingual=True),
'breadcrumb_title': Unicode(multilingual=True),
'image_category': ImagePathDataType(multilingual=True),
'default_product_cover': ImagePathDataType(multilingual=True)}
edit_widgets = [
TextWidget('breadcrumb_title', title=MSG(u'Breadcrumb title')),
ImageSelectorWidget('image_category', title=MSG(u'Category image')),
ImageSelectorWidget('default_product_cover', title=MSG(u'Default cover for products')),
RTEWidget('data', title=MSG(u"Description"))]
# Views
view = Category_View()
browse_content = Products_View()
view_categories = Category_BackofficeView()
edit = AutomaticEditView()
batch_edition = Category_BatchEdition()
new_product = Product_NewProduct()
new_category = NewCategory_Form()
comparator = Category_Comparator()
@property
def class_views(self):
context = get_context()
# Back-Office
hostname = context.uri.authority
if hostname[:6] == 'admin.' :
return ['browse_content', 'new_product',
'view_categories', 'new_category',
'edit']
return ['view', 'edit']
@classmethod
def get_metadata_schema(cls):
return merge_dicts(ShopFolder.get_metadata_schema(),
data=XHTMLBody(multilingual=True),
breadcrumb_title=Unicode(multilingual=True),
image_category=ImagePathDataType(multilingual=True),
default_product_cover=ImagePathDataType(multilingual=True))
def _get_catalog_values(self):
# Get the languages
site_root = self.get_site_root()
languages = site_root.get_property('website_languages')
# Titles
m_title = {}
m_breadcrumb_title = {}
for language in languages:
value = self.get_property('title', language=language)
if value:
m_title[language] = value
value = self.get_property('breadcrumb_title', language=language)
if value:
m_breadcrumb_title[language] = value
# Data
data = self.get_property('data')
if data is not None:
data = xml_to_text(data)
return merge_dicts(
super(Category, self)._get_catalog_values(),
data=data,
# XXX Hack to be on sitemap
workflow_state='public',
m_title=m_title,
m_breadcrumb_title=m_breadcrumb_title)
def get_document_types(self):
return [Product, Category]
####################################
# Computed fields
####################################
computed_schema = {'nb_products': Integer(title=MSG(u'Nb products')),
'nb_categories': Integer(title=MSG(u'Nb sub categories'))}
def get_nb_products(self, only_public=False):
root = self.get_root()
shop = get_shop(self)
abspath = self.get_canonical_path()
query = [PhraseQuery('parent_paths', str(abspath)),
PhraseQuery('format', shop.product_class.class_id)]
if shop.get_property('hide_not_buyable_products') is True:
context = get_context()
group_name = get_group_name(shop, context)
query.append(NotQuery(PhraseQuery('not_buyable_by_groups', group_name)))
if only_public is True:
query.append(PhraseQuery('workflow_state', 'public'))
return len(root.search(AndQuery(*query)))
def get_nb_categories(self):
root = self.get_root()
abspath = self.get_canonical_path()
query = AndQuery(
PhraseQuery('parent_paths', str(abspath)),
PhraseQuery('format', 'category'))
return len(root.search(query))
@property
def nb_products(self):
return self.get_nb_products()
@property
def nb_categories(self):
return self.get_nb_categories()
#############################
# Export
#############################
export = Export(export_resource=Product,
access='is_allowed_to_edit',
file_columns=['reference', 'state',
'frontoffice_uri', 'cover_uri',
'manufacturer', 'price_with_tax',
'description'])
register_resource_class(Category)
# Add m_title field if it does not already exist
if 'm_title' in get_register_fields() is False:
register_field('m_title', Unicode(is_stored=True, is_indexed=True))
register_field('m_breadcrumb_title', Unicode(is_stored=True, is_indexed=True))