Skip to content

Commit

Permalink
Merge branch 'dev' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
Pooya-Oladazimi committed Jan 26, 2022
2 parents 3b7ac58 + 4b814ee commit d4ff7ae
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 77 deletions.
46 changes: 25 additions & 21 deletions ckanext/semantic_media_wiki/controllers/media_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ def edit_machines_view(id):
resource_machine_data = {}
machine_link_name = {}
for resource in package['resources']:
record = Helper.get_machine_link(resource['id'])
if record and record.url not in resource_machine_data.keys():
resource_machine_data[record.url] = [resource['id']]
machine_link_name[record.url] = record.link_name
elif record:
resource_machine_data[record.url].append(resource['id'])
urls = Helper.get_machine_link(resource['id'])
for name, url in urls.items():
if url not in resource_machine_data.keys():
resource_machine_data[url] = [resource['id']]
machine_link_name[url] = name
else:
resource_machine_data[url].append(resource['id'])

return render_template('edit_machines.html',
pkg_dict=package,
Expand Down Expand Up @@ -106,25 +107,28 @@ def edit_save():
def get_machine_link(id):
if not toolkit.g.user:
return toolkit.abort(403, "You are not authorized to access this function" )

try:
package = toolkit.get_action('package_show')({}, {'name_or_id': id})
machine_links = []
results = []
for res in package['resources']:
record = Helper.get_machine_link(res['id'])
if not record or record.url == '0':
machien_urls = Helper.get_machine_link(res['id'])
if len(machien_urls.keys()) == 0:
continue
if record.url not in machine_links:
machine_links.append(record.url)
if not record.link_name or record.link_name == '':
record.link_name = "Link to the Equipment"
temp = ['', '']
temp[0] = record.url
temp[1] = record.link_name
results.append(temp)
for name, link in machien_urls.items():
if link not in machine_links:
machine_links.append(link)
eq_name = name
if name == '':
eq_name = "Link to the Equipment"
temp = ['', '']
temp[0] = link
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 All @@ -133,10 +137,10 @@ def get_machine_link(id):


def get_resource_machine(id):
record = Helper.get_machine_link(id)
if not record or record.url == '0':
urls = Helper.get_machine_link(id)
if len(urls.keys()) == 0:
return '0'
return json.dumps(record.url)
return json.dumps(urls)



Expand Down
69 changes: 48 additions & 21 deletions ckanext/semantic_media_wiki/libs/media_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ def add_machine_links(request, resources_len):
return true

def update_resource_machine(request, resources_len, package):

try:
already_edited_resources = []
already_edited_resources = {}
for i in range(1, resources_len + 1):
link = request.form.get('machine_link' + str(i))
if link == '0':
Expand All @@ -54,40 +55,66 @@ def update_resource_machine(request, resources_len, package):
machine_name = Helper.get_machine_name(link)
resources_checkbox_list = request.form.getlist('machine_resources_list' + str(i))
updated_at = _time.now()
for Id in resources_checkbox_list:
resource_object = ResourceEquipmentLink(resource_id=Id).get_by_resource(id=Id)
if resource_object == false:
for entry in resources_checkbox_list:
Id = entry.split('@@@')[0]
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
resource_object = ResourceEquipmentLink(Id, link, machine_name, create_at, updated_at)
resource_object.save()
already_edited_resources.append(Id)
if Id in already_edited_resources.keys():
already_edited_resources[Id].append(link)
else:
already_edited_resources[Id] = [link]
continue
resource_object.url = link
resource_object.link_name = machine_name
resource_object.updated_at = updated_at
resource_object.commit()
already_edited_resources.append(Id)

for res in package['resources']:
resource_object = ResourceEquipmentLink(resource_id=res['id']).get_by_resource(id=res['id'])
if resource_object != false and res['id'] not in already_edited_resources:
resource_object.delete()
resource_object.commit()

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 record.resource_id not in already_edited_resources.keys():
record.delete()
record.commit()
elif record.url not in already_edited_resources[res['id']]:
record.delete()
record.commit()

except:
return False
raise
# return False

return True


def get_machine_link(resource_id):
res_object = ResourceEquipmentLink(resource_id=resource_id)
result = res_object.get_by_resource(id=resource_id)
if result != false and result.url != '0':
return result
return False
results = res_object.get_by_resource(id=resource_id)
urls = {}
if results:
for record in results:
if record.url != '0' and record.link_name != '':
urls[record.link_name] = record.url
elif record.url != '0' and record.link_name == '':
urls[record.url] = record.url
return urls

return {}


def get_machines_list():
Expand Down
18 changes: 16 additions & 2 deletions ckanext/semantic_media_wiki/models/resource_mediawiki_link.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,31 @@ 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:
return None

exists = meta.Session.query(cls).filter(cls.resource_id==id).first() is not None
if not exists:
return false
return False
query = meta.Session.query(cls).filter(cls.resource_id==id)
query = query.autoflush(autoflush)
record = query.first()
record = query
return record


Expand Down
27 changes: 0 additions & 27 deletions ckanext/semantic_media_wiki/public/statics/machines.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,33 +75,6 @@ $(document).ready(function(){
}
});


