Skip to content

Commit

Permalink
add a new model function + modify edit machine saving
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya-Oladazimi committed Jan 21, 2022
1 parent c19c327 commit 4b814ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
3 changes: 2 additions & 1 deletion ckanext/semantic_media_wiki/controllers/media_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
38 changes: 20 additions & 18 deletions ckanext/semantic_media_wiki/libs/media_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions ckanext/semantic_media_wiki/models/resource_mediawiki_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4b814ee

Please sign in to comment.