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 Dec 3, 2021
2 parents 5e5b765 + 97bd605 commit 9736334
Show file tree
Hide file tree
Showing 21 changed files with 710 additions and 305 deletions.
78 changes: 58 additions & 20 deletions ckanext/semantic_media_wiki/controllers/media_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import ckan.plugins.toolkit as toolkit
from flask import render_template, request, redirect
from ckanext.semantic_media_wiki.libs.media_wiki import Helper
from sqlalchemy.sql.expression import false, true
from sqlalchemy.sql.expression import false
import json
import ckan.lib.helpers as h

Expand All @@ -12,6 +12,9 @@
class MediaWikiController():

def machines_view(id):
if not Helper.check_access_edit_package(id):
toolkit.abort(403, 'You are not authorized to access this function')

package = toolkit.get_action('package_show')({}, {'name_or_id': id})
stages = ['complete', 'complete','complete', 'active']
machines, machine_imageUrl = Helper.get_machines_list()
Expand All @@ -23,9 +26,6 @@ def machines_view(id):
)

def save_machines():
if not toolkit.g.user:
return toolkit.abort(403, "You need to authenticate before accessing this function" )

package_name = request.form.get('package')
resources_len = 0
if package_name == None:
Expand All @@ -37,6 +37,9 @@ def save_machines():

except:
return toolkit.abort(400, "Package not found")

if not Helper.check_access_edit_package(package['id']):
return toolkit.abort(403, "You are not authorized to access this function" )

action = request.form.get('save_btn')
if action == 'go-dataset-veiw': # I will add it later button
Expand All @@ -53,38 +56,45 @@ def save_machines():


def edit_machines_view(id):
if not Helper.check_access_edit_package(id):
return toolkit.abort(403, "You are not authorized to access this function" )

package = toolkit.get_action('package_show')({}, {'name_or_id': id})
machines, machine_imageUrl = Helper.get_machines_list()
resource_machine_data = []
resource_machine_data = {}
machine_link_name = {}
for resource in package['resources']:
temp = {}
temp['name'] = resource['name']
temp['id'] = resource['id']
record = Helper.get_machine_link(resource['id'])
temp['machine'] = record.url if record != false else '0'
resource_machine_data.append(temp)
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'])

return render_template('edit_machines.html',
pkg_dict=package,
machines_list=machines,
resource_data=resource_machine_data,
machine_imageUrl=machine_imageUrl
machine_imageUrl=machine_imageUrl,
machines_count=len(resource_machine_data.keys()),
machine_link_name=machine_link_name
)


def edit_save():
if not toolkit.g.user:
return toolkit.abort(403, "You need to authenticate before accessing this function" )

package_name = request.form.get('package')
package = toolkit.get_action('package_show')({}, {'name_or_id': package_name})
if not Helper.check_access_edit_package(package['id']):
return toolkit.abort(403, "You are not authorized to access this function" )

resources_len = int(request.form.get('resources_length'))
action = request.form.get('save_btn')
if action == 'go-dataset-veiw': # cancel button
return redirect(h.url_for('dataset.read', id=str(package_name) , _external=True))

if action == 'update_machine':
result = Helper.update_resource_machine(request, resources_len)
if result != false:
result = Helper.update_resource_machine(request, resources_len, package)
if result:
return redirect(h.url_for('dataset.read', id=str(package_name) , _external=True))

return toolkit.abort(500, "Server issue")
Expand All @@ -95,12 +105,40 @@ def edit_save():

def get_machine_link(id):
if not toolkit.g.user:
return toolkit.abort(403, "You need to authenticate before accessing this function" )
record = Helper.get_machine_link(id)
if record == false or record.url == '0':
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':
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)
except:
return toolkit.abort(403, "bad request")

if len(results) == 0:
return '0'
return json.dumps([record.url, record.link_name])
return json.dumps(results)



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



def cancel_dataset_plugin_is_enabled():
if Helper.check_plugin_enabled('cancel_dataset_creation'):
Expand Down
93 changes: 64 additions & 29 deletions ckanext/semantic_media_wiki/libs/media_wiki.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# encoding: utf-8

from sqlalchemy.sql.expression import false, null, true
from sqlalchemy.sql.expression import false, true
from ckanext.semantic_media_wiki.models.resource_mediawiki_link import ResourceEquipmentLink
from datetime import datetime as _time
from ckanext.semantic_media_wiki.libs.media_wiki_api import API
Expand All @@ -10,58 +10,84 @@

class Helper():

def check_access_edit_package(package_id):
context = {'user': toolkit.g.user, 'auth_user_obj': toolkit.g.userobj}
data_dict = {'id':package_id}
try:
toolkit.check_access('package_update', context, data_dict)
return True

except toolkit.NotAuthorized:
return False


def add_machine_links(request, resources_len):

