Minor fixes

This commit is contained in:
Elia El Lazkani 2019-10-05 20:04:19 +02:00
parent 03a38d7160
commit e37aa4bb13
2 changed files with 20 additions and 8 deletions

View file

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

View file

@ -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:
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:
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:
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:
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):