Add exception messages

This commit is contained in:
Sergi Almacellas Abellana 2020-06-22 15:59:49 +02:00
parent a25272347b
commit 7d7bf4d635
5 changed files with 64 additions and 19 deletions

11
exceptions.py Normal file
View File

@ -0,0 +1,11 @@
# The COPYRIGHT file at the top level of this repository contains the full
# copyright notices and license terms.
from trytond.exceptions import UserError
class WooCommerceError(UserError):
pass
class MissingParentsError(UserError):
pass

View File

@ -34,6 +34,18 @@ msgctxt "model:ir.message,text:msg_id_record_unique"
msgid "The WooCommerce ID must be unique for each record and shop."
msgstr "El ID de WooCommerce ha de ser únic per cada registre i tenda."
msgctxt "model:ir.message,text:msg_missing_parents_error"
msgid ""
"In order to sincronize \"%(records)s\" you must also sincronize it's parent "
"records."
msgstr ""
"Per sincronitzar els registres \"%(records)s\" heu de sincronitzar també els"
" seus registres pare."
msgctxt "model:ir.message,text:msg_sincronization_error"
msgid "Error while sincronizing woocommerce records: \"%(response)s\"."
msgstr "Error al sincronizar els registres de woocommernce: %(response)s\"."
msgctxt "model:ir.message,text:msg_woocommerce_id"
msgid "WooCoomerce ID"
msgstr "ID WooCommerce"

View File

@ -34,6 +34,18 @@ msgctxt "model:ir.message,text:msg_id_record_unique"
msgid "The WooCommerce ID must be unique for each record and shop."
msgstr "El ID de WooCommerce debe ser único por registro y tienda."
msgctxt "model:ir.message,text:msg_missing_parents_error"
msgid ""
"In order to sincronize \"%(records)s\" you must also sincronize it's parent "
"records."
msgstr ""
"Para sincronizar los registros \"%(records)s\" debeis sincronizar también "
"sus registros padre."
msgctxt "model:ir.message,text:msg_sincronization_error"
msgid "Error while sincronizing woocommerce records: \"%(response)s\"."
msgstr "Error al sincronizar los registros de woocommernce: %(response)s\"."
msgctxt "model:ir.message,text:msg_woocommerce_id"
msgid "WooCoomerce ID"
msgstr "ID WooCommerce"

View File

@ -9,5 +9,11 @@ this repository contains the full copyright notices and license terms. -->
<record model="ir.message" id="msg_woocommerce_id">
<field name="text">WooCoomerce ID</field>
</record>
<record model="ir.message" id="msg_missing_parents_error">
<field name="text">In order to sincronize "%(records)s" you must also sincronize it's parent records.</field>
</record>
<record model="ir.message" id="msg_sincronization_error">
<field name="text">Error while sincronizing woocommerce records: "%(response)s".</field>
</record>
</data>
</tryton>

42
web.py
View File

@ -6,7 +6,7 @@ import logging
from woocommerce import API
from trytond.exceptions import UserError
from trytond.i18n import lazy_gettext
from trytond.i18n import gettext, lazy_gettext
from trytond.model import Model, ModelSQL, Unique, fields
from trytond.pyson import Eval
from trytond.pool import PoolMeta, Pool
@ -14,6 +14,8 @@ from trytond.tools import grouped_slice
from trytond.transaction import Transaction
from trytond.modules.product import round_price
from .exceptions import WooCommerceError, MissingParentsError
logger = logging.getLogger(__name__)
@ -172,7 +174,9 @@ class Shop(metaclass=PoolMeta):
def woocommerce_response(cls, request):
response = request.json()
if 'message' in response:
raise Exception(response['message'])
raise WooCommerceError(
gettext('web_shop_woocommerce.msg_sincronization_error',
response=response['message']))
return response
def woocommerce_tryton_record(self, model, woocommerce_id):
@ -225,24 +229,24 @@ class Shop(metaclass=PoolMeta):
latter = []
while records:
with Transaction().new_transaction() as t:
for record in records:
entity = record.get_woocommerce_entity()
if entity is None:
latter.append(record)
continue
woo_id = record.woocommerce_id
if not woo_id:
response = self.woocommerce_response(
wcapi.post(endpoint, entity))
record.woocommerce_id = response['id']
else:
to_update[woo_id] = entity
Model.save(records)
t.commit()
for record in records:
entity = record.get_woocommerce_entity()
if entity is None:
latter.append(record)
continue
woo_id = record.woocommerce_id
if not woo_id:
response = self.woocommerce_response(
wcapi.post(endpoint, entity))
record.woocommerce_id = response['id']
else:
to_update[woo_id] = entity
Model.save(records)
Transaction().commit()
if latter and len(records) == len(latter):
raise Exception('Missing parent records: %s',
[x.rec_name for x in latter])
raise MissingParentsError(
gettext('web_shop_woocommerce.msg_missing_parents_error',
records=','.join([x.rec_name for x in latter])))
logger.info("Created new records %d/%d", len(records), len(latter))
records = latter
latter = []