Exemplo n.º 1
0
 def __call__(self, *args, **kwargs):
     try:
         return self.app(*args, **kwargs)
     finally:
         DBSession.remove()
Exemplo n.º 2
0
def api(request):
    session = DBSession()
    url = request.params.get('url', '')

    # Check that the url is good (this is dirty hax, should use a regex)
    parsed = urlparse.urlparse(url)
    if parsed.scheme not in ('http', 'https'):
        return Response('Invalid URL', status_int=503)
    elif parsed.netloc in ('www.kan.gd', 'kan.gd', 'kanged.net', 'www.kanged.net'):
        return Response('Invalid URL - This one would cause some issues ;)', status_int=503)

    try:
        obj = ShortURL(url)
        session.add(obj)
        session.commit()
    except:
        session.rollback()
        return Response('Oops!  We had an error!', status=503)

    # Encode the primary key.
    key = base36encode(obj.id)

    # Ensure the session is completely closed.
    session.commit()
    session.close()

    return Response('http://kan.gd/%s' % key)