Compare commits
1 commit
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1286fa6f2f |
13 changed files with 92 additions and 128 deletions
|
@ -1,6 +1,3 @@
|
||||||
syntax: glob
|
syntax: glob
|
||||||
__pycache__/*
|
__pycache__/*
|
||||||
locale/convert_de2en.py
|
locale/convert_de2en.py
|
||||||
build/*
|
|
||||||
mds_cashbook_media.egg-info/*
|
|
||||||
dist/*
|
|
||||||
|
|
|
@ -9,11 +9,12 @@ pip install mds-cashbook-media
|
||||||
|
|
||||||
Requires
|
Requires
|
||||||
========
|
========
|
||||||
- Tryton 7.0
|
- Tryton 6.0
|
||||||
|
|
||||||
Changes
|
Changes
|
||||||
=======
|
=======
|
||||||
|
|
||||||
*7.0.0 - 01.12.2023*
|
*6.0.0 - 14.10.2022*
|
||||||
|
|
||||||
|
- init
|
||||||
|
|
||||||
- ported to Tryton 7.0
|
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
from trytond.pool import Pool
|
from trytond.pool import Pool
|
||||||
from .line import Line
|
from .line import Line
|
||||||
|
|
||||||
|
|
||||||
def register():
|
def register():
|
||||||
Pool.register(
|
Pool.register(
|
||||||
Line,
|
Line,
|
||||||
|
|
60
line.py
60
line.py
|
@ -3,13 +3,13 @@
|
||||||
# The COPYRIGHT file at the top level of this repository contains the
|
# The COPYRIGHT file at the top level of this repository contains the
|
||||||
# full copyright notices and license terms.
|
# full copyright notices and license terms.
|
||||||
|
|
||||||
import mimetypes
|
import mimetypes, magic
|
||||||
import magic
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from PIL import Image, UnidentifiedImageError
|
from PIL import Image
|
||||||
from trytond.model import fields
|
from trytond.model import fields
|
||||||
from trytond.pool import PoolMeta
|
from trytond.pool import Pool, PoolMeta
|
||||||
from trytond.config import config
|
from trytond.config import config
|
||||||
|
from trytond.transaction import Transaction
|
||||||
from trytond.exceptions import UserError
|
from trytond.exceptions import UserError
|
||||||
from trytond.i18n import gettext
|
from trytond.i18n import gettext
|
||||||
from trytond.pyson import Eval, Bool
|
from trytond.pyson import Eval, Bool
|
||||||
|
@ -24,44 +24,24 @@ try:
|
||||||
image_limit = 100
|
image_limit = 100
|
||||||
if image_limit > 10000:
|
if image_limit > 10000:
|
||||||
image_limit = 10000
|
image_limit = 10000
|
||||||
except Exception:
|
except :
|
||||||
image_limit = 2000
|
image_limit = 2000
|
||||||
|
|
||||||
|
|
||||||
STATES2 = {}
|
|
||||||
STATES2.update(STATES)
|
|
||||||
DEPENDS2 = []
|
|
||||||
DEPENDS2.extend(DEPENDS)
|
|
||||||
|
|
||||||
|
|
||||||
class Line(metaclass=PoolMeta):
|
class Line(metaclass=PoolMeta):
|
||||||
__name__ = 'cashbook.line'
|
__name__ = 'cashbook.line'
|
||||||
|
|
||||||
media = fields.Binary(
|
media = fields.Binary(string='Image of PDF', filename='media_name',
|
||||||
string='Image of PDF', filename='media_name', file_id='media_id',
|
file_id='media_id', store_prefix=store_prefix,
|
||||||
store_prefix=store_prefix, states=STATES2, depends=DEPENDS2)
|
states=STATES, depends=DEPENDS)
|
||||||
media_name = fields.Char(
|
media_name = fields.Char(string='File name',
|
||||||
string='File name',
|
|
||||||
states={
|
states={
|
||||||
'required': Bool(Eval('media')),
|
'required': Bool(Eval('media')),
|
||||||
'readonly': STATES2['readonly'],
|
'readonly': STATES['readonly'],
|
||||||
}, depends=DEPENDS2)
|
}, depends=DEPENDS)
|
||||||
media_id = fields.Char(string='File ID', readonly=True)
|
media_id = fields.Char(string='File ID', readonly=True)
|
||||||
media_mime = fields.Char(string='MIME', readonly=True)
|
media_mime = fields.Char(string='MIME', readonly=True)
|
||||||
media_size = fields.Integer(string='File size', readonly=True)
|
media_size = fields.Integer(string='File size', readonly=True)
|
||||||
media_image = fields.Function(fields.Binary(
|
|
||||||
string='Image', readonly=True,
|
|
||||||
states={
|
|
||||||
'invisible': ~Eval('media_mime', '').in_([
|
|
||||||
'image/png', 'image/jpg', 'image/jpeg']),
|
|
||||||
}, depends=['media_mime']), 'on_change_with_media_image')
|
|
||||||
|
|
||||||
@fields.depends('media', 'media_mime')
|
|
||||||
def on_change_with_media_image(self, name=True):
|
|
||||||
""" return binary if its a image
|
|
||||||
"""
|
|
||||||
if (self.media_mime or '-').startswith('image/'):
|
|
||||||
return self.media
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def _identify_file(cls, data, mime=True):
|
def _identify_file(cls, data, mime=True):
|
||||||
|
@ -85,10 +65,7 @@ class Line(metaclass=PoolMeta):
|
||||||
"""
|
"""
|
||||||
image_data2 = None
|
image_data2 = None
|
||||||
with BytesIO(image_data) as fhdl:
|
with BytesIO(image_data) as fhdl:
|
||||||
try:
|
|
||||||
image = Image.open(fhdl, 'r')
|
image = Image.open(fhdl, 'r')
|
||||||
except UnidentifiedImageError:
|
|
||||||
raise UserError(gettext('cashbook_media.msg_file_unknown_type'))
|
|
||||||
|
|
||||||
(width, height) = image.size
|
(width, height) = image.size
|
||||||
if (width > image_limit) or (height > image_limit):
|
if (width > image_limit) or (height > image_limit):
|
||||||
|
@ -121,14 +98,12 @@ class Line(metaclass=PoolMeta):
|
||||||
else :
|
else :
|
||||||
values['media_mime'] = cls._identify_file(values['media'][:1024])
|
values['media_mime'] = cls._identify_file(values['media'][:1024])
|
||||||
|
|
||||||
# if its a image, resize it to fit
|
# if its a image, resize it to fit in (image_limit x image_limit) pixel
|
||||||
# in (image_limit x image_limit) pixel
|
|
||||||
if values['media_mime'].startswith('image'):
|
if values['media_mime'].startswith('image'):
|
||||||
new_image = cls.resize_image_file(values['media'])
|
new_image = cls.resize_image_file(values['media'])
|
||||||
if new_image is not None:
|
if new_image is not None:
|
||||||
values['media'] = new_image
|
values['media'] = new_image
|
||||||
values['media_mime'] = cls._identify_file(
|
values['media_mime'] = cls._identify_file(values['media'][:1024])
|
||||||
values['media'][:1024])
|
|
||||||
|
|
||||||
values['media_size'] = len(values['media'])
|
values['media_size'] = len(values['media'])
|
||||||
file_ext = mimetypes.guess_extension(values['media_mime'])
|
file_ext = mimetypes.guess_extension(values['media_mime'])
|
||||||
|
@ -151,14 +126,15 @@ class Line(metaclass=PoolMeta):
|
||||||
if line.media_size > 1024*1024*5:
|
if line.media_size > 1024*1024*5:
|
||||||
raise UserError(gettext(
|
raise UserError(gettext(
|
||||||
'cashbook_media.msg_file_too_big',
|
'cashbook_media.msg_file_too_big',
|
||||||
recname=line.rec_name))
|
recname = line.rec_name,
|
||||||
if line.media_mime not in [
|
))
|
||||||
'application/pdf',
|
if not line.media_mime in ['application/pdf',
|
||||||
'image/png', 'image/jpg', 'image/jpeg']:
|
'image/png', 'image/jpg', 'image/jpeg']:
|
||||||
raise UserError(gettext(
|
raise UserError(gettext(
|
||||||
'cashbook_media.msg_file_invalid_mime',
|
'cashbook_media.msg_file_invalid_mime',
|
||||||
recname = line.rec_name,
|
recname = line.rec_name,
|
||||||
fmime=line.media_mime))
|
fmime = line.media_mime,
|
||||||
|
))
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, vlist):
|
def create(cls, vlist):
|
||||||
|
|
|
@ -14,10 +14,6 @@ msgctxt "model:ir.message,text:msg_file_invalid_mime"
|
||||||
msgid "The file type '%(fmime)s' of the record '%(recname)s' is not allowed. (allowed: PNG, JPG, PDF)"
|
msgid "The file type '%(fmime)s' of the record '%(recname)s' is not allowed. (allowed: PNG, JPG, PDF)"
|
||||||
msgstr "Der Dateityp '%(fmime)s' des Datensatzes '%(recname)s' ist nicht zugelassen. (erlaubt: PNG, JPG, PDF)"
|
msgstr "Der Dateityp '%(fmime)s' des Datensatzes '%(recname)s' ist nicht zugelassen. (erlaubt: PNG, JPG, PDF)"
|
||||||
|
|
||||||
msgctxt "model:ir.message,text:msg_file_unknown_type"
|
|
||||||
msgid "cannot identify image file"
|
|
||||||
msgstr "Bilddatei kann nicht identifiziert werden"
|
|
||||||
|
|
||||||
|
|
||||||
#################
|
#################
|
||||||
# cashbook.line #
|
# cashbook.line #
|
||||||
|
@ -46,6 +42,3 @@ msgctxt "field:cashbook.line,media_size:"
|
||||||
msgid "File size"
|
msgid "File size"
|
||||||
msgstr "Dateigröße"
|
msgstr "Dateigröße"
|
||||||
|
|
||||||
msgctxt "field:cashbook.line,media_image:"
|
|
||||||
msgid "Image"
|
|
||||||
msgstr "Bild"
|
|
||||||
|
|
|
@ -10,10 +10,6 @@ msgctxt "model:ir.message,text:msg_file_invalid_mime"
|
||||||
msgid "The file type '%(fmime)s' of the record '%(recname)s' is not allowed. (allowed: PNG, JPG, PDF)"
|
msgid "The file type '%(fmime)s' of the record '%(recname)s' is not allowed. (allowed: PNG, JPG, PDF)"
|
||||||
msgstr "The file type '%(fmime)s' of the record '%(recname)s' is not allowed. (allowed: PNG, JPG, PDF)"
|
msgstr "The file type '%(fmime)s' of the record '%(recname)s' is not allowed. (allowed: PNG, JPG, PDF)"
|
||||||
|
|
||||||
msgctxt "model:ir.message,text:msg_file_unknown_type"
|
|
||||||
msgid "cannot identify image file"
|
|
||||||
msgstr "cannot identify image file"
|
|
||||||
|
|
||||||
msgctxt "view:cashbook.line:"
|
msgctxt "view:cashbook.line:"
|
||||||
msgid "Image/PDF"
|
msgid "Image/PDF"
|
||||||
msgstr "Image/PDF"
|
msgstr "Image/PDF"
|
||||||
|
|
|
@ -11,9 +11,6 @@ full copyright notices and license terms. -->
|
||||||
<record model="ir.message" id="msg_file_invalid_mime">
|
<record model="ir.message" id="msg_file_invalid_mime">
|
||||||
<field name="text">The file type '%(fmime)s' of the record '%(recname)s' is not allowed. (allowed: PNG, JPG, PDF)</field>
|
<field name="text">The file type '%(fmime)s' of the record '%(recname)s' is not allowed. (allowed: PNG, JPG, PDF)</field>
|
||||||
</record>
|
</record>
|
||||||
<record model="ir.message" id="msg_file_unknown_type">
|
|
||||||
<field name="text">cannot identify image file</field>
|
|
||||||
</record>
|
|
||||||
|
|
||||||
</data>
|
</data>
|
||||||
</tryton>
|
</tryton>
|
||||||
|
|
33
setup.py
33
setup.py
|
@ -2,7 +2,7 @@
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# Always prefer setuptools over distutils
|
# Always prefer setuptools over distutils
|
||||||
from setuptools import setup
|
from setuptools import setup, find_packages
|
||||||
# To use a consistent encoding
|
# To use a consistent encoding
|
||||||
from codecs import open
|
from codecs import open
|
||||||
from os import path
|
from os import path
|
||||||
|
@ -39,7 +39,7 @@ with open(path.join(here, 'versiondep.txt'), encoding='utf-8') as f:
|
||||||
modversion[l2[0]] = {'min':l2[1], 'max':l2[2], 'prefix':l2[3]}
|
modversion[l2[0]] = {'min':l2[1], 'max':l2[2], 'prefix':l2[3]}
|
||||||
|
|
||||||
# tryton-version
|
# tryton-version
|
||||||
major_version = 7
|
major_version = 6
|
||||||
minor_version = 0
|
minor_version = 0
|
||||||
|
|
||||||
requires = ['python-magic>=0.4.24', 'Pillow']
|
requires = ['python-magic>=0.4.24', 'Pillow']
|
||||||
|
@ -51,32 +51,23 @@ for dep in info.get('depends', []):
|
||||||
prefix = modversion[dep]['prefix']
|
prefix = modversion[dep]['prefix']
|
||||||
|
|
||||||
if len(modversion[dep]['max']) > 0:
|
if len(modversion[dep]['max']) > 0:
|
||||||
requires.append(
|
requires.append('%s_%s >= %s, <= %s' %
|
||||||
'%s_%s >= %s, <= %s' %
|
(prefix, dep, modversion[dep]['min'], modversion[dep]['max']))
|
||||||
(prefix, dep,
|
|
||||||
modversion[dep]['min'],
|
|
||||||
modversion[dep]['max']))
|
|
||||||
else :
|
else :
|
||||||
requires.append(
|
requires.append('%s_%s >= %s' %
|
||||||
'%s_%s >= %s' %
|
|
||||||
(prefix, dep, modversion[dep]['min']))
|
(prefix, dep, modversion[dep]['min']))
|
||||||
else :
|
else :
|
||||||
requires.append(
|
requires.append('%s_%s >= %s.%s, < %s.%s' %
|
||||||
'%s_%s >= %s.%s, < %s.%s' % (
|
('trytond', dep, major_version, minor_version,
|
||||||
'trytond', dep, major_version, minor_version,
|
|
||||||
major_version, minor_version + 1))
|
major_version, minor_version + 1))
|
||||||
requires.append(
|
requires.append('trytond >= %s.%s, < %s.%s' %
|
||||||
'trytond >= %s.%s, < %s.%s' % (
|
(major_version, minor_version, major_version, minor_version + 1))
|
||||||
major_version, minor_version, major_version, minor_version + 1))
|
|
||||||
|
|
||||||
setup(
|
setup(name='%s_%s' % (PREFIX, MODULE),
|
||||||
name='%s_%s' % (PREFIX, MODULE),
|
|
||||||
version=info.get('version', '0.0.1'),
|
version=info.get('version', '0.0.1'),
|
||||||
description='Tryton module to add a file-field to cashbook.',
|
description='Tryton module to add a file-field to cashbook.',
|
||||||
long_description=long_description,
|
long_description=long_description,
|
||||||
long_description_content_type='text/x-rst',
|
|
||||||
url='https://www.m-ds.de/',
|
url='https://www.m-ds.de/',
|
||||||
download_url='https://scmdev.m-ds.de/Tryton/Extra/cashbook_media',
|
|
||||||
author='martin-data services',
|
author='martin-data services',
|
||||||
author_email='service@m-ds.de',
|
author_email='service@m-ds.de',
|
||||||
license='GPL-3',
|
license='GPL-3',
|
||||||
|
@ -96,7 +87,6 @@ setup(
|
||||||
'License :: OSI Approved :: GNU General Public License (GPL)',
|
'License :: OSI Approved :: GNU General Public License (GPL)',
|
||||||
'Programming Language :: Python :: 3.7',
|
'Programming Language :: Python :: 3.7',
|
||||||
'Programming Language :: Python :: 3.8',
|
'Programming Language :: Python :: 3.8',
|
||||||
'Programming Language :: Python :: 3.9',
|
|
||||||
],
|
],
|
||||||
|
|
||||||
keywords='tryton cashbook pdf png image file',
|
keywords='tryton cashbook pdf png image file',
|
||||||
|
@ -105,8 +95,7 @@ setup(
|
||||||
'trytond.modules.%s' % MODULE,
|
'trytond.modules.%s' % MODULE,
|
||||||
],
|
],
|
||||||
package_data={
|
package_data={
|
||||||
'trytond.modules.%s' % MODULE: (
|
'trytond.modules.%s' % MODULE: (info.get('xml', [])
|
||||||
info.get('xml', [])
|
|
||||||
+ ['tryton.cfg', 'locale/*.po', 'tests/*.py',
|
+ ['tryton.cfg', 'locale/*.po', 'tests/*.py',
|
||||||
'view/*.xml', 'versiondep.txt', 'README.rst']),
|
'view/*.xml', 'versiondep.txt', 'README.rst']),
|
||||||
},
|
},
|
||||||
|
|
|
@ -1,2 +1,24 @@
|
||||||
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
|
||||||
# this repository contains the full copyright notices and license terms.
|
# this repository contains the full copyright notices and license terms.
|
||||||
|
|
||||||
|
import trytond.tests.test_tryton
|
||||||
|
import unittest
|
||||||
|
|
||||||
|
from trytond.modules.cashbook_media.tests.test_line import LineTestCase
|
||||||
|
|
||||||
|
|
||||||
|
__all__ = ['suite']
|
||||||
|
|
||||||
|
|
||||||
|
class CashbookTestCase(\
|
||||||
|
LineTestCase,
|
||||||
|
):
|
||||||
|
'Test cashbook module'
|
||||||
|
module = 'cashbook_media'
|
||||||
|
|
||||||
|
# end CashbookTestCase
|
||||||
|
|
||||||
|
def suite():
|
||||||
|
suite = trytond.tests.test_tryton.suite()
|
||||||
|
suite.addTests(unittest.TestLoader().loadTestsFromTestCase(CashbookTestCase))
|
||||||
|
return suite
|
||||||
|
|
|
@ -5,10 +5,11 @@
|
||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from trytond.tests.test_tryton import with_transaction
|
from trytond.tests.test_tryton import ModuleTestCase, with_transaction
|
||||||
from trytond.pool import Pool
|
from trytond.pool import Pool
|
||||||
|
from trytond.transaction import Transaction
|
||||||
from trytond.exceptions import UserError
|
from trytond.exceptions import UserError
|
||||||
from trytond.modules.cashbook.tests.test_module import CashbookTestCase
|
from trytond.modules.cashbook.tests import CashbookTestCase
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from decimal import Decimal
|
from decimal import Decimal
|
||||||
from .img_data import img_data_png, dok_data_pdf, text_data
|
from .img_data import img_data_png, dok_data_pdf, text_data
|
||||||
|
@ -125,11 +126,8 @@ class LineTestCase(CashbookTestCase):
|
||||||
self.assertEqual(book.state, 'open')
|
self.assertEqual(book.state, 'open')
|
||||||
|
|
||||||
# add invalid file
|
# add invalid file
|
||||||
self.assertRaisesRegex(
|
self.assertRaisesRegex(UserError,
|
||||||
UserError,
|
"The file type 'text/plain' of the record '05/02/2022|Rev|1.00 usd|Text 2 [Cat1]' is not allowed. (allowed: PNG, JPG, PDF)",
|
||||||
"The file type 'text/plain' of the record " +
|
|
||||||
"'05/02/2022|Rev|1.00 usd|Text 2 [Cat1]' is not allowed. " +
|
|
||||||
"(allowed: PNG, JPG, PDF)",
|
|
||||||
Book.write,
|
Book.write,
|
||||||
*[
|
*[
|
||||||
[book],
|
[book],
|
||||||
|
@ -148,11 +146,8 @@ class LineTestCase(CashbookTestCase):
|
||||||
])
|
])
|
||||||
|
|
||||||
# replace image at line-1 by invalid file
|
# replace image at line-1 by invalid file
|
||||||
self.assertRaisesRegex(
|
self.assertRaisesRegex(UserError,
|
||||||
UserError,
|
"The file type 'text/plain' of the record '05/02/2022|Rev|1.00 usd|Text 2 [Cat1]' is not allowed. (allowed: PNG, JPG, PDF)",
|
||||||
"The file type 'text/plain' of the record " +
|
|
||||||
"'05/02/2022|Rev|1.00 usd|Text 2 [Cat1]' is not allowed. " +
|
|
||||||
"(allowed: PNG, JPG, PDF)",
|
|
||||||
Lines.write,
|
Lines.write,
|
||||||
*[
|
*[
|
||||||
[book.lines[0]],
|
[book.lines[0]],
|
||||||
|
@ -168,6 +163,7 @@ class LineTestCase(CashbookTestCase):
|
||||||
"""
|
"""
|
||||||
pool = Pool()
|
pool = Pool()
|
||||||
Book = pool.get('cashbook.book')
|
Book = pool.get('cashbook.book')
|
||||||
|
Lines = pool.get('cashbook.line')
|
||||||
|
|
||||||
types = self.prep_type()
|
types = self.prep_type()
|
||||||
category = self.prep_category(cattype='in')
|
category = self.prep_category(cattype='in')
|
||||||
|
@ -230,6 +226,3 @@ class LineTestCase(CashbookTestCase):
|
||||||
self.assertEqual(img2.size, (2000, 837))
|
self.assertEqual(img2.size, (2000, 837))
|
||||||
|
|
||||||
# end LineTestCase
|
# end LineTestCase
|
||||||
|
|
||||||
|
|
||||||
del CashbookTestCase
|
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
[tryton]
|
[tryton]
|
||||||
version=7.0.0
|
version=6.0.0
|
||||||
depends:
|
depends:
|
||||||
cashbook
|
cashbook
|
||||||
xml:
|
xml:
|
||||||
message.xml
|
message.xml
|
||||||
line.xml
|
line.xml
|
||||||
|
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
cashbook;7.0.31;7.0.999;mds
|
cashbook;6.0.18;6.0.999;mds
|
||||||
|
|
|
@ -13,10 +13,10 @@ full copyright notices and license terms. -->
|
||||||
|
|
||||||
<label name="media_name"/>
|
<label name="media_name"/>
|
||||||
<field name="media_name"/>
|
<field name="media_name"/>
|
||||||
|
|
||||||
<label name="media_mime"/>
|
<label name="media_mime"/>
|
||||||
<field name="media_mime"/>
|
<field name="media_mime"/>
|
||||||
|
|
||||||
<field name="media_image" readonly="1" colspan="4" widget="image" xexpand="1" yexpand="1"/>
|
|
||||||
</page>
|
</page>
|
||||||
|
|
||||||
</xpath>
|
</xpath>
|
||||||
|
|
Loading…
Reference in a new issue