Commits

Benoit Boissinot committed 71e10d1

use a query, wsgi on google side is buggy

Comments (0)

Files changed (3)

 api_version: 1
 
 handlers:
-- url: /urn:lex:fr:.*
-  script: main.py
-
 - url: /(index.html)?
   static_files: static/index.html
   upload: static/index.html
+
+- url: /redirect/?
+  script: main.py
+
 from google.appengine.ext import db, webapp
 from google.appengine.ext.webapp.util import run_wsgi_app
 from django.utils import simplejson as json
-import urllib
+import re, urllib
 
 def quoteurnre(urlre):
-    return urllib.quote(urlre, safe='/()[]?+.*{}\\')
+    return urllib.quote(urlre, safe='/^$()[]?+.*{}\\')
 
-class Senat(webapp.RequestHandler):
+def resolvesenat(query):
     typemapping = {
             'proposition.loi': 'ppl',
             'projet.loi': 'pjl',
             'proposition.resolution': 'ppr'
     }
-    def get(self, type, year, id):
-        year = int(year, 10) % 100
-        id = int(id, 10)
-        try:
-            type = self.typemapping[type]
-        except KeyError:
-            self.error(404)
-        self.redirect("http://www.senat.fr/leg/%s%02d-%03d.html" % (type, year, id))
+    m = re.search(r'urn:lex:fr:senat:(.*?):(\d{4});(\d+)$', query)
+    if not m:
+        return None
+    year = int(m.group(2), 10) % 100
+    id = int(m.group(3), 10)
+    try:
+        type = typemapping[m.group(1)]
+    except KeyError:
+        return None
+    return "http://www.senat.fr/leg/%s%02d-%03d.html" % (type, year, id)
 
-class AssembleeNationale(webapp.RequestHandler):
+def resolvean(query):
     typemapping = {
             'proposition.loi': 'propositions/pion',
             'projet.loi': 'projets/pl',
     }
-    def get(self, type, legi, id):
-        legi = int(legi, 10)
-        id = int(id, 10)
+    m = re.search(r'urn:lex:fr:assemblee.nationale:(.*?):(\d+)\.legislature;(\d+)$', query)
+    if not m:
+        return None
+    legi = int(m.group(2), 10)
+    id = int(m.group(3), 10)
+    try:
+        type = typemapping[m.group(1)]
+    except KeyError:
+        return None
+    return "http://www.assemblee-nationale.fr/%02d/%s%04d.asp" % (legi, type, id)
+
+resolve = {
+        'senat': resolvesenat,
+        'assemblee.nationale': resolvean,
+}
+
+class Resolver(webapp.RequestHandler):
+    def get(self):
+        query = self.request.query_string
+        m = re.search(r'urn:lex:fr:(.*?):', query)
+        if not m:
+            self.response.out.write('got path: %s' % self.request.path_qs)
+            self.error(404)
+            return
+
         try:
-            type = self.typemapping[type]
+            url = resolve[m.group(1)](query)
         except KeyError:
             self.error(404)
-        self.redirect("http://www.assemblee-nationale.fr/%02d/%s%04d.asp" % (legi, type, id))
+            return
+        if url is None:
+            self.error(404)
+            return
+        self.redirect(url)
+
+class Fallback(webapp.RequestHandler):
+    def get(self):
+        self.response.out.write('%s\n' % self.request.path_qs)
 
 application = webapp.WSGIApplication(
-    [(quoteurnre(r'/urn:lex:fr:senat:(.*?):(\d{4});(\d+)'), Senat),
-     (quoteurnre(r'/urn:lex:fr:assemblee.nationale:(.*?):(\d+).legislature;(\d+)'), AssembleeNationale),
+    [(r'/redirect/?', Resolver),
+     #(r'/.*', Fallback),
     ], debug=True)
 
 def main():

static/index.html

 <html>
 	<body>
 		<p>A very simple <a href="http://tools.ietf.org/html/draft-spinosa-urn-lex-01">urn:lex</a> resolver for France. It currently supports only some links to senat.fr and assemblee-nationale.fr.</p>
-		<p>Some examples: <a href="/urn:lex:fr:assemblee.nationale:projet.loi:13.legislature;2339">urn:lex:fr:assemblee.nationale:projet.loi:13.legislature;2339</a> or <a href="/urn:lex:fr:senat:proposition.loi:2008;16">urn:lex:fr:senat:proposition.loi:2008;16</a>.</p>
+		<p>Some examples: <a href="/redirect?urn:lex:fr:assemblee.nationale:projet.loi:13.legislature;2339">urn:lex:fr:assemblee.nationale:projet.loi:13.legislature;2339</a> or <a href="/redirect?urn:lex:fr:senat:proposition.loi:2008;16">urn:lex:fr:senat:proposition.loi:2008;16</a>.</p>
 		<p>Comments, suggestions: <a href="mailto:bboissin@gmail.com">bboissin@gmail.com</a>, code on <a href="http://bitbucket.org/bboissin/urnlex/">bitbucket</a>.</p>
 	</body>
 </html>