Browse Source

Better consistency in exceptions and translator class.

Stefano Cossu 6 years ago
parent
commit
f376d12ed8
3 changed files with 21 additions and 14 deletions
  1. 6 6
      lakesuperior/exceptions.py
  2. 1 1
      lakesuperior/model/ldp_rs.py
  3. 14 7
      lakesuperior/util/translator.py

+ 6 - 6
lakesuperior/exceptions.py

@@ -20,7 +20,7 @@ class ResourceExistsError(ResourceError):
     This usually surfaces at the HTTP level as a 409.
     '''
     def __str__(self):
-        return self.msg or 'Resource {} already exists.'.format(self.uuid)
+        return self.msg or 'Resource /{} already exists.'.format(self.uuid)
 
 
 
@@ -32,7 +32,7 @@ class ResourceNotExistsError(ResourceError):
     This usually surfaces at the HTTP level as a 404.
     '''
     def __str__(self):
-        return self.msg or 'Resource {} not found.'.format(self.uuid)
+        return self.msg or 'Resource /{} not found.'.format(self.uuid)
 
 
 
@@ -43,7 +43,7 @@ class InvalidResourceError(ResourceError):
     This usually surfaces at the HTTP level as a 409 or other error.
     '''
     def __str__(self):
-        return self.msg or 'Resource {} is invalid.'.format(self.uuid)
+        return self.msg or 'Resource /{} is invalid.'.format(self.uuid)
 
 
 
@@ -93,12 +93,12 @@ class SingleSubjectError(RuntimeError):
     Raised when a SPARQL-Update query or a RDF payload for a PUT contain
     subjects that do not correspond to the resource being operated on.
     '''
-    def __init__(self, uri, subject):
-        self.uri = uri
+    def __init__(self, uuid, subject):
+        self.uuid = uuid
         self.subject = subject
 
     def __str__(self):
         return '{} is not in the topic of this RDF, which is {}'.format(
-                self.uri, self.subject)
+                self.uuid, self.subject)
 
 

+ 1 - 1
lakesuperior/model/ldp_rs.py

@@ -230,7 +230,7 @@ class LdpRs(Ldpr):
         '''
         for s in set(g.subjects()):
             if not s == self.uri:
-                return SingleSubjectError(s, self.uri)
+                return SingleSubjectError(s, self.uuid)
 
 
     def _check_ref_int(self, config):

+ 14 - 7
lakesuperior/util/translator.py

@@ -38,7 +38,19 @@ class Translator:
 
         @return URIRef
         '''
-        return URIRef('{}rest/{}'.format(request.host_url, uuid))
+        return URIRef('{}/{}'.format(Translator.base_url(), uuid))
+
+
+    @staticmethod
+    def uri_to_uuid(uri):
+        '''Convert an absolute URI (internal or external) to a UUID.
+
+        @return string
+        '''
+        if uri.startswith(nsc['fcres']):
+            return str(uri).replace(nsc['fcres'], '')
+        else:
+            return str(uri).replace(Translator.base_url(), '')
 
 
     @staticmethod
@@ -49,7 +61,6 @@ class Translator:
 
         @return string
         '''
-        #import pdb; pdb.set_trace()
         return s.replace(Translator.base_url()+'/', str(nsc['fcres']))\
                 .replace(Translator.base_url(), str(nsc['fcres']))
 
@@ -63,7 +74,6 @@ class Translator:
 
         @return rdflib.term.URIRef
         '''
-        #import pdb; pdb.set_trace()
         Translator._logger.debug('Input URI: {}'.format(uri))
         if uri.strip('/') == Translator.base_url():
             return BaseRdfLayout.ROOT_NODE_URN
@@ -78,10 +88,7 @@ class Translator:
 
         @return string
         '''
-        return s.replace(
-            str(nsc['fcres']),
-            request.host_url + 'rest/'
-        )
+        return s.replace(str(nsc['fcres']), Translator.base_url() + '/')
 
 
     @staticmethod