/**
* hide a resource from other modals when the resource is chosen for one machine in a modal
*
*/

let resources = $('.resource-box');
for(let i=0; i < resources.length; i++){
if($(resources[i]).prop('checked') == true){
$(".checkbox-container[value=" + $(resources[i]).val() + "]").hide();
$(resources[i]).parent().show();
}
}

$('.resource-box').click(function(){
let id = $(this).attr('name');
id = id[id.length - 1];
let resource = $(this).val();
if($(this).prop('checked') == true){
$(".checkbox-container[value=" + resource + "]").hide();
$(this).parent().show();
}
else{
$(".checkbox-container[value=" + resource + "]").show();
}
});

/**
* Click Add button on a modal
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ $(document).ready(function(){
$('#machine_link_box').hide();
}
else{
$('#machine_url_anchor').attr('href', result);
$('#machine_url_anchor').text(result);
$.each(result, function(key,value){
let anchor = '<a href="';
anchor += value;
anchor += ('" target="_blank">' + key + '</a><br>');
$('#equipment_list').append(anchor);
});

}
}
Expand Down
3 changes: 2 additions & 1 deletion ckanext/semantic_media_wiki/templates/edit_machines.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
{% snippet 'snippets/semantic_media_wiki_resources_modal.html',
resources=pkg_dict['resources'],
counter=loop.index,
chosen_resources=value
chosen_resources=value,
the_machine=key
%}
<div class="col-sm-11">
<h4>{{_('Please select an Equipment:')}}</h4>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<input type="hidden" id="get_link_url" value='{{dest_url}}'>

<div id="machine_link_box">
{{_('Equipment URL')}}: <a href="#" id="machine_url_anchor" target="_blank"> </a>
{{_('Linked Equipment')}}: <div id="equipment_list"></div>
</div>
<br><br>
{% endblock %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ <h4>{{_('Please select an Equipment:')}}</h4>
<select name={{'machine_link' ~ (loop.index + offset)}} id={{'machines_dropdown_' ~ (loop.index + offset)}} class="machine_dropdown" >
{% for machine in machines_list %}
<option value={{machine.value}} image={{machine.image}}>
{{machine.text}}
{{machine.text}}
</option>
{% endfor %}
<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ <h4 class="modal-title">Equipment: <div id={{'modalMachineName' ~ counter}}></di
<input type="checkbox"
class="{{'resource-checkbox-input' ~ counter}} resource-box"
name={{'machine_resources_list' ~ counter}}
value={{res.id}}
value={{res.id ~ '@@@' ~ the_machine}}
checked
>
<span class="checkmark"></span>
Expand Down

0 comments on commit d4ff7ae

Please sign in to comment.