Skip to content

Commit

Permalink
WIP: refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
amitt001 committed Jun 17, 2019
1 parent 09d2bf7 commit 49e263c
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pygmy/app/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

def shorten(long_url, short_code=None, expire_after=None, description=None,
secret_key=None, owner=None, request=None):
"""Helper class that has been delicated the task of inserting the
"""Helper class that has been deligated the task of inserting the
passed url in DB, base 62 encoding from db id and return the short
url value.
Expand Down
14 changes: 7 additions & 7 deletions pygmy/rest/shorturl.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ def get(self):
def post(self):
payload = request.get_json()
data, errors = self.schema.load(payload)

if errors:
log.error('Error in the request payload %s', errors)
if errors.get('long_url'):
errors.update({'error': errors.get('long_url')})
return jsonify(errors), 400

# if authenticated request check valid user
user_email = APITokenAuth.get_jwt_identity()
if user_email:
user = UserManager().find(email=user_email)
if not user:
return jsonify(dict(error='Invalid user')), 400
data['owner'] = user.id
if errors:
log.error('Error in the request payload %s', errors)
if errors.get('long_url'):
errors.update({'error': errors.get('long_url')})
return jsonify(errors), 400

long_url = data.pop('long_url')
log.info('Shortening url %s', long_url)
Expand Down Expand Up @@ -90,8 +92,6 @@ def get(self):
@APITokenAuth.token_optional
def resolve(code):
"""Resolve the short url. code=301 PERMANENT REDIRECTION"""
# TODO not needed
user_email = APITokenAuth.get_jwt_identity()
secret_key = request.headers.get('secret_key')
try:
# check if link is not a secret link
Expand Down
4 changes: 1 addition & 3 deletions pygmy/validator/link.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import string

from datetime import datetime
from marshmallow import (
Schema, fields, post_dump, validate, ValidationError)
from marshmallow import Schema, fields, post_dump, ValidationError

from pygmy.model.clickmeta import ClickMetaManager
from pygmy.utilities.urls import make_short_url, validate_url
Expand Down Expand Up @@ -50,7 +49,6 @@ class LinkSchema(Schema):
created_at = fields.DateTime(format='%Y-%m-%d %H:%M:%S', dump_only=True)
updated_at = fields.DateTime()


def short_url_path(self, link):
if link and link.short_code:
return make_short_url(link.short_code)
Expand Down
13 changes: 7 additions & 6 deletions pygmyui/pygmy/views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import string
import operator

Expand All @@ -8,7 +9,7 @@
from utils import pygmy_client_object
from restclient.errors import ObjectNotFound, UnAuthorized, LinkExpired, \
InvalidInput
from restclient.error_msg import *
from restclient.error_msg import API_ERROR, INVALID_TOKEN
from iso2full import iso2full

# TODO: [IMP] middleware to return 500 page when internal error occurs.
Expand Down Expand Up @@ -112,15 +113,15 @@ def short_link_stats(request, code):
pygmy_client = pygmy_client_object(settings, request)
if request.method == 'GET':
try:
clickmeta = pygmy_client.link_stats(code)
clickmeta = pygmy_client.link_stats(code)
clickmeta['country_stats'] = sorted(
clickmeta['country_stats'].items(),
key=operator.itemgetter(1),
reverse=True)
stats =[(country,iso2full.get(country,"unknown"),hits) for (country,
hits) in clickmeta['country_stats']]
clickmeta['country_stats'] = stats

country_stats = [(country, iso2full.get(country, "unknown"), hits)
for (country, hits) in clickmeta['country_stats']]
clickmeta['country_stats'] = country_stats

clickmeta['referrer'] = sorted(
clickmeta['referrer'].items(),
Expand Down
2 changes: 1 addition & 1 deletion pygmyui/restclient/error_msg.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def API_ERROR(error_message):
pass
elif isinstance(error_message, dict):
error_message = error_message.get('error')
except Exception as e:
except Exception:
import traceback
traceback.print_exc()
error_message = INTERNAL_SERVER_ERROR_API
Expand Down
3 changes: 0 additions & 3 deletions pygmyui/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ body {
margin-right: .2px;
}

a {
}

ul.dropdown-lr {
width: 300px;
}
Expand Down

0 comments on commit 49e263c

Please sign in to comment.