Skip to content

Commit

Permalink
Added delete and template uses right variables
Browse files Browse the repository at this point in the history
  • Loading branch information
pietercolpaert committed Aug 14, 2013
1 parent 45ae040 commit 9c48d1e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
39 changes: 26 additions & 13 deletions ckanext/tdt/plugin.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import requests
import ckan.plugins as p
import json

from ckan.logic import get_action
from ckan import model
from logging import getLogger
from pylons import config

log = getLogger(__name__)

Expand Down Expand Up @@ -32,44 +35,54 @@ def update_config(self, config):
def notify(self, entity, operation=None):
# Make sure we're working with a Resource
if isinstance(entity, model.Resource):
## TODO: check whether we aren't deleting
self.create_tdt_source(entity)
if operation:
if operation == model.domain_object.DomainObjectOperation.deleted:
self.delete_tdt_source(entity)
else:
self.create_tdt_source(entity)
return

def can_preview(self, data_dict):
log.info(data_dict)
return True
""" This function will test whether the resource exists at TDT's side and if it returns a nice HTTP 200 OK
"""
rid = data_dict["resource"]["id"]
rname = data_dict["resource"]["name"]
if(rname == ""):
rname = "unnamed"
tdt_uri = self.tdt_host + "/ckan/" + rid + "/" + rname + ".about"
r = requests.head(tdt_uri)
log.info(r.status_code)
return r.status_code == 200

def preview_template(self, context, data_dict):
return 'dataviewer/tdt.html'

def setup_template_variables(self, context, data_dict):
p.toolkit.c.tdt_host = self.tdt_host
p.toolkit.c.id = data_dict["resource"]["id"]
p.toolkit.c.name = data_dict["resource"]["name"]


def create_tdt_source(self, entity):
"""This method should add a resource configuration to The DataTank and return the uri
"""
log.debug(entity)
# This should change towards a configurable array of supported formats
if(hasattr(entity, 'format') and ( entity.format.lower() == "xml" or entity.format.lower() == "json")):
log.info("We have an XML or a JSON file! Let's do a request!")
log.info("Adding to The DataTank since we have an XML or a JSON")
# !! entity.name is not necessarily set in CKAN
if(entity.name == ""):
entity.name = "unnamed"

tdt_uri = self.tdt_host + "/tdtadmin/resources/ckan/" + entity.id + "/" + entity.name
r = requests.put(tdt_uri,
auth=(self.tdt_user, self.tdt_pass),
data="resource_type=generic&generic_type=" + entity.format.upper() + "&documentation=" + entity.description +"&uri=" + entity.url)


if(r.status_code == 200):
log.info(r.headers["content-location"])
entity.extras["tdt-url"] = r.headers["content-location"]
#TODO: store the entity in the back-end of CKAN

#get_action('resource_update')(context, entity)

elif (r.status_code > 200):
log.error("Could not add dataset \""+ entity.name +"\" to The DataTank")
log.error(r.status_code)

return

def delete_tdt_source(self,entity):
Expand Down
10 changes: 5 additions & 5 deletions ckanext/tdt/templates/dataviewer/tdt.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
{% block page %}

<div height="20%" width="100%">
<a href="http://tdt-001.iminds.openminds.be/ckan/d2b73dda-564d-428f-a049-116758bea409/parko.html" target="datapreview">HTML</a> |
<a href="http://tdt-001.iminds.openminds.be/ckan/d2b73dda-564d-428f-a049-116758bea409/parko.json" target="datapreview">JSON</a> |
<a href="http://tdt-001.iminds.openminds.be/ckan/d2b73dda-564d-428f-a049-116758bea409/parko.xml" target="datapreview">XML</a>
Data URI: http://tdt-001.iminds.openminds.be/ckan/d2b73dda-564d-428f-a049-116758bea409/parko.xml
<a href="{{ c.tdt_host }}ckan/{{ c.id }}/{{ c.name }}.html" target="datapreview">HTML</a> |
<a href="{{ c.tdt_host }}ckan/{{ c.id }}/{{ c.name }}.json" target="datapreview">JSON</a> |
<a href="{{ c.tdt_host }}ckan/{{ c.id }}/{{ c.name }}.xml" target="datapreview">XML</a>
Data URI: <a href="{{ c.tdt_host }}ckan/{{ c.id }}/{{ c.name }}" target="_blank">{{ c.tdt_host }}ckan/{{ c.id }}/{{ c.name }}</a>
</div>
<iframe name="datapreview" src="http://tdt-001.iminds.openminds.be/ckan/d2b73dda-564d-428f-a049-116758bea409/parko.map" frameborder="0" height="500" width="100%">
<iframe name="datapreview" src="{{ c.tdt_host }}ckan/{{ c.id }}/{{ c.name }}.map" frameborder="0" height="500" width="100%">
</iframe>

{% endblock %}
Expand Down

0 comments on commit 9c48d1e

Please sign in to comment.