<commit_before>from functools import wraps
from flask import \
    render_template, redirect, url_for, \
    abort, flash, request, current_app, \
    make_response
from flask_login import login_required, current_user, login_user, logout_user

from . import app, db
from .models import *
from .forms import *

from datetime import date, datetime, timedelta
from calendar import Calendar
from urllib.parse import quote

print('Processing views.py')

valid_paths = ['/', '/index', '/login', '/logout', '/register']

def next_is_valid(next):
    for i in valid_paths:
        if next == i:
            return True
    return False

def logout_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if current_user.is_anonymous:
            return f(*args, **kwargs)
        return redirect(url_for('index_route'))
    return decorated_function

@app.route('/')
def root():
    return redirect(url_for('login_route'))
    #render_template('index.html')

@app.route('/index')
@login_required
def index_route():
    return render_template('index.html', user=current_user)

@app.route('/login', methods=['GET', 'POST'])
@logout_required
def login_route():
    form = LoginForm()
    if form.validate_on_submit():
        username = request.form['username']
        password = request.form['password']

        user = User.query.filter_by(username=username).first()
        if user is None or not user.verify_password(password):
            flash('Invalid username/password pair')
            return render_template('login.html', title='Sign In', form=form)
        login_user(user)
        flash('Logged in successfully as {}'.format(username))
        next = request.args.get('next')
        if not next_is_valid(next):
            return redirect(url_for('index_route'))
        return redirect(next or url_for('index_route'))
    return render_template('login.html', title='Sign In', form=form)

@app.route('/logout')
@login_required
def logout_route():
    logout_user()
    return redirect(url_for('login_route'))

@app.route('/register', methods=['GET','POST'])
@logout_required
def register_route():
    form = RegisterForm()
    if form.validate_on_submit():
        username = request.form['username']
        pw = request.form['password']
        pw_retype = request.form['retype_password']

        same_count = User.query.filter_by(username=username).count()
        if same_count != 0:
            flash('Username {} already taken'.format(username))
        elif pw != pw_retype:
            flash('Passwords do not match')
        else:
            new_user = User(username, pw)
            db.session.add(new_user)
            db.session.commit()
            flash('Successfully registered')
            return redirect(url_for('login_route'))
    return render_template('register.html', title='Register', form=form)

# Returns the date of the first day of the next month
def get_next_month(this_month):
    # increasing the date by 40 days should
    m = ((this_month.month + 1) % 13) + 1
    y = this_month.year
    if m == 1:
        y += 1
    return date(y, m, 1)

# get_month_and_events :: [Week [Day Name Date [Event ID Name]]]
def get_month_and_events(year, month):
    cal = Calendar(0) # default replace by user db?
    the_month = cal.monthdatescalendar(year, month)

    begin = the_month[0][0]
    end = the_month[-1][-1]
    events = Event.query.filter(
        Event.event_date > begin.strftime('%Y-%m-%d'),
        Event.event_date < end.strftime('%Y-%m-%d'))
    print(events)

    def per_day(day):
        return (day.strftime('%A'), day)

    def per_week(week):
        return [per_day(d) for d in week]

    def per_month(month):
        return [per_week(w) for w in month]

    return per_month(the_month)

# get_day_and_events :: [Event ID Name]
def get_day_and_events(year, month, day):
    today = date(year,month,day)
    tomorrow = today + timedelta(days=1)
    events = Event.query.filter(
        Event.event_date > today.strftime('%Y-%m-%d'),
        Event.event_date < tomorrow.strftime('%Y-%m-%d')).all()
    return events

