trytond-galatea_blog-old/blog.py

208 lines
7.2 KiB
Python
Raw Normal View History

2014-11-07 14:23:55 +01:00
# This file is part galatea_blog module for Tryton.
# The COPYRIGHT file at the top level of this repository contains
# the full copyright notices and license terms.
2014-05-27 12:17:33 +02:00
from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import Pool
from trytond.transaction import Transaction
2014-06-12 18:00:20 +02:00
from trytond.cache import Cache
2014-05-27 12:17:33 +02:00
from .tools import slugify
from datetime import datetime
2014-05-27 12:17:33 +02:00
__all__ = ['Post', 'Comment']
class Post(ModelSQL, ModelView):
"Blog Post"
__name__ = 'galatea.blog.post'
name = fields.Char('Title', translate=True,
required=True, on_change=['name', 'slug'])
2014-06-12 17:00:20 +02:00
slug = fields.Char('slug', required=True, translate=True,
2014-05-27 12:17:33 +02:00
help='Cannonical uri.')
2014-06-12 17:00:20 +02:00
slug_langs = fields.Function(fields.Dict(None, 'Slug Langs'), 'get_slug_langs')
2014-07-16 11:54:07 +02:00
uri = fields.Function(fields.Char('Uri'), 'get_uri')
2014-05-27 12:17:33 +02:00
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.')
long_description = fields.Text('Long Description', 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.')
2014-05-27 12:17:33 +02:00
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 post.')
galatea_website = fields.Many2One('galatea.website', 'Website',
domain=[('active', '=', True)], required=True)
post_create_date = fields.DateTime('Create Date', readonly=True)
post_write_date = fields.DateTime('Write Date', readonly=True)
user = fields.Many2One('galatea.user', 'User', required=True)
gallery = fields.Boolean('Gallery', help='Active gallery attachments.')
comment = fields.Boolean('Comment', help='Active comments.')
2014-07-29 13:08:29 +02:00
comments = fields.One2Many('galatea.blog.comment', 'post', 'Comments')
attachments = fields.One2Many('ir.attachment', 'resource', 'Attachments')
2014-06-12 18:00:20 +02:00
_slug_langs_cache = Cache('galatea_blog_post.slug_langs')
2014-05-27 12:17:33 +02:00
@staticmethod
def default_active():
return True
@staticmethod
def default_template():
return 'blog-post.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
@classmethod
def default_user(cls):
Website = Pool().get('galatea.website')
websites = Website.search([('active', '=', True)])
if not websites:
return None
website, = websites
if website.blog_anonymous_user:
return website.blog_anonymous_user.id
return None
@staticmethod
def default_gallery():
return True
@staticmethod
def default_comment():
return True
2014-05-27 12:17:33 +02:00
@classmethod
def __setup__(cls):
super(Post, cls).__setup__()
2014-10-15 09:44:06 +02:00
cls._order.insert(0, ('post_create_date', 'DESC'))
2014-05-27 12:17:33 +02:00
cls._order.insert(1, ('id', 'DESC'))
cls._error_messages.update({
'delete_posts': ('You can not delete '
'posts because you will get error 404 NOT Found. '
'Dissable active field.'),
})
def on_change_name(self):
res = {}
if self.name and not self.slug:
res['slug'] = slugify(self.name)
return res
@classmethod
def create(cls, vlist):
now = datetime.now()
vlist = [x.copy() for x in vlist]
for vals in vlist:
vals['post_create_date'] = now
return super(Post, cls).create(vlist)
@classmethod
def write(cls, *args):
now = datetime.now()
actions = iter(args)
args = []
for blogs, values in zip(actions, actions):
values = values.copy()
values['post_write_date'] = now
args.extend((blogs, values))
return super(Post, cls).write(*args)
2014-05-27 12:17:33 +02:00
@classmethod
def copy(cls, posts, default=None):
new_posts = []
for post in posts:
default['slug'] = '%s-copy' % post.slug
default['blog_create_date'] = None
default['blog_write_date'] = None
2014-05-27 12:17:33 +02:00
new_post, = super(Post, cls).copy([post], default=default)
new_posts.append(new_post)
return new_posts
@classmethod
def delete(cls, posts):
cls.raise_user_error('delete_posts')
2014-06-12 17:00:20 +02:00
def get_slug_langs(self, name):
2014-12-15 09:38:32 +01:00
'Return dict slugs for each active languages'
2014-06-12 17:00:20 +02:00
pool = Pool()
Lang = pool.get('ir.lang')
Post = pool.get('galatea.blog.post')
post_id = self.id
langs = Lang.search([
('active', '=', True),
('translatable', '=', True),
])
slugs = {}
for lang in langs:
with Transaction().set_context(language=lang.code):
post, = Post.read([post_id], ['slug'])
slugs[lang.code] = post['slug']
2014-10-01 12:58:59 +02:00
return slugs
2014-07-16 11:54:07 +02:00
def get_uri(self, name):
if self.galatea_website:
locale = Transaction().context.get('language', 'en')
2014-10-07 14:33:43 +02:00
return '%s%s/blog/%s' % (
2014-07-16 11:54:07 +02:00
self.galatea_website.uri,
locale[:2],
self.slug,
)
return ''
2014-05-27 12:17:33 +02:00
class Comment(ModelSQL, ModelView):
"Blog Comment Post"
__name__ = 'galatea.blog.comment'
post = fields.Many2One('galatea.blog.post', 'Post', required=True)
user = fields.Many2One('galatea.user', 'User', required=True)
description = fields.Text('Description', required=True,
help='You could write wiki markup to create html content. Formats text following '
'the MediaWiki (http://meta.wikimedia.org/wiki/Help:Editing) syntax.')
active = fields.Boolean('Active',
help='Dissable to not show content post.')
comment_create_date = fields.Function(fields.Char('Create Date'),
'get_comment_create_date')
2014-05-27 12:17:33 +02:00
2014-07-29 13:11:03 +02:00
@classmethod
def __setup__(cls):
super(Comment, cls).__setup__()
cls._order.insert(0, ('create_date', 'DESC'))
cls._order.insert(1, ('id', 'DESC'))
2014-05-27 12:17:33 +02:00
@staticmethod
def default_active():
return True
@classmethod
def default_user(cls):
Website = Pool().get('galatea.website')
websites = Website.search([('active', '=', True)])
if not websites:
return None
website, = websites
if website.blog_anonymous_user:
return website.blog_anonymous_user.id
return None
@classmethod
def get_comment_create_date(cls, records, name):
2014-12-15 09:38:32 +01:00
'Created domment date'
res = {}
DATE_FORMAT = '%s %s' % (Transaction().context['locale']['date'], '%H:%M:%S')
for record in records:
res[record.id] = record.create_date.strftime(DATE_FORMAT) or ''
return res