Permalink
Browse files

allow lowercase urls

  • Loading branch information...
1 parent b5669f9 commit c76d77e8f639ea1eb9c61e41f1e11a4c12f6780e @dean-m dean-m committed Jul 18, 2013
Showing with 14 additions and 8 deletions.
  1. +14 −8 main.py
View
22 main.py
@@ -25,7 +25,7 @@
if os.environ['SERVER_SOFTWARE'].startswith('Dev'):
CACHE_ENABLED = False
CDN_ENABLED = False
-
+
def _request(url, cache_ttl=3600, force=False):
request_cache_key = 'request:%s' % url
failure_cache_key = 'failure:%s' % url
@@ -38,7 +38,7 @@ def _request(url, cache_ttl=3600, force=False):
if "html" in resp:
resp["html"] = re.sub("/w/page/\d*", "", resp["html"])
else:
- resp = json.loads(data.content)
+ resp = json.loads(data.content)
memcache.set(request_cache_key, resp, cache_ttl)
memcache.set(failure_cache_key, resp, cache_ttl*10)
except (ValueError, urlfetch.DownloadError), e:
@@ -57,7 +57,7 @@ def post(self):
memcache.delete(request_cache_key)
memcache.delete(failure_cache_key)
self.response.out.write("200 OK")
-
+
class IndexHandler(webapp.RequestHandler):
def get(self):
utc_now = pytz.utc.localize(datetime.utcnow())
@@ -82,7 +82,7 @@ class MainHandler(webapp.RequestHandler):
def get(self, pagename, site = PB_WIKI):
skip_cache = self.request.get('cache') == '0'
version = os.environ['CURRENT_VERSION_ID']
-
+
redirect_urls = {
# From: To
'give': 'Give',
@@ -97,25 +97,31 @@ def get(self, pagename, site = PB_WIKI):
if pagename in redirect_urls:
url = redirect_urls[pagename]
self.redirect(url, permanent=True)
- else:
+ else:
if CDN_ENABLED:
- cdn = CDN_HOSTNAME
+ cdn = CDN_HOSTNAME
try:
if not(pagename):
pagename = 'FrontPage'
page = _request(PB_API_URL % (site, pagename), cache_ttl=604800, force=skip_cache)
+ # fetch a page where a lowercase version may exist
+ if not(page):
+ pagename = memcache.get(pagename.lower())
+ page = _request(PB_API_URL % (site, pagename), cache_ttl=604800, force=skip_cache)
# Convert quasi-camel-case to spaced words
title = re.sub('([a-z]|[A-Z])([A-Z])', r'\1 \2', pagename)
if page and "name" in page:
+ fiveDays = 432000
+ memcache.set(pagename.lower(), pagename, fiveDays)
self.response.out.write(template.render('templates/content.html', locals()))
else:
raise LookupError
except LookupError:
self.response.out.write(template.render('templates/404.html', locals()))
self.response.set_status(404)
-
-
+
+
app = webapp.WSGIApplication([
('/api/pbwebhook', PBWebHookHandler),
('/api/event_staff', StaffHandler),

0 comments on commit c76d77e

Please sign in to comment.