diff --git a/ckanext/harvest/harvesters/base.py b/ckanext/harvest/harvesters/base.py index 6f13f53..56d0fb2 100644 --- a/ckanext/harvest/harvesters/base.py +++ b/ckanext/harvest/harvesters/base.py @@ -3,7 +3,6 @@ import re import uuid from sqlalchemy.sql import update,and_, bindparam -from sqlalchemy.exc import InvalidRequestError from pylons import config from ckan import plugins as p @@ -136,31 +135,8 @@ class HarvesterBase(SingletonPlugin): return ideal_name[:PACKAGE_NAME_MAX_LENGTH-APPEND_MAX_CHARS] + \ str(uuid.uuid4())[:APPEND_MAX_CHARS] - - def _save_gather_error(self, message, job): - err = HarvestGatherError(message=message, job=job) - try: - err.save() - except InvalidRequestError: - Session.rollback() - err.save() - finally: - log.error(message) - - - def _save_object_error(self, message, obj, stage=u'Fetch', line=None): - err = HarvestObjectError(message=message, - object=obj, - stage=stage, - line=line) - try: - err.save() - except InvalidRequestError, e: - Session.rollback() - err.save() - finally: - log_message = '{0}, line {1}'.format(message,line) if line else message - log.debug(log_message) + _save_gather_error = HarvestGatherError.create + _save_object_error = HarvestObjectError.create def _get_user_name(self): ''' diff --git a/ckanext/harvest/model/__init__.py b/ckanext/harvest/model/__init__.py index 5a813a5..a87eb58 100644 --- a/ckanext/harvest/model/__init__.py +++ b/ckanext/harvest/model/__init__.py @@ -10,6 +10,7 @@ from sqlalchemy import ForeignKey from sqlalchemy import types from sqlalchemy.engine.reflection import Inspector from sqlalchemy.orm import backref, relation +from sqlalchemy.exc import InvalidRequestError from ckan import model from ckan import logic @@ -153,13 +154,43 @@ class HarvestGatherError(HarvestDomainObject): '''Gather errors are raised during the **gather** stage of a harvesting job. ''' - pass + @classmethod + def create(cls, message, job): + ''' + Helper function to create an error object and save it. + ''' + err = cls(message=message, job=job) + try: + err.save() + except InvalidRequestError: + Session.rollback() + err.save() + finally: + # No need to alert administrator so don't log as an error + log.info(message) + class HarvestObjectError(HarvestDomainObject): '''Object errors are raised during the **fetch** or **import** stage of a harvesting job, and are referenced to a specific harvest object. ''' - pass + @classmethod + def create(cls, message, object, stage=u'Fetch', line=None): + ''' + Helper function to create an error object and save it. + ''' + err = cls(message=message, object=object, + stage=stage, line=line) + try: + err.save() + except InvalidRequestError: + Session.rollback() + err.save() + finally: + log_message = '{0}, line {1}'.format(message, line) \ + if line else message + log.debug(log_message) + def harvest_object_before_insert_listener(mapper,connection,target): '''