Browse Source

Minor fixes

master
Elia El Lazkani 3 years ago
parent
commit
5b37f5759c
  1. 1
      requirements/development.txt
  2. 27
      shortenit/web.py

1
requirements/development.txt

@ -1 +1,2 @@ @@ -1 +1,2 @@
-r requirements.txt
setuptools-git

27
shortenit/web.py

@ -3,7 +3,7 @@ import logging @@ -3,7 +3,7 @@ import logging
import aiohttp_jinja2
import jinja2
import trafaret as t
import trafaret
from aiohttp import web
from pathlib import Path
@ -40,6 +40,8 @@ class Web: @@ -40,6 +40,8 @@ class Web:
name='index')
self.router.add_post('/shortenit', self.handler.shortenit,
name='shortenit')
self.router.add_put('/pointers/favicon.ico', self.handler.favicon,
name='favicon')
self.router.add_get('/{identifier}', self.handler.redirect,
name='redirect')
@ -50,9 +52,9 @@ class SiteHandler: @@ -50,9 +52,9 @@ class SiteHandler:
self.database = database
self.shorten_url = shorten_url
self.lenghten_url = lenghten_url
self.shortenit_load_format = t.Dict({
t.Key('url'): t.URL,
t.Key('timestamp'): t.Int
self.shortenit_load_format = trafaret.Dict({
trafaret.Key('url'): trafaret.URL,
trafaret.Key('timestamp'): trafaret.Int
})
async def shortenit(self, request):
@ -60,9 +62,15 @@ class SiteHandler: @@ -60,9 +62,15 @@ class SiteHandler:
self.logger.debug(data)
try:
data = self.shortenit_load_format(data)
except t.DataError:
raise web.HTTPBadRequest('URL is not valid')
short_url = self.shorten_url(self.database, data['url'], data['timestamp'])
except Exception as e:
self.logger.debug(e)
raise web.HTTPBadRequest()
try:
short_url = self.shorten_url(
self.database, data['url'], data['timestamp'])
except KeyError as e:
self.logger.debug(e)
raise web.HTTPBadRequest()
self.logger.debug(short_url)
return web.json_response({"url": short_url})
@ -71,7 +79,10 @@ class SiteHandler: @@ -71,7 +79,10 @@ class SiteHandler:
url = self.lenghten_url(self.database, identifier)
if not url:
raise web.HTTPNotFound()
return web.HTTPFound(location=url)
raise web.HTTPFound(location=url)
async def favicon(self, request):
raise web.Response(text={})
@aiohttp_jinja2.template('index.html')
async def index(self, request):

Loading…
Cancel
Save