From 4c81c7c3a7591f5076a93c7c1e2466b55a58d9ba Mon Sep 17 00:00:00 2001 From: amercader Date: Wed, 1 Feb 2012 12:52:52 +0000 Subject: [PATCH] [#1726][model] Harvest source reference compatibility The 'source' property of harvest objects now comes from the actual foreign key. For compatibility with old harvesters, an before insert event listener has beeen added to check if the source id has been set, and set it automatically from the job if not. Note that this requires SQLAlchemy 0.7 (ie CKAN 1.5.1) --- ckanext/harvest/model/__init__.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/ckanext/harvest/model/__init__.py b/ckanext/harvest/model/__init__.py index a372de4..205873e 100644 --- a/ckanext/harvest/model/__init__.py +++ b/ckanext/harvest/model/__init__.py @@ -1,6 +1,7 @@ import logging import datetime +from sqlalchemy import event from sqlalchemy import distinct from sqlalchemy.engine.reflection import Inspector from sqlalchemy.orm import backref, relation @@ -120,10 +121,6 @@ class HarvestObject(HarvestDomainObject): ''' - @property - def source(self): - return self.job.source - class HarvestGatherError(HarvestDomainObject): '''Gather errors are raised during the **gather** stage of a harvesting job. @@ -136,6 +133,18 @@ class HarvestObjectError(HarvestDomainObject): ''' pass +def harvest_object_before_insert_listener(mapper,connection,target): + ''' + For compatibility with old harvesters, check if the source id has + been set, and set it automatically from the job if not. + ''' + if not target.harvest_source_id or not target.source: + if not target.job: + raise Exception('You must define a Harvest Job for each Harvest Object') + target.source = target.job.source + target.harvest_source_id = target.job.source.id + + def define_harvester_tables(): global harvest_source_table @@ -228,6 +237,12 @@ def define_harvester_tables(): lazy=True, backref=u'objects', ), + 'source': relation( + HarvestSource, + lazy=True, + backref=u'objects', + ), + }, ) @@ -253,6 +268,7 @@ def define_harvester_tables(): }, ) + event.listen(HarvestObject, 'before_insert', harvest_object_before_insert_listener) def migrate_v2(): log.debug('Migrating harvest tables to v2. This may take a while...')