<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""links/views.py: Links views."""


import re
from flask import render_template, Blueprint, request, redirect, url_for,\
    flash
from flask.ext.login import login_required, current_user
from shortener import app, db, random_str
from shortener.models import Link
from datetime import datetime

links_blueprint = Blueprint(
    'links', __name__,
    template_folder='templates'
)


@links_blueprint.route('/list')
@login_required
def list():
    """Page with list of links."""
    links = Link.query.filter_by(user_id=current_user.id).all()
    return render_template('links.html', links=links)


@links_blueprint.route('/add', methods=['GET', 'POST'])
@login_required
def add():
    """Page with list of links and a form to add links."""
    link = None
    if request.method == "POST":
        url = request.form.get('url')
        if not url:
            flash('Forget to add a link?')
            return render_template('add.html')
        # validate url with Django url checker
        url_regex = re.compile(
            r'^(?:http|ftp)s?://'  # http:// or https://
            r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|'
            r'[A-Z0-9-]{2,}\.?)|'  # domain...
            r'localhost|'  # localhost...
            r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # ...or ip
            r'(?::\d+)?'  # optional port
            r'(?:/?|[/?]\S+)$', re.IGNORECASE)
        if not re.match(url_regex, url):
            flash('That link isn\'t formatted correctly.')
            return render_template('add.html')

        # check if that user has already saved this link
        pre_existing = Link.query.filter_by(
            user_id=current_user.id, url=url).first()
        if pre_existing:
            flash('This link has already been added by you in the past.')
            link = pre_existing
        else:
            # get unique slug
            unique_slug = None
            while not unique_slug:
                slug = random_str(6)
                if not Link.query.filter_by(slug=slug).first():
                    unique_slug = slug

            # add link
            link = Link(url, slug, current_user)
            db.session.add(link)
            db.session.commit()
            flash('Your link was added.')

    return render_template('add.html', link=link)


@links_blueprint.route('/')
def home():
    """Redirector, and logger of links."""
    return redirect(url_for('users.login'))


@links_blueprint.route('/<path:path>')
def redirect_link(path):<commit_msg>redirect to add from home page if logged in<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""links/views.py: Links views."""


import re
from flask import render_template, Blueprint, request, redirect, url_for,\
    flash
from flask.ext.login import login_required, current_user
from shortener import app, db, random_str
from shortener.models import Link
from datetime import datetime

links_blueprint = Blueprint(
    'links', __name__,
    template_folder='templates'
)


@links_blueprint.route('/list')
@login_required
def list():
    """Page with list of links."""
    links = Link.query.filter_by(user_id=current_user.id).all()
    return render_template('links.html', links=links)


@links_blueprint.route('/add', methods=['GET', 'POST'])
@login_required
def add():
    """Page with list of links and a form to add links."""
    link = None
    if request.method == "POST":
        url = request.form.get('url')
        if not url:
            flash('Forget to add a link?')
            return render_template('add.html')
        # validate url with Django url checker
        url_regex = re.compile(
            r'^(?:http|ftp)s?://'  # http:// or https://
            r'(?:(?:[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?\.)+(?:[A-Z]{2,6}\.?|'
            r'[A-Z0-9-]{2,}\.?)|'  # domain...
            r'localhost|'  # localhost...
            r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})'  # ...or ip
            r'(?::\d+)?'  # optional port
            r'(?:/?|[/?]\S+)$', re.IGNORECASE)
        if not re.match(url_regex, url):
            flash('That link isn\'t formatted correctly.')
            return render_template('add.html')

        # check if that user has already saved this link
        pre_existing = Link.query.filter_by(
            user_id=current_user.id, url=url).first()
        if pre_existing:
            flash('This link has already been added by you in the past.')
            link = pre_existing
        else:
            # get unique slug
            unique_slug = None
            while not unique_slug:
                slug = random_str(6)
                if not Link.query.filter_by(slug=slug).first():
                    unique_slug = slug

            # add link
            link = Link(url, slug, current_user)
            db.session.add(link)
            db.session.commit()
            flash('Your link was added.')

    return render_template('add.html', link=link)


@links_blueprint.route('/')
def home():
    """Redirector, and logger of links."""
    if current_user.is_authenticated():
        redirect(url_for('links.add'))
    return redirect(url_for('users.login'))


@links_blueprint.route('/<path:path>')
def redirect_link(path):
{'fraction_non_alphanumeric': 0.15713471404142124, 'fraction_numerical': 0.009120273608208246, 'mean_word_length': 2.323232323232323, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '6167271', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1636, 'n_words': 406}