<commit_before>from pyramid.view import view_config, view_defaults
from pyramid.httpexceptions import HTTPNotFound, HTTPFound

from urllib.parse import urlparse
from string import ascii_letters, digits
import random


@view_config(route_name='home', renderer='templates/index.mako')
def my_view(request):
    return {'project': 'Krympa'}

@view_config(route_name='redirect')
def redirect(request):
    shortened = request.matchdict['shortened']
    url = request.redis.get('short-url:%s' % shortened)
    if url:
        return HTTPFound(location=url.decode('utf-8'))
    else:
        return HTTPNotFound()

@view_defaults(renderer='jsonp')
class API(object):
    def __init__(self, request):
        self.request = request
        self.status = 'error'
        self.errmsg = ''
        self.response = {}

    def finish(self):
        if self.status is 'error':
            self.response['errmsg'] = self.errmsg
        return {'status': self.status, 'response': self.response}

    @view_config(route_name='api_set')
    def set(self):
        try:
            url = urlparse(self.request.params['url'])
        except KeyError:
            self.errmsg = 'Required paraneter "url" not set.'
        else:
            if url.scheme in ['http', 'https']:
                shortened = self.request.redis.get('reverse-url:%s' % url.geturl())
                if not shortened:
                    shortened = ''.join(random.choice(ascii_letters + digits) for x in range(5))
                    self.request.redis.set('short-url:%s' % shortened, url.geturl())
                    self.request.redis.set('reverse-url:%s' % url.geturl(), shortened)
                self.status = 'success'
                self.response['short'] = self.request.route_url('redirect', shortened=shortened)
                self.response['url'] = url.geturl()
            else:
                self.errmsg = 'Not an allowed scheme'

        return self.finish()

    @view_config(route_name='api_get')
    def get(self):
        try:
            shortened = self.request.params['shortened']
        except KeyError:
            self.errmsg = 'Required parameter "shortened" not set.'
        else:
            url = self.request.redis.get('short-url:%s' % shortened)
            if not url:
                self.errmsg = 'Not in use.'
            else:
                self.status = 'success'
                self.response['url'] = url.decode('utf-8')

        return self.finish()
<commit_msg>Better name for home view callable<commit_after>from pyramid.view import view_config, view_defaults
from pyramid.httpexceptions import HTTPNotFound, HTTPFound

from urllib.parse import urlparse
from string import ascii_letters, digits
import random

@view_config(route_name='home', renderer='templates/index.mako')
def home(request):
    return {'project': 'Krympa'}

@view_config(route_name='redirect')
def redirect(request):
    shortened = request.matchdict['shortened']
    url = request.redis.get('short-url:%s' % shortened)
    if url:
        return HTTPFound(location=url.decode('utf-8'))
    else:
        return HTTPNotFound()

@view_defaults(renderer='jsonp')
class API(object):
    def __init__(self, request):
        self.request = request
        self.status = 'error'
        self.errmsg = ''
        self.response = {}

    def finish(self):
        if self.status is 'error':
            self.response['errmsg'] = self.errmsg
        return {'status': self.status, 'response': self.response}

    @view_config(route_name='api_set')
    def set(self):
        try:
            url = urlparse(self.request.params['url'])
        except KeyError:
            self.errmsg = 'Required paraneter "url" not set.'
        else:
            if url.scheme in ['http', 'https']:
                shortened = self.request.redis.get('reverse-url:%s' % url.geturl())
                if not shortened:
                    shortened = ''.join(random.choice(ascii_letters + digits) for x in range(5))
                    self.request.redis.set('short-url:%s' % shortened, url.geturl())
                    self.request.redis.set('reverse-url:%s' % url.geturl(), shortened)
                self.status = 'success'
                self.response['short'] = self.request.route_url('redirect', shortened=shortened)
                self.response['url'] = url.geturl()
            else:
                self.errmsg = 'Not an allowed scheme'

        return self.finish()

    @view_config(route_name='api_get')
    def get(self):
        try:
            shortened = self.request.params['shortened']
        except KeyError:
            self.errmsg = 'Required parameter "shortened" not set.'
        else:
            url = self.request.redis.get('short-url:%s' % shortened)
            if not url:
                self.errmsg = 'Not in use.'
            else:
                self.status = 'success'
                self.response['url'] = url.decode('utf-8')

        return self.finish()

{'fraction_non_alphanumeric': 0.1276509796000808, 'fraction_numerical': 0.001211876388608362, 'mean_word_length': 2.272967614011897, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '476257', 'n_tokens_mistral': 1469, 'n_tokens_neox': 1398, 'n_words': 313}