Skip to content
Browse files

fix bugs with primitive webui, and wiring into webpy and pylons inter…

…faces

git-svn-id: http://sword-app.svn.sourceforge.net/svnroot/sword-app/sss/branches/sss-2@449 2bf6ea0f-123d-0410-b71a-f1a21eb24612
  • Loading branch information...
1 parent c41f17d commit 08aef1a751cea4c955e701fcfb15038028abb03c richard-jones committed Jan 14, 2012
Showing with 121 additions and 40 deletions.
  1. +3 −0 PYLONS.txt
  2. +103 −25 sss/pylons_sword_controller.py
  3. +2 −2 sss/webpy.py
  4. +13 −13 sss/webui.py
View
3 PYLONS.txt
@@ -19,6 +19,9 @@ become impossible
routing.py
+In order to make the routes work properly, you need to remove the public/index.html
+to make the "/" route work properly
+
"""Routes configuration
The more specific and detailed routes should be defined first so they
View
128 sss/pylons_sword_controller.py
@@ -3,10 +3,11 @@
from pylons.controllers import WSGIController
from pylons.templating import render_mako as render
-import re, base64, urllib, uuid
+import re, base64, urllib, uuid, inspect
from core import Auth, SWORDSpec, SwordError, AuthException, DepositRequest, DeleteRequest
from negotiator import ContentNegotiator, AcceptParameters, ContentType
from spec import Errors, HttpHeaders, ValidationException
+from webui import HomePage, CollectionPage, ItemPage
import logging
ssslog = logging.getLogger(__name__)
@@ -263,7 +264,7 @@ def service_document(self, sub_path=None):
if http_method == "GET":
return self._GET_service_document(sub_path)
else:
- ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + __name__)
+ ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + inspect.stack()[0][3])
abort(405, "Method Not Allowed")
return
@@ -274,7 +275,7 @@ def collection(self, path=None):
elif http_method == "POST":
return self._POST_collection(path)
else:
- ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + __name__)
+ ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + inspect.stack()[0][3])
abort(405, "Method Not Allowed")
return
@@ -289,7 +290,7 @@ def media_resource(self, path=None):
elif http_method == "DELETE":
return self._DELETE_media_resource(path)
else:
- ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + __name__)
+ ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + inspect.stack()[0][3])
abort(405, "Method Not Allowed")
return
@@ -304,7 +305,7 @@ def container(self, path=None):
elif http_method == "DELETE":
return self._DELETE_container(path)
else:
- ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + __name__)
+ ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + inspect.stack()[0][3])
abort(405, "Method Not Allowed")
return
@@ -313,13 +314,39 @@ def statement(self, path=None):
if http_method == "GET":
return self._GET_statement(path)
else:
- ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + __name__)
+ ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + inspect.stack()[0][3])
abort(405, "Method Not Allowed")
return
- def aggregation(self, path=None): pass
- def part(self, path=None): pass
- def webui(self, path=None): pass
+ def aggregation(self, path=None):
+ http_method = request.environ['REQUEST_METHOD']
+ if http_method == "GET":
+ return self._GET_aggregation(path)
+ else:
+ ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + inspect.stack()[0][3])
+ abort(405, "Method Not Allowed")
+ return
+
+ def webui(self, path=None):
+ http_method = request.environ['REQUEST_METHOD']
+ if http_method == "GET":
+ return self._GET_webui(path)
+ else:
+ ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + inspect.stack()[0][3])
+ abort(405, "Method Not Allowed")
+ return
+
+
+ def part(self, path=None):
+ http_method = request.environ['REQUEST_METHOD']
+ if http_method == "GET":
+ return self._GET_part(path)
+ elif http_method == "PUT":
+ return self._PUT_part(path)
+ else:
+ ssslog.info("Returning (405) Method Not Allowed; Received " + http_method + " request on " + inspect.stack()[0][3])
+ abort(405, "Method Not Allowed")
+ return
# SWORD Protocol Operations
###########################
@@ -341,7 +368,7 @@ def _GET_service_document(self, path=None):
ss = SwordServer(config, auth)
sd = ss.service_document(path)
response.content_type = "text/xml"
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return sd
def _GET_collection(self, path=None):
@@ -363,7 +390,7 @@ def _GET_collection(self, path=None):
ss = SwordServer(config, auth)
cl = ss.list_collection(path)
response.content_type = "text/xml"
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return cl
def _POST_collection(self, path=None):
@@ -398,11 +425,11 @@ def _POST_collection(self, path=None):
response.status = "201 Created"
if config.return_deposit_receipt:
ssslog.info("Returning deposit receipt")
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return result.receipt
else:
ssslog.info("Omitting deposit receipt")
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return
except SwordError as e:
@@ -456,7 +483,7 @@ def _GET_media_resource(self, path=None):
f = open(media_resource.filepath, "r")
response.status_int = 200
response.status = "200 OK"
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return f.read()
def _PUT_media_resource(self, path=None):
@@ -493,7 +520,7 @@ def _PUT_media_resource(self, path=None):
ssslog.info("Content replaced")
response.status_int = 204
response.status = "204 No Content" # notice that this is different from the POST as per AtomPub
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return
except SwordError as e:
@@ -532,11 +559,11 @@ def _POST_media_resource(self, path=None):
response.status = "201 Created"
if config.return_deposit_receipt:
ssslog.info("Returning Receipt")
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return result.receipt
else:
ssslog.info("Omitting Receipt")
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return
except SwordError as e:
@@ -574,7 +601,7 @@ def _DELETE_media_resource(self, path=None):
# just return, no need to give any more feedback
response.status_int = 204
response.status = "204 No Content" # No Content
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return
except SwordError as e:
@@ -619,7 +646,7 @@ def _GET_container(self, path=None):
# now actually get hold of the representation of the container and send it to the client
cont = ss.get_container(path, accept_parameters)
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return cont
except SwordError as e:
@@ -656,13 +683,13 @@ def _PUT_container(self, path=None):
response.status_int = 200
response.status = "200 OK"
ssslog.info("Returning Deposit Receipt")
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return result.receipt
else:
response.status_int = 204
response.status = "204 No Content"
ssslog.info("Omitting Deposit Receipt")
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return
except SwordError as e:
@@ -706,11 +733,11 @@ def _POST_container(self, path=None):
if config.return_deposit_receipt:
response.content_type = "application/atom+xml;type=entry"
ssslog.info("Returning Deposit Receipt")
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return result.receipt
else:
ssslog.info("Omitting Deposit Receipt")
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return
except SwordError as e:
@@ -746,7 +773,7 @@ def _DELETE_container(self, path=None):
# no need to return any content
response.status_int = 204
response.status = "204 No Content"
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return
except SwordError as e:
@@ -769,8 +796,59 @@ def _GET_statement(self, path=None):
# now actually get hold of the representation of the statement and send it to the client
cont = ss.get_statement(path)
- ssslog.info("Returning " + response.status + " from request on " + __name__)
+ ssslog.info("Returning " + response.status + " from request on " + inspect.stack()[0][3])
return cont
except SwordError as e:
return self.manage_error(e)
+
+
+ # OTHER HTTP HANDLERS
+ #############################################################################
+ # Define a set of handlers for the various URLs defined above to be used by web.py
+ # These ones aren't anything to do with the SWORD standard, they are just
+ # convenient to support the additional URIs produced
+
+ def _GET_aggregation(self, path=None):
+ # in this case we just redirect back to the Edit-URI with a 303 See Other
+ ss = SwordServer(config, None)
+ edit_uri = ss.get_edit_uri()
+ response.status_int = 303
+ response.status = "303 See Other"
+ response.headers["Content-Location"] = edit_uri
+ return
+
+ def _GET_webui(self, path=None):
+ if path is not None:
+ if path.find("/") >= 0:
+ ip = ItemPage(config)
+ return ip.get_item_page(path)
+ else:
+ cp = CollectionPage(config)
+ return cp.get_collection_page(path)
+ else:
+ hp = HomePage(config)
+ return hp.get_home_page()
+
+ def _GET_part(self, path):
+ ss = SwordServer(config, None)
+
+ # if we did, we can get hold of the media resource
+ fh = ss.get_part(path)
+
+ if fh is None:
+ return self.manage_error(SwordError(status=404, empty=True))
+
+ response.content_type = "application/octet-stream" # FIXME: we're not keeping track of content types
+ response.status_int = 200
+ response.status = "200 OK"
+ return fh.read()
+
+ def _PUT_part(self, path):
+ # FIXME: the spec says that we should either support this or return
+ # 405 Method Not Allowed.
+ # This would be useful for DepositMO compliance, so we should consider
+ # implementing this when time permits
+ response.status_int = 405
+ response.status = "405 Method Not Allowed"
+ return
View
4 sss/webpy.py
@@ -736,10 +736,10 @@ class WebUI(SwordHttpHandler):
def GET(self, path=None):
if path is not None:
if path.find("/") >= 0:
- ip = ItemPage()
+ ip = ItemPage(config)
return ip.get_item_page(path)
else:
- cp = CollectionPage()
+ cp = CollectionPage(config)
return cp.get_collection_page(path)
else:
hp = HomePage(config)
View
26 sss/webui.py
@@ -18,30 +18,29 @@ class HomePage(WebPage):
def __init__(self, config):
self.config = config
self.dao = DAO(self.config)
- self.um = URIManager()
+ self.um = URIManager(config)
def get_home_page(self):
- cfg = self.config
-
frag = "<h1>Simple SWORDv2 Server</h1>"
- frag += "<p><strong>Service Document (SD-IRI)</strong>: <a href=\"" + cfg.base_url + "sd-uri\">" + cfg.base_url + "sd-uri</a></p>"
- frag += "<p>If prompted, use the username <strong>" + cfg.user + "</strong> and the password <strong>" + cfg.password + "</strong></p>"
- frag += "<p>The On-Behalf-Of user to use is <strong>" + cfg.obo + "</strong></p>"
+ frag += "<p><strong>Service Document (SD-IRI)</strong>: <a href=\"" + self.config.base_url + "sd-uri\">" + self.config.base_url + "sd-uri</a></p>"
+ frag += "<p>If prompted, use the username <strong>" + self.config.user + "</strong> and the password <strong>" + self.config.password + "</strong></p>"
+ frag += "<p>The On-Behalf-Of user to use is <strong>" + self.config.obo + "</strong></p>"
# list the collections
frag += "<h2>Collections</h2><ul>"
for col in self.dao.get_collection_names():
frag += "<li><a href=\"" + self.um.html_url(col) + "\">" + col + "</a></li>"
frag += "</ul>"
- head_frag = "<link rel=\"http://purl.org/net/sword/discovery/service-document\" href=\"" + cfg.base_url + "sd-uri\"/>"
+ head_frag = "<link rel=\"http://purl.org/net/sword/discovery/service-document\" href=\"" + self.config.base_url + "sd-uri\"/>"
return self._wrap_html("Simple SWORDv2 Server", frag, head_frag)
class CollectionPage(WebPage):
- def __init__(self):
- self.dao = DAO()
- self.um = URIManager()
+ def __init__(self, config):
+ self.config = config
+ self.dao = DAO(config)
+ self.um = URIManager(config)
def get_collection_page(self, id):
frag = "<h1>Collection: " + id + "</h1>"
@@ -59,9 +58,10 @@ def get_collection_page(self, id):
return self._wrap_html("Collection: " + id, frag, head_frag)
class ItemPage(WebPage):
- def __init__(self):
- self.dao = DAO()
- self.um = URIManager()
+ def __init__(self, config):
+ self.config = config
+ self.dao = DAO(config)
+ self.um = URIManager(config)
def get_item_page(self, oid):
collection, id = self.um.interpret_oid(oid)

0 comments on commit 08aef1a

Please sign in to comment.
Something went wrong with that request. Please try again.