Updated the validator to check for unique sets as well as URL.

This commit is contained in:
Mark Winterbottom 2015-10-29 18:30:51 +00:00
parent 39ce744368
commit 2c41293c9c
1 changed files with 26 additions and 6 deletions

View File

@ -60,6 +60,11 @@ def _normalize_url(url):
def harvest_source_url_validator(key, data, errors, context): def harvest_source_url_validator(key, data, errors, context):
"""Validate the provided harvest source URL.
Checks that the URL is not already existing with the same config.
"""
package = context.get("package") package = context.get("package")
if package: if package:
@ -67,21 +72,36 @@ def harvest_source_url_validator(key, data, errors, context):
else: else:
package_id = data.get(key[:-1] + ("id",)) package_id = data.get(key[:-1] + ("id",))
new_url = _normalize_url(data[key]) try:
# pkg_id = data.get(('id',),'') new_config = data.get(key[:-1] + ('config',))
new_config_dict = json.loads(new_config)
new_config_set = new_config_dict.get('set', None)
except:
new_config_set = None
q = model.Session.query(model.Package.url, model.Package.state) \ new_url = _normalize_url(data[key])
# q = model.Session.query(model.Package.url, model.Package.state) \
q = model.Session.query(HarvestSource.url, HarvestSource.config) \
.filter(model.Package.type == DATASET_TYPE_NAME) .filter(model.Package.type == DATASET_TYPE_NAME)
if package_id: if package_id:
# When editing a source we need to avoid its own URL # When editing a source we need to avoid its own URL.
q = q.filter(model.Package.id != package_id) q = q.filter(model.Package.id != package_id)
existing_sources = q.all() existing_sources = q.all()
for url, state in existing_sources: for url, conf in existing_sources:
url = _normalize_url(url) url = _normalize_url(url)
if url == new_url: try:
config_dict = json.loads(conf)
config_set = config_dict.get('set', None)
except:
config_set = None
if url == new_url and config_set == new_config_set:
# You can have a duplicate URL if it's pointing to a unique
# set as it will be harvesting unique datasets.
raise Invalid( raise Invalid(
'There already is a Harvest Source for this URL: %s' 'There already is a Harvest Source for this URL: %s'
% data[key] % data[key]