diff --git a/ckanext/semantic_media_wiki/controllers/media_wiki.py b/ckanext/semantic_media_wiki/controllers/media_wiki.py index 7a2887d..c35a2a8 100644 --- a/ckanext/semantic_media_wiki/controllers/media_wiki.py +++ b/ckanext/semantic_media_wiki/controllers/media_wiki.py @@ -127,7 +127,8 @@ def get_machine_link(id): temp[1] = eq_name results.append(temp) except: - return toolkit.abort(403, "bad request") + raise + # return toolkit.abort(403, "bad request") if len(results) == 0: return '0' diff --git a/ckanext/semantic_media_wiki/libs/media_wiki.py b/ckanext/semantic_media_wiki/libs/media_wiki.py index 11ba623..25d6ca8 100644 --- a/ckanext/semantic_media_wiki/libs/media_wiki.py +++ b/ckanext/semantic_media_wiki/libs/media_wiki.py @@ -57,9 +57,12 @@ def update_resource_machine(request, resources_len, package): updated_at = _time.now() for entry in resources_checkbox_list: Id = entry.split('@@@')[0] - old_machine_url = entry.split('@@@')[1] - resource_objects = ResourceEquipmentLink(resource_id=Id).get_by_resource(id=Id) - if not resource_objects: + if len(entry.split('@@@')) == 2: + old_machine_url = entry.split('@@@')[1] + else: + old_machine_url = '' + resource_record = ResourceEquipmentLink(resource_id=Id).get_by_resource_machine(id=Id, machine_url=old_machine_url) + if not resource_record: # resource link does not exist --> add a new one create_at = _time.now() updated_at = create_at @@ -70,32 +73,31 @@ def update_resource_machine(request, resources_len, package): else: already_edited_resources[Id] = [link] continue - - for resource_obj in resource_objects: - if resource_obj.url == old_machine_url: - resource_obj.url = link - resource_obj.link_name = machine_name - resource_obj.updated_at = updated_at - resource_obj.commit() - if Id in already_edited_resources.keys(): - already_edited_resources[Id].append(link) - else: - already_edited_resources[Id] = [link] + + resource_record.url = link + resource_record.link_name = machine_name + resource_record.updated_at = updated_at + resource_record.commit() + if Id in already_edited_resources.keys(): + already_edited_resources[Id].append(link) + else: + already_edited_resources[Id] = [link] for res in package['resources']: resource_objects = ResourceEquipmentLink(resource_id=res['id']).get_by_resource(id=res['id']) if resource_objects: for record in resource_objects: - if res['id'] not in already_edited_resources.keys(): + if record.resource_id not in already_edited_resources.keys(): record.delete() record.commit() - elif record.url not in already_edited_resources[res['id']]: + elif record.url not in already_edited_resources[res['id']]: record.delete() record.commit() except: - return False + raise + # return False return True @@ -104,7 +106,7 @@ def get_machine_link(resource_id): res_object = ResourceEquipmentLink(resource_id=resource_id) results = res_object.get_by_resource(id=resource_id) urls = {} - if results != false: + if results: for record in results: if record.url != '0' and record.link_name != '': urls[record.link_name] = record.url diff --git a/ckanext/semantic_media_wiki/models/resource_mediawiki_link.py b/ckanext/semantic_media_wiki/models/resource_mediawiki_link.py index d754a2b..1e9c9b8 100644 --- a/ckanext/semantic_media_wiki/models/resource_mediawiki_link.py +++ b/ckanext/semantic_media_wiki/models/resource_mediawiki_link.py @@ -28,6 +28,20 @@ def __init__(self, resource_id=None, url=None, link_name=None, create_at=None, u self.create_at = create_at self.updated_at = updated_at + @classmethod + def get_by_resource_machine(cls, id, machine_url, autoflush=True): + if not id: + return None + + exists = meta.Session.query(cls).filter(cls.resource_id==id, cls.url == machine_url).first() is not None + if not exists: + return False + query = meta.Session.query(cls).filter(cls.resource_id==id) + query = query.autoflush(autoflush) + record = query.first() + return record + + @classmethod def get_by_resource(cls, id, autoflush=True): if not id: