<commit_before>from collections import defaultdict

from flask import Blueprint, jsonify, request, redirect, current_app

from usb.models import db, Redirect, DesktopRedirect, TabletRedirect, MobileRedirect
from usb.utils import get_device_model_from_string, get_device_model_from_request

api = Blueprint('api', __name__)


@api.route('/urls')
def get_list_of_urls():
    redirects = Redirect.query.all()
    result = defaultdict(list)
    for redirect in redirects:
        result[redirect.short].append({
            'type': redirect.type,
            'url': redirect.url,
            'redirects': redirect.count,
            'datetime': redirect.datetime
        })
    return jsonify(result), 200


@api.route('/urls', methods=['POST'])
def create_short_url():
    long_url = request.json['url']
    desktop_redirect = DesktopRedirect('', long_url)
    db.session.add(desktop_redirect)
    db.session.commit()
    short_id = current_app.shortener.encode(desktop_redirect.id)
    desktop_redirect.short = short_id
    db.session.add(TabletRedirect(short_id, long_url))
    db.session.add(MobileRedirect(short_id, long_url))
    db.session.commit()
    short_url = f'http://{current_app.config["SHORT_URL_DOMAIN"]}/{short_id}'
    return jsonify(url=short_url), 200


@api.route('/urls/<string:short_id>', methods=['PATCH'])
def update_short_url(short_id):
    data = request.json
    if Redirect.query.filter_by(short=short_id).first() is None:
        return jsonify({}), 404
    for key in data:
        device_model = get_device_model_from_string(key)
        device_model.query.filter_by(short=short_id).delete()
        db.session.add(device_model(short_id, data[key]))
    if data:
        db.session.commit()
    return jsonify({}), 200


@api.route('/<string:short_id>')
@api.route('/urls/<string:short_id>')
def redirect_from_short_url(short_id):
    device_model = get_device_model_from_request(request)
    redirect_instance = device_model.query.filter_by(short=short_id).first()<commit_msg>Explain "to store" / "not to store" hash alternatives<commit_after>from collections import defaultdict

from flask import Blueprint, jsonify, request, redirect, current_app

from usb.models import db, Redirect, DesktopRedirect, TabletRedirect, MobileRedirect
from usb.utils import get_device_model_from_string, get_device_model_from_request

api = Blueprint('api', __name__)


@api.route('/urls')
def get_list_of_urls():
    redirects = Redirect.query.all()
    result = defaultdict(list)
    for redirect in redirects:
        result[redirect.short].append({
            'type': redirect.type,
            'url': redirect.url,
            'redirects': redirect.count,
            'datetime': redirect.datetime
        })
    return jsonify(result), 200


@api.route('/urls', methods=['POST'])
def create_short_url():
    long_url = request.json['url']
    desktop_redirect = DesktopRedirect('', long_url)
    db.session.add(desktop_redirect)
    db.session.commit()
    # Actually, we have two ways here:
    # 1) store a hash (`short_id`) in a DB and just filter records by its value
    # 2) don't store hash and decode it each time to access the right record
    # (2) requires sequence generator (like one in PosgreSQL or INCR in Redis)
    short_id = current_app.shortener.encode(desktop_redirect.id)
    desktop_redirect.short = short_id
    db.session.add(TabletRedirect(short_id, long_url))
    db.session.add(MobileRedirect(short_id, long_url))
    db.session.commit()
    short_url = f'http://{current_app.config["SHORT_URL_DOMAIN"]}/{short_id}'
    return jsonify(url=short_url), 200


@api.route('/urls/<string:short_id>', methods=['PATCH'])
def update_short_url(short_id):
    data = request.json
    if Redirect.query.filter_by(short=short_id).first() is None:
        return jsonify({}), 404
    for key in data:
        device_model = get_device_model_from_string(key)
        device_model.query.filter_by(short=short_id).delete()
        db.session.add(device_model(short_id, data[key]))
    if data:
        db.session.commit()
    return jsonify({}), 200


@api.route('/<string:short_id>')
@api.route('/urls/<string:short_id>')
def redirect_from_short_url(short_id):
    device_model = get_device_model_from_request(request)
    redirect_instance = device_model.query.filter_by(short=short_id).first()
{'fraction_non_alphanumeric': 0.12248671134735382, 'fraction_numerical': 0.00623988906863878, 'mean_word_length': 4.146254458977408, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '772865', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1361, 'n_words': 277}