Use GalateaVisiblePage as superclass for Article

This commit is contained in:
Guillem Barba 2015-02-07 13:47:12 +01:00
parent 41b74feaee
commit 5826da6111
3 changed files with 13 additions and 237 deletions

224
cms.py
View file

@ -2,12 +2,11 @@
# 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
from trytond.pool import Pool, PoolMeta
from trytond.transaction import Transaction
from trytond.cache import Cache
from trytond.pyson import Bool, Equal, Eval, Greater, In, Not
from trytond.pool import Pool
from trytond.pyson import Bool, Equal, Eval, In, Not
from .tools import slugify
from trytond.modules.galatea import GalateaVisiblePage
from trytond.modules.galatea.tools import slugify
__all__ = ['Menu', 'Article', 'ArticleWebsite', 'Block',
'Carousel', 'CarouselItem']
@ -117,36 +116,11 @@ class Menu(ModelSQL, ModelView):
return super(Menu, cls).copy(menus, default=default)
class Article(ModelSQL, ModelView):
class Article(GalateaVisiblePage, ModelSQL, ModelView):
"Article CMS"
__name__ = 'galatea.cms.article'
name = fields.Char('Title', translate=True, required=True)
canonical_uri = fields.Many2One('galatea.uri', 'Canonical URI',
required=True, select=True, domain=[
('website', 'in', Eval('websites')),
('type', '=', 'content'),
('template.allowed_models.model', 'in', ['galatea.cms.article']),
],
states={
'invisible': ~Greater(Eval('id', -1), 0),
}, depends=['websites', 'id'])
slug = fields.Function(fields.Char('Slug', translate=True, required=True),
'on_change_with_slug', setter='set_canonical_uri_field',
searcher='search_canonical_uri_field')
slug_langs = fields.Function(fields.Dict(None, 'Slug Langs'),
'get_slug_langs')
_slug_langs_cache = Cache('galatea_cms_article.slug_langs')
# TODO: ha d'incloure el canonical_uri o tenir alternative_uris + camp
# funcional *uris*
uris = fields.One2Many('galatea.uri', 'content', 'URIs', readonly=True,
help='All article URIs')
uri = fields.Function(fields.Many2One('galatea.uri', 'URI'),
'get_uri', searcher='search_uri')
# TODO: maybe websites should be a searchable functional field as sum of
# canonical_uri/uris website field
websites = fields.Many2Many('galatea.cms.article-galatea.website',
'article', 'website', 'Websites', required=True,
help='Tutorial will be available in those websites')
'article', 'website', 'Websites', required=True)
description = fields.Text('Description', required=True, translate=True,
help='You could write wiki markup to create html content. Formats '
'text following the MediaWiki '
@ -162,19 +136,6 @@ class Article(ModelSQL, ModelView):
metakeywords = fields.Char('Meta Keywords', translate=True,
help='Separated by comma')
metatitle = fields.Char('Meta Title', translate=True)
# TODO: I think it should go to another module which implements private
# area/portal features
visibility = fields.Selection([
('public', 'Public'),
('register', 'Register'),
('manager', 'Manager'),
], 'Visibility', required=True, select=True)
# TODO: extend getter/setter/searcher to all uris (is active is some uri is
# active)
active = fields.Function(fields.Boolean('Active',
help='Dissable to not show content article.'),
'get_active', setter='set_canonical_uri_field',
searcher='search_canonical_uri_field')
attachments = fields.One2Many('ir.attachment', 'resource', 'Attachments')
@classmethod
@ -186,183 +147,16 @@ class Article(ModelSQL, ModelView):
'Dissable active field.'),
})
@fields.depends('name', 'slug')
def on_change_name(self):
res = {}
if self.name and not self.slug:
res['slug'] = slugify(self.name)
return res
@fields.depends('canonical_uri', 'slug')
def on_change_with_slug(self, name=None):
if self.canonical_uri:
return self.canonical_uri.slug
if self.slug:
return slugify(self.slug)
@classmethod
def set_canonical_uri_field(cls, articles, name, value):
pool = Pool()
Uri = pool.get('galatea.uri')
Uri.write([a.canonical_uri for a in articles if a.canonical_uri], {
name: value,
})
@classmethod
def search_canonical_uri_field(cls, name, clause):
domain = [
('canonical_uri.%s' % name,) + tuple(clause[1:]),
]
if clause == ['active', '=', False]:
domain = [
'OR',
domain,
[('canonical_uri', '=', None)],
]
return domain
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):
context = Transaction().context
if context.get('website', False):
for uri in self.uris:
if uri.website.id == context['website']:
return uri.id
return self.canonical_uri.id
@classmethod
def search_uri(cls, name, clause):
context = Transaction().context
if context.get('website', False):
# TODO: is it better and If()?
return [
['OR', [
('canonical_uri',) + tuple(clause[1:]),
('website', '=', context['website']),
], [
('uris',) + tuple(clause[1:]),
('website', '=', context['website']),
]],
]
return [
['OR', [
('canonical_uri',) + tuple(clause[1:]),
], [
('uris',) + tuple(clause[1:]),
]],
]
@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_visibility():
return 'public'
@staticmethod
def default_active():
return True
def get_active(self, name):
return self.canonical_uri.active if self.canonical_uri else False
@classmethod
def create(cls, vlist):
pool = Pool()
Uri = pool.get('galatea.uri')
for vals in vlist:
if not vals.get('canonical_uri'):
assert vals.get('slug')
assert vals.get('websites')
uri, = Uri.create([{
'website': vals['websites'][0],
'name': vals['name'],
'slug': vals['slug'],
'type': 'content',
'active': vals.get('active', cls.default_active()),
}])
vals['canonical_uri'] = uri.id
new_articles = super(Article, cls).create(vlist)
uri_args = []
for article in new_articles:
if not article.canonical_uri.content:
uri_args.extend([article.canonical_uri], {
'content': str(article),
})
if uri_args:
Uri.write(*uri_args)
return new_articles
@classmethod
def copy(cls, articles, default=None):
pool = Pool()
Uri = pool.get('galatea.uri')
if default is None:
default = {}
else:
default = default.copy()
new_articles = []
for article in articles:
default['canonical_uri'] = Uri.copy([article.canonical_uri], {
'slug': '%s-copy' % article.slug,
})
new_articles += super(Article, cls).copy([article],
default=default)
return new_articles
@classmethod
def write(cls, *args):
pool = Pool()
Uri = pool.get('galatea.uri')
actions = iter(args)
uri_args = []
for articles, values in zip(actions, actions):
if values.get('canonical_uri'):
canonical_uri = Uri(values['canonical_uri'])
canonical_uri.content = articles[0]
canonical_uri.save()
if 'name' in values:
uri_todo = []
for article in articles:
if article.canonical_uri.name == article.name:
uri_todo.append(article.canonical_uri)
for uri in article.uris:
if uri.name == article.name and uri not in uri_todo:
uri_todo.append(uri)
if uri_todo:
# What happens if canonical_uri and name change?
uri_args.append(uri_todo)
uri_args.append({
'name': values['name'],
})
super(Article, cls).write(*args)
if uri_args:
Uri.write(*uri_args)
def calc_uri_vals(cls, record_vals):
# TODO: calc parent and template?
return super(Article, cls).calc_uri_vals(record_vals)
@classmethod
def delete(cls, articles):

View file

@ -1,24 +1,6 @@
# 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.
import logging
try:
import slug
except ImportError:
logging.getLogger('product esale').error(
'Unable to import slug. Install slug package.')
def slugify(value):
"""Convert value to slug: az09 and replace spaces by -"""
try:
if isinstance(value, unicode):
name = slug.slug(value)
else:
name = slug.slug(unicode(value, 'UTF-8'))
except:
name = ''
return name
def seo_lenght(string):
'''Get first 155 characters from string'''

View file

@ -1,16 +1,16 @@
<?xml version="1.0"?>
<!-- This file is part galatea_cms of Tryton. The COPYRIGHT file at the top level of
this repository contains the full copyright notices and license terms. -->
<form string="Article">
<form string="Article" col="6">
<label name="name"/>
<field name="name"/>
<label name="slug"/>
<field name="slug"/>
<label name="canonical_uri"/>
<field name="canonical_uri"/>
<label name="active"/>
<field name="active"/>
<notebook colspan="4">
<label name="canonical_uri"/>
<field name="canonical_uri"/>
<notebook colspan="6">
<page string="Descriptions" id="descriptions">
<field name="description" colspan="4"/>
<newline/>