Thumb field
This commit is contained in:
parent
9a21a10efe
commit
a268af81a4
|
@ -2,11 +2,13 @@
|
|||
# The COPYRIGHT file at the top level of this repository contains
|
||||
# the full copyright notices and license terms.
|
||||
from trytond.pool import Pool
|
||||
from .configuration import *
|
||||
from .tutorial import *
|
||||
from .galatea import *
|
||||
|
||||
def register():
|
||||
Pool.register(
|
||||
Configuration,
|
||||
GalateaTutorial,
|
||||
GalateaTutorialWebSite,
|
||||
GalateaTutorialComment,
|
||||
|
|
|
@ -0,0 +1,20 @@
|
|||
# This file is part galatea_tutorial 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 fields
|
||||
from trytond.pool import PoolMeta
|
||||
|
||||
__all__ = ['Configuration']
|
||||
__metaclass__ = PoolMeta
|
||||
|
||||
|
||||
class Configuration:
|
||||
__name__ = 'galatea.configuration'
|
||||
tutorial_thumb_size = fields.Integer('Tutorial Thumb Size',
|
||||
help='Thumbnail Tutorial Image Size (width x height)')
|
||||
tutorial_thumb_crop = fields.Boolean('Tutorial Thumb Crop',
|
||||
help='Crop Thumb Tutorial Image')
|
||||
|
||||
@staticmethod
|
||||
def default_tutorial_thumb_size():
|
||||
return 300
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0"?>
|
||||
<!-- This file is part galatea_tutorial module for Tryton.
|
||||
The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. -->
|
||||
<tryton>
|
||||
<data>
|
||||
<record model="ir.ui.view" id="galatea_configuration_view_form">
|
||||
<field name="model">galatea.configuration</field>
|
||||
<field name="inherit" ref="galatea.galatea_configuration_view_form"/>
|
||||
<field name="name">configuration_form</field>
|
||||
</record>
|
||||
</data>
|
||||
</tryton>
|
27
tools.py
27
tools.py
|
@ -1,27 +0,0 @@
|
|||
#~ # This file is part galatea_tutorial 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('galatea tutorial').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'''
|
||||
if len(string) > 155:
|
||||
return '%s...' % (string[:152])
|
||||
return string
|
|
@ -4,5 +4,6 @@ depends:
|
|||
ir
|
||||
galatea
|
||||
xml:
|
||||
configuration.xml
|
||||
tutorial.xml
|
||||
galatea.xml
|
||||
|
|
81
tutorial.py
81
tutorial.py
|
@ -1,12 +1,16 @@
|
|||
# This file is part galatea_tutorial module for Tryton.
|
||||
# The COPYRIGHT file at the top level of this repository contains
|
||||
# the full copyright notices and license terms.
|
||||
from mimetypes import guess_type
|
||||
from datetime import datetime
|
||||
import os
|
||||
import hashlib
|
||||
from trytond.model import ModelSQL, ModelView, fields
|
||||
from trytond.pool import Pool
|
||||
from trytond.transaction import Transaction
|
||||
from trytond.cache import Cache
|
||||
from .tools import slugify
|
||||
from datetime import datetime
|
||||
from trytond.config import config
|
||||
from trytond.modules.galatea.tools import slugify, IMAGE_TYPES, thumbly
|
||||
|
||||
__all__ = ['GalateaTutorial', 'GalateaTutorialWebSite', 'GalateaTutorialComment']
|
||||
|
||||
|
@ -49,6 +53,11 @@ class GalateaTutorial(ModelSQL, ModelView):
|
|||
total_comments = fields.Function(fields.Integer("Total Comments"),
|
||||
'get_totalcomments')
|
||||
attachments = fields.One2Many('ir.attachment', 'resource', 'Attachments')
|
||||
thumb = fields.Function(fields.Binary('Thumb', filename='thumb_filename'),
|
||||
'get_thumb', setter='set_thumb')
|
||||
thumb_filename = fields.Char('File Name',
|
||||
help='Thumbnail File Name')
|
||||
thumb_path = fields.Function(fields.Char('Thumb Path'), 'get_thumb_path')
|
||||
_slug_langs_cache = Cache('galatea_tutorial.slug_langs')
|
||||
|
||||
@staticmethod
|
||||
|
@ -88,6 +97,10 @@ class GalateaTutorial(ModelSQL, ModelView):
|
|||
'delete_tutorials': ('You can not delete '
|
||||
'tutorials because you will get error 404 NOT Found. '
|
||||
'Dissable active field.'),
|
||||
'not_file_mime': ('Not know file mime "%(file_name)s"'),
|
||||
'not_file_mime_image': ('"%(file_name)s" file mime is not an image ' \
|
||||
'(jpg, png or gif)'),
|
||||
'image_size': ('Thumb "%(file_name)s" size is larger than "%(size)s"Kb'),
|
||||
})
|
||||
|
||||
def on_change_name(self):
|
||||
|
@ -153,6 +166,70 @@ class GalateaTutorial(ModelSQL, ModelView):
|
|||
def get_totalcomments(self, name):
|
||||
return len(self.comments)
|
||||
|
||||
def get_thumb(self, name):
|
||||
db_name = Transaction().cursor.dbname
|
||||
filename = self.thumb_filename
|
||||
if not filename:
|
||||
return None
|
||||
filename = os.path.join(config.get('database', 'path'), db_name,
|
||||
'galatea', 'tutorial', filename[0:2], filename[2:4], filename)
|
||||
|
||||
value = None
|
||||
try:
|
||||
with open(filename, 'rb') as file_p:
|
||||
value = buffer(file_p.read())
|
||||
except IOError:
|
||||
pass
|
||||
return value
|
||||
|
||||
def get_thumb_path(self, name):
|
||||
filename = self.thumb_filename
|
||||
if not filename:
|
||||
return None
|
||||
return '%s/%s/%s' % (filename[:2], filename[2:4], filename)
|
||||
|
||||
@classmethod
|
||||
def set_thumb(cls, tutorials, name, value):
|
||||
if value is None:
|
||||
return
|
||||
|
||||
Config = Pool().get('galatea.configuration')
|
||||
galatea_config = Config(1)
|
||||
size = galatea_config.tutorial_thumb_size or 300
|
||||
crop = galatea_config.tutorial_thumb_crop
|
||||
db_name = Transaction().cursor.dbname
|
||||
galatea_dir = os.path.join(
|
||||
config.get('database', 'path'), db_name, 'galatea', 'tutorial')
|
||||
|
||||
for tutorial in tutorials:
|
||||
file_name = tutorial['thumb_filename']
|
||||
|
||||
file_mime, _ = guess_type(file_name)
|
||||
if not file_mime:
|
||||
cls.raise_user_error('not_file_mime', {
|
||||
'file_name': file_name,
|
||||
})
|
||||
if file_mime not in IMAGE_TYPES:
|
||||
cls.raise_user_error('not_file_mime_image', {
|
||||
'file_name': file_name,
|
||||
})
|
||||
|
||||
_, ext = file_mime.split('/')
|
||||
digest = '%s.%s' % (hashlib.md5(value).hexdigest(), ext)
|
||||
subdir1 = digest[0:2]
|
||||
subdir2 = digest[2:4]
|
||||
directory = os.path.join(galatea_dir, subdir1, subdir2)
|
||||
filename = os.path.join(directory, digest)
|
||||
|
||||
thumb = thumbly(directory, filename, value, size, crop)
|
||||
if not thumb:
|
||||
cls.raise_user_error('not_file_mime_image', {
|
||||
'file_name': file_name,
|
||||
})
|
||||
cls.write([tutorial], {
|
||||
'thumb_filename': digest,
|
||||
})
|
||||
|
||||
|
||||
class GalateaTutorialWebSite(ModelSQL):
|
||||
'Galatea Tutorial - Website'
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
<?xml version="1.0"?>
|
||||
<!-- This file is part galatea_tutorial module for Tryton.
|
||||
The COPYRIGHT file at the top level of this repository contains the full copyright notices and license terms. -->
|
||||
<data>
|
||||
<xpath expr="/form" position="inside">
|
||||
<label name="tutorial_thumb_size"/>
|
||||
<field name="tutorial_thumb_size"/>
|
||||
<label name="tutorial_thumb_crop"/>
|
||||
<field name="tutorial_thumb_crop"/>
|
||||
</xpath>
|
||||
</data>
|
|
@ -40,6 +40,9 @@ this repository contains the full copyright notices and license terms. -->
|
|||
<field name="websites" colspan="6"/>
|
||||
</page>
|
||||
<page string="Attachments" id="attachments">
|
||||
<label name="thumb"/>
|
||||
<field name="thumb"/>
|
||||
<field name="thumb_filename" invisible="1"/>
|
||||
<field name="attachments" colspan="6"/>
|
||||
</page>
|
||||
</notebook>
|
||||
|
|
Loading…
Reference in New Issue