@app.route('/calendar', methods=['GET','POST'])
def calendar_route():
    unsubscribe = request.form.get('unsubscribe')
    if unsubscribe is not None:
        s = Subscription.query\
            .filter(Subscription.event_id == int(unsubscribe))\
            .filter(Subscription.user_id == current_user.id).one()

        db.session.delete(s)
        db.session.commit()

        flash('Unsubscribed from event')

    subscribe = request.form.get('subscribe')
    if subscribe is not None:
        s = Subscription(current_user.id, int(subscribe))
        db.session.add(s)
        db.session.commit()
        flash('Subscribed to event')

    now = date.today()
    year = int(request.args.get('year') or now.year)
    month = int(request.args.get('month') or now.month)
    day = request.args.get('day')

    at_month = date(2016, month, 1)
    month_name = at_month.strftime("%B")

    if day is None:
        month_and_events = get_month_and_events(year, month)
        return render_template(<commit_msg>integrity of subscriptions<commit_after>from functools import wraps
from flask import \
    render_template, redirect, url_for, \
    abort, flash, request, current_app, \
    make_response
from flask_login import login_required, current_user, login_user, logout_user

from . import app, db
from .models import *
from .forms import *

from datetime import date, datetime, timedelta
from calendar import Calendar
from urllib.parse import quote

print('Processing views.py')

valid_paths = ['/', '/index', '/login', '/logout', '/register']

def next_is_valid(next):
    for i in valid_paths:
        if next == i:
            return True
    return False

def logout_required(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        if current_user.is_anonymous:
            return f(*args, **kwargs)
        return redirect(url_for('index_route'))
    return decorated_function

@app.route('/')
def root():
    return redirect(url_for('login_route'))
    #render_template('index.html')

@app.route('/index')
@login_required
def index_route():
    return render_template('index.html', user=current_user)

@app.route('/login', methods=['GET', 'POST'])
@logout_required
def login_route():
    form = LoginForm()
    if form.validate_on_submit():
        username = request.form['username']
        password = request.form['password']

        user = User.query.filter_by(username=username).first()
        if user is None or not user.verify_password(password):
            flash('Invalid username/password pair')
            return render_template('login.html', title='Sign In', form=form)
        login_user(user)
        flash('Logged in successfully as {}'.format(username))
        next = request.args.get('next')
        if not next_is_valid(next):
            return redirect(url_for('index_route'))
        return redirect(next or url_for('index_route'))
    return render_template('login.html', title='Sign In', form=form)

@app.route('/logout')
@login_required
def logout_route():
    logout_user()
    return redirect(url_for('login_route'))

@app.route('/register', methods=['GET','POST'])
@logout_required
def register_route():
    form = RegisterForm()
    if form.validate_on_submit():
        username = request.form['username']
        pw = request.form['password']
        pw_retype = request.form['retype_password']

        same_count = User.query.filter_by(username=username).count()
        if same_count != 0:
            flash('Username {} already taken'.format(username))
        elif pw != pw_retype:
            flash('Passwords do not match')
        else:
            new_user = User(username, pw)
            db.session.add(new_user)
            db.session.commit()
            flash('Successfully registered')
            return redirect(url_for('login_route'))
    return render_template('register.html', title='Register', form=form)

# Returns the date of the first day of the next month
def get_next_month(this_month):
    # increasing the date by 40 days should
    m = ((this_month.month + 1) % 13) + 1
    y = this_month.year
    if m == 1:
        y += 1
    return date(y, m, 1)

# get_month_and_events :: [Week [Day Name Date [Event ID Name]]]
def get_month_and_events(year, month):
    cal = Calendar(0) # default replace by user db?
    the_month = cal.monthdatescalendar(year, month)

    begin = the_month[0][0]
    end = the_month[-1][-1]
    events = Event.query.filter(
        Event.event_date > begin.strftime('%Y-%m-%d'),
        Event.event_date < end.strftime('%Y-%m-%d'))
    print(events)

    def per_day(day):
        return (day.strftime('%A'), day)

    def per_week(week):
        return [per_day(d) for d in week]

    def per_month(month):
        return [per_week(w) for w in month]

    return per_month(the_month)

# get_day_and_events :: [Event ID Name]
def get_day_and_events(year, month, day):
    today = date(year,month,day)
    tomorrow = today + timedelta(days=1)
    events = Event.query.filter(
        Event.event_date > today.strftime('%Y-%m-%d'),
        Event.event_date < tomorrow.strftime('%Y-%m-%d')).all()
    return events

@app.route('/calendar', methods=['GET','POST'])
def calendar_route():
    unsubscribe = request.form.get('unsubscribe')
    if unsubscribe is not None:
        xs = Subscription.query\
            .filter(Subscription.event_id == int(unsubscribe))\
            .filter(Subscription.user_id == current_user.id).all()

        for s in xs:
            db.session.delete(s)
        db.session.commit()

        flash('Unsubscribed from event')

    subscribe = request.form.get('subscribe')
    if subscribe is not None:
        xs = Subscription.query\
            .filter(Subscription.event_id == int(subscribe))\
            .filter(Subscription.user_id == current_user.id).all()
        if len(xs) == 0:
            s = Subscription(current_user.id, int(subscribe))
            db.session.add(s)
            db.session.commit()
            flash('Subscribed to event')

    now = date.today()
    year = int(request.args.get('year') or now.year)
    month = int(request.args.get('month') or now.month)
    day = request.args.get('day')

    at_month = date(2016, month, 1)
    month_name = at_month.strftime("%B")

    if day is None:
        month_and_events = get_month_and_events(year, month)
        return render_template(
{'fraction_non_alphanumeric': 0.1317502872462658, 'fraction_numerical': 0.0041171964764458065, 'mean_word_length': 3.062621548035784, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7003650', 'n_tokens_mistral': 3262, 'n_tokens_neox': 3177, 'n_words': 762}