try:
for i in range(1, resources_len + 1):
resource = request.form.get('resource_' + str(i))
for i in range(1, resources_len + 1):
link = request.form.get('machine_link' + str(i))
if link == '0': # not specified
continue
machine_name = request.form.get('machine_name_' + str(i))
machine_name =request.form.get('machine_name_' + str(i))
if not machine_name or machine_name == '':
machine_name = Helper.get_machine_name(link)
resources_checkbox_list = request.form.getlist('machine_resources_list' + str(i))
create_at = _time.now()
updated_at = create_at
resource_object = ResourceEquipmentLink(resource, link, machine_name, create_at, updated_at)
resource_object.save()
for Id in resources_checkbox_list:
resource_object = ResourceEquipmentLink(Id, link, machine_name, create_at, updated_at)
resource_object.save()
except:
return false

return true


def update_resource_machine(request, resources_len):
def update_resource_machine(request, resources_len, package):
try:
already_edited_resources = []
for i in range(1, resources_len + 1):
resource = request.form.get('resource_' + str(i))
link = request.form.get('machine_link' + str(i))
machine_name = request.form.get('machine_name_' + str(i))
link = request.form.get('machine_link' + str(i))
if link == '0':
machine_name = None
machine_name = request.form.get('machine_name_' + str(i))
if not machine_name or machine_name == '':
machine_name = Helper.get_machine_name(link)
resources_checkbox_list = request.form.getlist('machine_resources_list' + str(i))
updated_at = _time.now()
resource_object = ResourceEquipmentLink(resource_id=resource).get_by_resource(id=resource)
if resource_object == false:
# resource link does not exist --> add a new one
create_at = _time.now()
updated_at = create_at
resource_object = ResourceEquipmentLink(resource, link, machine_name, create_at, updated_at)
resource_object.save()
continue
for Id in resources_checkbox_list:
resource_object = ResourceEquipmentLink(resource_id=Id).get_by_resource(id=Id)
if resource_object == false:
# 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)
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_object.url = link
resource_object.link_name = machine_name
resource_object.updated_at = updated_at
resource_object.commit()
except:
return false
return False

return true
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:
if result != false and result.url != '0':
return result
return false
return False


def get_machines_list():
Expand All @@ -84,7 +110,7 @@ def get_machines_list():
if results and len(results) > 0:
temp = {}
temp['value'] = '0'
temp['text'] = 'Not selected'
temp['text'] = 'None selected'
machines_list.append(temp)
for machine in results:
temp = {}
Expand All @@ -101,6 +127,15 @@ def get_machines_list():
return [[], []]


def get_machine_name(machine_url):
machines, images = Helper.get_machines_list()
for machine in machines:
if machine['value'] == machine_url:
return machine['text']

return None


def get_api_config():
credential_path = '/etc/ckan/default/credentials/smw1153.txt'
smw_base_url = "https://service.tib.eu/sfb1153/wiki/"
Expand Down
7 changes: 7 additions & 0 deletions ckanext/semantic_media_wiki/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ def get_blueprint(self):
methods=['GET']
)

blueprint.add_url_rule(
u'/smw/get_resource_machine/<id>',
u'get_resource_machine',
MediaWikiController.get_resource_machine,
methods=['GET']
)

blueprint.add_url_rule(
u'/smw/edit_save',
u'edit_save',
Expand Down
31 changes: 17 additions & 14 deletions ckanext/semantic_media_wiki/public/statics/dataset_machines.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
$(document).ready(function(){
let anchors = $('.machine-link');
for(let i=0; i<anchors.length; i++){
get_resource_link(anchors[i] ,$(anchors[i]).siblings('.base_url').val());
}
let dest_url = $('#dataset_id_for_machine').val();
get_resource_link($('#machinesTableRow') ,dest_url);

});

Expand All @@ -13,16 +11,21 @@ function get_resource_link(target, url){
dataType: 'json',
type: "GET",
success: function(result){
if(result == '0'){
$(target).find('.machine-name-tag').css('visibility', 'hidden');
}
else{
$(target).attr('href', result[0]);
$(target).find('.machine-name-tag').text(result[1]);
$(target).show();


}
if(result !== '0'){
let block = '';
for (let i=0; i < result.length; i++){
block += build(result[i][0], result[i][1]);
}
$(target).append(block);
$('.machine-link').fadeIn();
}
}
});
}

function build(url, name){
let machineName = '<div class="machine-name-tag">' + name + '</div>';
let anchor = '<a href="' + url + '" target="_blank" class="machine-link">' + machineName + '</a>';
return '<span>' + anchor + '</span>';

}
27 changes: 0 additions & 27 deletions ckanext/semantic_media_wiki/public/statics/edit_machine.css

This file was deleted.

Loading

0 comments on commit 9736334

Please sign in to comment.