From f4f4bbc06710498686eeab8d157f36c530066bf2 Mon Sep 17 00:00:00 2001 From: Amit Tripathi Date: Sat, 22 Jun 2019 18:12:19 +0200 Subject: [PATCH] Versioning using version file, change navbar color, other fixes (#39) --- README.md | 46 +++++++++++++++++---------------- pygmy/app/link.py | 2 +- pygmy/rest/shorturl.py | 14 +++++----- pygmy/validator/link.py | 4 +-- pygmyui/pygmy/views.py | 13 +++++----- pygmyui/restclient/error_msg.py | 2 +- pygmyui/static/css/styles.css | 5 +--- pygmyui/templates/base.html | 2 +- requirements.txt | 4 +-- version | 1 + 10 files changed, 46 insertions(+), 47 deletions(-) create mode 100644 version diff --git a/README.md b/README.md index 0ba774d..59b0cb9 100644 --- a/README.md +++ b/README.md @@ -13,33 +13,35 @@ Live version of this project @ [https://pygy.co](https://pygy.co) Check link stats by adding **+** to the URL. Example [pygy.co/pygmy+](https://pygy.co/pygmy+) +Hackernews Thread: [https://news.ycombinator.com/item?id=17690559](https://pygy.co/shorten/pygmyHN) + *Note that pygy.co is a demo website for this project and should be used as such. While the website is going to be up for the foreseeable future, its future depends on the sponsorship and hosting that I get. Currently, the project is hosted on Digitalocean, as they were kind enough to offer me one year of sponsorship. I would like to keep the project website up and maintain the project but I do not make any financial gains out of this project or website. Website is free to use and is completely ad-free. If you would like to support the project, Please use the donate link in [Donations](#donations) section.* # Table of Contents -- [Pygmy](#pygmy) -- [Table of Contents](#table-of-contents) - - [Features](#features) - - [Technical Info](#technical-info) - - [Installation/Setup](#installationsetup) - - [Docker](#docker) - - [Manual(from source)](#manualfrom-source) - - [DB Setup:](#db-setup) - - [Use MySQL](#use-mysql) - - [Use Postgresql](#use-postgresql) - - [Use SQLite](#use-sqlite) - - [Docker](#docker-1) - - [Using Pygmy API](#using-pygmy-api) - - [Create User:](#create-user) - - [Shell Usage](#shell-usage) - - [How Link Stats Are Generated?](#how-link-stats-are-generated) - - [How Pygmy Auth Token Works?](#how-pygmy-auth-token-works) - - [Development](#development) - - [Contributions](#contributions) - - [Donations](#donations) - - [Sponsorship](#sponsorship) - - [License](#license) +- [Pygmy](#Pygmy) +- [Table of Contents](#Table-of-Contents) + - [Features](#Features) + - [Technical Info](#Technical-Info) + - [Installation/Setup](#InstallationSetup) + - [Docker](#Docker) + - [Manual(from source)](#Manualfrom-source) + - [DB Setup:](#DB-Setup) + - [Use MySQL](#Use-MySQL) + - [Use Postgresql](#Use-Postgresql) + - [Use SQLite](#Use-SQLite) + - [Docker](#Docker-1) + - [Using Pygmy API](#Using-Pygmy-API) + - [Create User:](#Create-User) + - [Shell Usage](#Shell-Usage) + - [How Link Stats Are Generated?](#How-Link-Stats-Are-Generated) + - [How Pygmy Auth Token Works?](#How-Pygmy-Auth-Token-Works) + - [Development](#Development) + - [Contributions](#Contributions) + - [Donations](#Donations) + - [Sponsorship](#Sponsorship) + - [License](#License) Pygmy or `pygy.co` is an open-source, extensible & easy-to-use but powerful URL shortener. It's created keeping in mind that it should be easy to host and run your custom URL shortener without much effort. [Open-source Python URL shortener] diff --git a/pygmy/app/link.py b/pygmy/app/link.py index 7db6f7a..83b9c6a 100644 --- a/pygmy/app/link.py +++ b/pygmy/app/link.py @@ -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. diff --git a/pygmy/rest/shorturl.py b/pygmy/rest/shorturl.py index bab9f23..0d8c4f1 100644 --- a/pygmy/rest/shorturl.py +++ b/pygmy/rest/shorturl.py @@ -34,6 +34,13 @@ 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: @@ -41,11 +48,6 @@ def post(self): 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) @@ -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 diff --git a/pygmy/validator/link.py b/pygmy/validator/link.py index 2811f83..e239d98 100644 --- a/pygmy/validator/link.py +++ b/pygmy/validator/link.py @@ -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 @@ -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) diff --git a/pygmyui/pygmy/views.py b/pygmyui/pygmy/views.py index bec7553..288712f 100644 --- a/pygmyui/pygmy/views.py +++ b/pygmyui/pygmy/views.py @@ -1,3 +1,4 @@ +import json import string import operator @@ -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. @@ -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(), diff --git a/pygmyui/restclient/error_msg.py b/pygmyui/restclient/error_msg.py index 795c2c3..0df4ee7 100644 --- a/pygmyui/restclient/error_msg.py +++ b/pygmyui/restclient/error_msg.py @@ -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 diff --git a/pygmyui/static/css/styles.css b/pygmyui/static/css/styles.css index 0ca866d..cea12a0 100644 --- a/pygmyui/static/css/styles.css +++ b/pygmyui/static/css/styles.css @@ -17,9 +17,6 @@ body { margin-right: .2px; } -a { -} - ul.dropdown-lr { width: 300px; } @@ -112,7 +109,7 @@ margin-top: 3%; .navbar .nav > li > a:focus, .navbar .nav > li > a:active, .navbar .nav > li > a:visited { - background-color: #44EEBB; + background-color: #599B36; } .item-detail-title { diff --git a/pygmyui/templates/base.html b/pygmyui/templates/base.html index 42867be..5bc4b06 100644 --- a/pygmyui/templates/base.html +++ b/pygmyui/templates/base.html @@ -25,7 +25,7 @@ -