-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcms.py
491 lines (440 loc) · 17.4 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# 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, DeactivableMixin, fields, tree
from trytond.pool import Pool
from trytond.pyson import Bool, Equal, Eval, In, Not
from trytond import backend
from trytond.i18n import gettext
from trytond.modules.galatea.resource import GalateaVisiblePage
from trytond.modules.galatea.tools import slugify
from trytond.transaction import Transaction
from .exceptions import DeleteWarning
__all__ = ['Menu', 'Article', 'ArticleBlock', 'ArticleWebsite', 'Block',
'Carousel', 'CarouselItem']
_TITLE_STYLE = [
(None, ''),
('h1', 'H1'),
('h2', 'H2'),
('h3', 'H3'),
('h4', 'H4'),
('h5', 'H5'),
('h6', 'H6'),
]
_BLOCK_TYPES = [
('image', 'Image'),
('remote_image', 'Remote Image'),
('custom_code', 'Custom Code'),
('section_description', 'Section Description'),
('image_bkg', 'Image Background'),
('section_description_black', 'Section Description Black'),
('css_bkg', 'CSS Background'),
('section_home_background', 'Section Home Background'),
('image_top_bkg', 'Image Top Background'),
('section_description_blue', 'Section Description Blue'),
('section_description_icon', 'Section Description Icon'),
('section_title', 'Section Title'),
]
_BLOCK_COVER_IMAGE_DOMAIN = [
('resource', '=', Eval('attachment_resource')),
]
_BLOCK_COVER_IMAGE_STATES = {
'readonly': Eval('id', 0) <= 0,
}
_BLOCK_COVER_IMAGE_CONTEXT = {
'resource': Eval('attachment_resource'),
}
_BLOCK_COVER_IMAGE_DEPENDS = ['attachment_resource']
_BLOCK_TITLE_REQUIRED = ['section_description']
class Menu(tree(), DeactivableMixin, ModelSQL, ModelView):
"Menu CMS"
__name__ = 'galatea.cms.menu'
# _rec_name = 'name_used'
website = fields.Many2One('galatea.website', 'Website',
ondelete='RESTRICT', required=True)
name = fields.Char('Name', translate=True, states={
'readonly': Eval('name_uri', False),
})
name_uri = fields.Boolean('Use URI\'s name', states={
'invisible': ((Eval('target_type', '') != 'internal_uri')
| ~Bool(Eval('target_uri'))),
})
name_used = fields.Function(fields.Char('Name', translate=True,
required=True),
'on_change_with_name_used', searcher='search_name_used')
code = fields.Char('Code', required=True,
help='Internal code.')
target_type = fields.Selection([
('internal_uri', 'Internal URI'),
('external_url', 'External URL'),
], 'Type', required=True)
target_uri = fields.Many2One('galatea.uri', 'Target URI', states={
'invisible': Eval('target_type', '') != 'internal_uri',
}, domain=[
('website', '=', Eval('website')),
])
target_url = fields.Char('Target URL', states={
'invisible': Eval('target_type', '') != 'external_url',
})
url = fields.Function(fields.Char('URL'),
'get_url')
parent = fields.Many2One('galatea.cms.menu', 'Parent', domain=[
('website', '=', Eval('website')),
])
left = fields.Integer('Left', required=True)
right = fields.Integer('Right', required=True)
childs = fields.One2Many('galatea.cms.menu', 'parent', 'Children')
sequence = fields.Integer('Sequence')
nofollow = fields.Boolean('Nofollow',
help='Add attribute in links to not search engines continue')
# TODO: I think the following fields should go to another module
css = fields.Char('CSS',
help='Class CSS in menu.')
icon = fields.Char('Icon',
help='Icon name show in menu.')
login = fields.Boolean('Login', help='Allow login users')
manager = fields.Boolean('Manager', help='Allow manager users')
hidden_xs = fields.Boolean('Hidden XS',
help='Hidden Extra small devices')
hidden_sm = fields.Boolean('Hidden SM',
help='Hidden Small devices')
hidden_md = fields.Boolean('Hidden MD',
help='Hidden Medium devices')
hidden_lg = fields.Boolean('Hidden LG',
help='Hidden Large devices')
image = fields.Many2One('ir.attachment', 'Image',
domain=[
('resource.id', '=', Eval('id', -1), 'galatea.cms.menu')
], depends = ['id'])
description = fields.Text('Description', translate=True)
@classmethod
def __setup__(cls):
super(Menu, cls).__setup__()
cls._order = [
('sequence', 'ASC'),
('id', 'ASC'),
]
@fields.depends('name_uri', 'target_uri', 'name')
def on_change_with_name_used(self, name=None):
return (self.target_uri.name if self.name_uri and self.target_uri
else self.name)
def get_rec_name(self, name):
if self.name_used:
return self.name_used
return '(%s)' % self.id
@classmethod
def search_name_used(cls, name, clause):
return [
['OR', [
('name_uri', '=', True),
('target_uri.name',) + tuple(clause[1:]),
], [
('name_uri', '=', False),
('name',) + tuple(clause[1:]),
]],
]
def get_url(self, name):
return (self.target_url if self.target_type == 'external_url'
else (self.target_uri.uri if self.target_uri else '#'))
@classmethod
def default_website(cls):
Website = Pool().get('galatea.website')
websites = Website.search([])
if len(websites) == 1:
return websites[0].id
@staticmethod
def default_left():
return 0
@staticmethod
def default_right():
return 0
@staticmethod
def default_sequence():
return 1
@classmethod
def copy(cls, menus, default=None):
if default is None:
default = {}
default['left'] = 0
default['right'] = 0
# new_menus = []
# for menu in menus:
# new_menu, = super(Menu, cls).copy([menu], default=default)
# new_menus.append(new_menu)
# return new_menus
return super(Menu, cls).copy(menus, default=default)
class Article(GalateaVisiblePage):
"Article CMS"
__name__ = 'galatea.cms.article'
_table = None # Needed to reset GalateaVisiblePage._table
websites = fields.Many2Many('galatea.cms.article-galatea.website',
'article', 'website', 'Websites', required=True)
description = fields.Text('Description', translate=True,
help='You could write wiki or RST markups to create html content.')
description_html = fields.Function(fields.Text('Description HTML'),
'on_change_with_description_html')
markup = fields.Selection([
(None, ''),
('wikimedia', 'WikiMedia'),
('rest', 'ReStructuredText'),
], 'Markup')
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)
attachments = fields.One2Many('ir.attachment', 'resource', 'Attachments')
blocks = fields.One2Many('galatea.cms.article.block', 'article', 'Blocks')
show_title = fields.Boolean('Show Title')
@classmethod
def __setup__(cls):
super(Article, cls).__setup__()
domain_clause = ('allowed_models.name', 'in', ['galatea.cms.article'])
if domain_clause not in cls.template.domain:
cls.template.domain.append(domain_clause)
@classmethod
def __register__(cls, module_name):
table = backend.TableHandler(cls, module_name)
super(Article, cls).__register__(module_name)
table.not_null_action('galatea_website', action='remove')
table.not_null_action('template', action='remove')
@classmethod
def view_attributes(cls):
return super(Article, cls).view_attributes() + [
('//page[@id="descriptions"]/group[@id="description_html"]', 'states', {
'invisible': Eval('markup'),
}),
('//page[@id="descriptions"]/group[@id="description"]', 'states', {
'invisible': ~Eval('markup'),
}),
]
@classmethod
def default_websites(cls):
Website = Pool().get('galatea.website')
websites = Website.search([('active', '=', True)])
return [w.id for w in websites]
@staticmethod
def default_show_title():
return True
@fields.depends('description')
def on_change_with_description_html(self, name=None):
if self.description:
return self.description
@classmethod
def calc_uri_vals(cls, record_vals):
# TODO: calc parent and template?
uri_vals = super(Article, cls).calc_uri_vals(record_vals)
if 'template' in record_vals:
uri_vals['template'] = record_vals['template']
return uri_vals
@classmethod
def delete(cls, articles):
Warning = Pool().get('res.user.warning')
key = 'delete_articles'
if Warning.check(key):
raise DeleteWarning(key,
gettext('galatea_cms.msg_delete_articles'))
super(Article, cls).delete(articles)
@property
def slug_langs(self):
'''Return dict slugs by llanguage'''
pool = Pool()
Lang = pool.get('ir.lang')
langs = Lang.search([
('active', '=', True),
('translatable', '=', True),
])
slugs = {}
for lang in langs:
with Transaction().set_context(language=lang.code):
slugs[lang.code] = self.__class__(self.id).slug
return slugs
class ArticleBlock(ModelSQL, ModelView):
"Article Block CMS"
__name__ = 'galatea.cms.article.block'
article = fields.Many2One('galatea.cms.article', 'Article',
required=True)
block = fields.Many2One('galatea.cms.block', 'Block',
required=True)
sequence = fields.Integer('Sequence')
@staticmethod
def default_sequence():
return 1
@classmethod
def __setup__(cls):
super(ArticleBlock, cls).__setup__()
cls._order.insert(0, ('sequence', 'ASC'))
class ArticleWebsite(ModelSQL):
'Galatea CMS Article - Website'
__name__ = 'galatea.cms.article-galatea.website'
article = fields.Many2One('galatea.cms.article', 'Article',
ondelete='CASCADE', required=True)
website = fields.Many2One('galatea.website', 'Website',
ondelete='RESTRICT', required=True)
class Block(DeactivableMixin, 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(_BLOCK_TYPES, '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 or RST markups to create html content.')
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']))
})
attachments = fields.One2Many('ir.attachment', 'resource', 'Attachments')
visibility = fields.Selection([
('public', 'Public'),
('register', 'Register'),
('manager', 'Manager'),
], 'Visibility', required=True)
css = fields.Char('CSS',
help='Seperated styles by a space')
title = fields.Char('Title', translate=True,
states={
'required': Eval('type').in_(_BLOCK_TITLE_REQUIRED),
})
title_headings = fields.Selection(_TITLE_STYLE, 'Title Headings')
show_title = fields.Boolean('Show Title')
paragraph1 = fields.Text('Paragraph 1', translate=True)
paragraph2 = fields.Text('Paragraph 2', translate=True)
paragraph3 = fields.Text('Paragraph 3', translate=True)
paragraph4 = fields.Text('Paragraph 4', translate=True)
paragraph5 = fields.Text('Paragraph 5', translate=True)
markup = fields.Selection([
(None, ''),
('wikimedia', 'WikiMedia'),
('rest', 'ReStructuredText'),
], 'Markup')
attachment_resource = fields.Function(fields.Char('Attachment Resource'),
'get_attachment_resource')
cover_image1 = fields.Many2One('ir.attachment', 'Cover Image 1',
domain=_BLOCK_COVER_IMAGE_DOMAIN,
states=_BLOCK_COVER_IMAGE_STATES,
context=_BLOCK_COVER_IMAGE_CONTEXT,
depends=_BLOCK_COVER_IMAGE_DEPENDS)
cover_image2 = fields.Many2One('ir.attachment', 'Cover Image 2',
domain=_BLOCK_COVER_IMAGE_DOMAIN,
states=_BLOCK_COVER_IMAGE_STATES,
context=_BLOCK_COVER_IMAGE_CONTEXT,
depends=_BLOCK_COVER_IMAGE_DEPENDS)
cover_image3 = fields.Many2One('ir.attachment', 'Cover Image 3',
domain=_BLOCK_COVER_IMAGE_DOMAIN,
states=_BLOCK_COVER_IMAGE_STATES,
context=_BLOCK_COVER_IMAGE_CONTEXT,
depends=_BLOCK_COVER_IMAGE_DEPENDS)
cover_image4 = fields.Many2One('ir.attachment', 'Cover Image 4',
domain=_BLOCK_COVER_IMAGE_DOMAIN,
states=_BLOCK_COVER_IMAGE_STATES,
context=_BLOCK_COVER_IMAGE_CONTEXT,
depends=_BLOCK_COVER_IMAGE_DEPENDS)
cover_image5 = fields.Many2One('ir.attachment', 'Cover Image 5',
domain=_BLOCK_COVER_IMAGE_DOMAIN,
states=_BLOCK_COVER_IMAGE_STATES,
context=_BLOCK_COVER_IMAGE_CONTEXT,
depends=_BLOCK_COVER_IMAGE_DEPENDS)
cover_image_align = fields.Selection([
(None, 'None'),
('top', 'Top'),
('bottom', 'Bottom'),
('right', 'Right'),
('left', 'Left'),
], 'Cover Image Align')
total_cover_images = fields.Function(fields.Integer('Total Cover Images'),
'on_change_with_total_cover_images')
@staticmethod
def default_type():
return 'custom_code'
@staticmethod
def default_visibility():
return 'public'
@staticmethod
def default_show_title():
return True
@fields.depends('name', 'code')
def on_change_name(self):
if self.name and not self.code:
self.code = slugify(self.name)
@fields.depends('cover_image1', 'cover_image2', 'cover_image3',
'cover_image4', 'cover_image5')
def on_change_with_total_cover_images(self, name=None):
total = 0
if self.cover_image1:
total += 1
if self.cover_image2:
total += 1
if self.cover_image3:
total += 1
if self.cover_image4:
total += 1
if self.cover_image5:
total += 1
return total
def get_attachment_resource(self, name):
if self.id:
return 'galatea.cms.block,%s' % self.id
return 'galatea.cms.block,-1'
class Carousel(DeactivableMixin, 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')
items = fields.One2Many('galatea.cms.carousel.item', 'carousel', 'Items')
@fields.depends('name', 'code')
def on_change_name(self):
if self.name and not self.code:
self.code = slugify(self.name)
class CarouselItem(DeactivableMixin, 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')
sequence = fields.Integer('Sequence')
@staticmethod
def default_sequence():
return 1
@classmethod
def __setup__(cls):
super(CarouselItem, cls).__setup__()
cls._order = [
('sequence', 'ASC'),
('id', 'ASC'),
]