forked from NaN-tic/trytond-galatea_cms-old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cms.py
335 lines (291 loc) · 11.2 KB
/
cms.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# This file is part galatea_cms module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
from trytond.model import ModelSQL, ModelView, fields, tree
from trytond.pool import Pool
from trytond.transaction import Transaction
from trytond.cache import Cache
from trytond.pyson import Eval, Not, Equal, In
from trytond.i18n import gettext
from trytond.exceptions import UserError
from .tools import slugify
__all__ = ['Menu', 'Article', 'Block', 'Carousel', 'CarouselItem']
class Menu(tree(), ModelSQL, ModelView):
"Menu CMS"
__name__ = 'galatea.cms.menu'
name = fields.Char('Name', translate=True, required=True)
code = fields.Char('Code', required=True,
help='Internal code.')
icon = fields.Char('Icon',
help='Icon name show in menu.')
css = fields.Char('CSS',
help='Class CSS in menu.')
slug = fields.Char('Slug', translate=True, required=True,
help='Cannonical uri.')
active = fields.Boolean('Active', select=True)
parent = fields.Many2One("galatea.cms.menu", "Parent", select=True)
left = fields.Integer('Left', required=True, select=True)
right = fields.Integer('Right', required=True, select=True)
childs = fields.One2Many('galatea.cms.menu', 'parent', 'Children')
sequence = fields.Integer('Sequence')
login = fields.Boolean('Login', help='Allow login users')
manager = fields.Boolean('Manager', help='Allow manager users')
nofollow = fields.Boolean('Nofollow',
help='Add attribute in links to not search engines continue')
@staticmethod
def default_active():
return True
@staticmethod
def default_left():
return 0
@staticmethod
def default_right():
return 0
@staticmethod
def default_sequence():
return 1
@classmethod
def __setup__(cls):
super(Menu, cls).__setup__()
cls._order.insert(0, ('sequence', 'ASC'))
cls._order.insert(1, ('id', 'ASC'))
@fields.depends('name', 'code', 'slug')
def on_change_name(self):
if self.name:
az09 = slugify(self.name)
if not self.code:
self.code = az09
if not self.slug:
self.slug = az09
@classmethod
def copy(cls, menus, default=None):
if default is None:
default = {}
default['left'] = 0
default['right'] = 0
new_menus = []
for menu in menus:
default['slug'] = '%s-copy' % menu.slug
new_menu, = super(Menu, cls).copy([menu], default=default)
new_menus.append(new_menu)
return new_menus
class Article(ModelSQL, ModelView):
"Article CMS"
__name__ = 'galatea.cms.article'
name = fields.Char('Title', translate=True, required=True)
slug = fields.Char('Slug', required=True, translate=True,
help='Cannonical uri.')
slug_langs = fields.Function(fields.Dict(None, 'Slug Langs'), 'get_slug_langs')
uri = fields.Function(fields.Char('Uri'), 'get_uri')
description = fields.Text('Description', required=True, translate=True,
help='You could write wiki markup to create html content. Formats text following '
'the MediaWiki (http://meta.wikimedia.org/wiki/Help:Editing) syntax.')
metadescription = fields.Char('Meta Description', translate=True,
help='Almost all search engines recommend it to be shorter ' \
'than 155 characters of plain text')
metakeywords = fields.Char('Meta Keywords', translate=True,
help='Separated by comma')
metatitle = fields.Char('Meta Title', translate=True)
template = fields.Char('Template', required=True)
active = fields.Boolean('Active',
help='Dissable to not show content article.')
visibility = fields.Selection([
('public','Public'),
('register','Register'),
('manager','Manager'),
], 'Visibility', required=True)
galatea_website = fields.Many2One('galatea.website', 'Website',
domain=[('active', '=', True)], required=True)
_slug_langs_cache = Cache('galatea_cms_article.slug_langs')
attachments = fields.One2Many('ir.attachment', 'resource', 'Attachments')
wikimarkup = fields.Boolean('WikiMarkup',
help='Article use wiki markups')
@staticmethod
def default_active():
return True
@staticmethod
def default_visibility():
return 'public'
@staticmethod
def default_wikimarkup():
return True
@staticmethod
def default_template():
return 'cms-article.html'
@classmethod
def default_galatea_website(cls):
Website = Pool().get('galatea.website')
websites = Website.search([('active', '=', True)])
if len(websites) == 1:
return websites[0].id
@fields.depends('name', 'slug')
def on_change_name(self):
if self.name and not self.slug:
self.slug = slugify(self.name)
@fields.depends('slug')
def on_change_slug(self):
if self.slug:
self.slug = slugify(self.slug)
@classmethod
def create(cls, vlist):
for values in vlist:
values = values.copy()
if values.get('slug'):
slug = slugify(values.get('esale_slug'))
values['slug'] = slug
return super(Article, cls).create(vlist)
@classmethod
def write(cls, *args):
actions = iter(args)
args = []
for articles, values in zip(actions, actions):
values = values.copy()
if values.get('slug'):
slug = slugify(values.get('slug'))
values['slug'] = slug
args.extend((articles, values))
return super(Article, cls).write(*args)
@classmethod
def copy(cls, articles, default=None):
new_articles = []
for article in articles:
default['slug'] = '%s-copy' % article.slug
new_article, = super(Article, cls).copy([article], default=default)
new_articles.append(new_article)
return new_articles
@classmethod
def delete(cls, articles):
raise UserError(gettext('galatea_msg.msg_delete_articles'))
def get_slug_langs(self, name):
'''Return dict slugs by all languaes actives'''
pool = Pool()
Lang = pool.get('ir.lang')
Article = pool.get('galatea.cms.article')
article_id = self.id
langs = Lang.search([
('active', '=', True),
('translatable', '=', True),
])
slugs = {}
for lang in langs:
with Transaction().set_context(language=lang.code):
article, = Article.read([article_id], ['slug'])
slugs[lang.code] = article['slug']
return slugs
def get_uri(self, name):
if self.galatea_website:
locale = Transaction().context.get('language', 'en')
return '%s%s/%s' % (
self.galatea_website.uri,
locale[:2],
self.slug,
)
return ''
class Block(ModelSQL, ModelView):
"Block CMS"
__name__ = 'galatea.cms.block'
name = fields.Char('Name', required=True)
code = fields.Char('Code', required=True, help='Internal code.')
type = fields.Selection([
('image', 'Image'),
('remote_image', 'Remote Image'),
('custom_code', 'Custom Code'),
], 'Type', required=True)
file = fields.Many2One('galatea.static.file', 'File',
states = {
'required': Equal(Eval('type'), 'image'),
'invisible': Not(Equal(Eval('type'), 'image'))
})
remote_image_url = fields.Char('Remote Image URL',
states = {
'required': Equal(Eval('type'), 'remote_image'),
'invisible': Not(Equal(Eval('type'), 'remote_image'))
})
custom_code = fields.Text('Custom Code', translate=True,
states={
'required': Equal(Eval('type'), 'custom_code'),
'invisible': Not(Equal(Eval('type'), 'custom_code'))
},
help='You could write wiki markup to create html content. Formats text following '
'the MediaWiki (http://meta.wikimedia.org/wiki/Help:Editing) syntax.')
height = fields.Integer('Height',
states = {
'invisible': Not(In(Eval('type'), ['image', 'remote_image']))
})
width = fields.Integer('Width',
states = {
'invisible': Not(In(Eval('type'), ['image', 'remote_image']))
})
alternative_text = fields.Char('Alternative Text',
states = {
'invisible': Not(In(Eval('type'), ['image', 'remote_image']))
})
click_url = fields.Char('Click URL', translate=True,
states = {
'invisible': Not(In(Eval('type'), ['image', 'remote_image']))
})
active = fields.Boolean('Active', select=True)
attachments = fields.One2Many('ir.attachment', 'resource', 'Attachments')
visibility = fields.Selection([
('public','Public'),
('register','Register'),
('manager','Manager'),
], 'Visibility', required=True)
@staticmethod
def default_active():
'Return True'
return True
@staticmethod
def default_type():
'Return Image'
return 'image'
@staticmethod
def default_visibility():
return 'public'
@fields.depends('name', 'code')
def on_change_name(self):
if self.name and not self.code:
self.code = slugify(self.name)
class Carousel(ModelSQL, ModelView):
"Carousel CMS"
__name__ = 'galatea.cms.carousel'
name = fields.Char('Name', translate=True, required=True)
code = fields.Char('Code', required=True,
help='Internal code. Use characters az09')
active = fields.Boolean('Active', select=True)
items = fields.One2Many('galatea.cms.carousel.item', 'carousel', 'Items')
@staticmethod
def default_active():
return True
@fields.depends('name', 'code')
def on_change_name(self):
if self.name and not self.code:
self.code = slugify(self.name)
class CarouselItem(ModelSQL, ModelView):
"Carousel Item CMS"
__name__ = 'galatea.cms.carousel.item'
carousel = fields.Many2One("galatea.cms.carousel", "Carousel", required=True)
name = fields.Char('Label', translate=True, required=True)
link = fields.Char('Link', translate=True,
help='URL absolute')
image = fields.Char('Image', translate=True,
help='Image with URL absolute')
sublabel = fields.Char('Sublabel', translate=True,
help='In case text carousel, second text')
description = fields.Char('Description', translate=True,
help='In cas text carousel, description text')
html = fields.Text('HTML', translate=True,
help='HTML formated item - Content carousel-inner')
active = fields.Boolean('Active', select=True)
sequence = fields.Integer('Sequence')
@staticmethod
def default_active():
return True
@staticmethod
def default_sequence():
return 1
@classmethod
def __setup__(cls):
super(CarouselItem, cls).__setup__()
cls._order.insert(0, ('sequence', 'ASC'))
cls._order.insert(1, ('id', 'ASC'))