def __init__(self): """ Init base variable short_url. """ self.short_url = short_url.UrlEncoder(alphabet=string.ascii_lowercase + string.ascii_uppercase + '0123456789', block_size=0)
from bottle import route, run, view, static_file, request, HTTPError, post, default_app import settings as _settings from utils import random_name, fetch_favicon from models import Custom con = redis.StrictRedis(_settings.REDIS.get('host', 'localhost'), _settings.REDIS.get('port', 6379), _settings.REDIS.get('db', 0)) socket.setdefaulttimeout(10) url_encoding = short_url.UrlEncoder('กขฃคฅฆงจฉชซฌญฎฏฐฑฒณดตถทธนบปผฝพฟภมยรฤลฦวศษสหฬอฮฯะเแโใไๅๆ๏๐๑๒๓๔๕๖๗๘๙๚๛') app = default_app() @route('/') @view('home') def index(): """ Home page """ settings = _settings.__dict__ settings.update({'short_url': ''}) return locals() @route('/b/:short_url') @view('home')
def test_too_short_alphabet(): with raises(AttributeError): short_url.UrlEncoder(alphabet='aa') with raises(AttributeError): short_url.UrlEncoder(alphabet='a')
def test_custom_alphabet(): encoder = short_url.UrlEncoder(alphabet='ab') url = encoder.encode_url(12) assert url == 'bbaaaaaaaaaaaaaaaaaaaa' key = encoder.decode_url('bbaaaaaaaaaaaaaaaaaaaa') assert key == 12
import short_url from django.contrib.auth import authenticate from django.contrib.auth import login as _login from django.core.exceptions import ObjectDoesNotExist from django.http import Http404, JsonResponse from django.http import HttpResponseRedirect from django.shortcuts import render from django.urls import reverse_lazy from django.views.decorators.http import require_GET from django.views.generic import CreateView from shortener_app.forms import CustomUserCreationForm from shortener_app.models import Shortening ENCODER = short_url.UrlEncoder( alphabet= '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~') def index(request): return render(request, 'shortener_app/index.html') def login(request): return render(request, 'shortener_app/login.html') class CustomCreateView(CreateView): template_name = 'shortener_app/signup.html' form_class = CustomUserCreationForm success_url = reverse_lazy(index)