<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

from flask import Flask, request, redirect, url_for, render_template, abort, make_response, flash, jsonify, session
from flask.ext.login import LoginManager, login_user, logout_user, login_required, current_user
from flask.ext.oauthlib.client import OAuth

from sqlalchemy import or_
from sqlalchemy.exc import IntegrityError

from imgtl.db import *
from imgtl.const import *
from imgtl.i18n import i18n
from imgtl.common import do_upload_image, do_delete_image
import imgtl.lib
import imgtl.validator


app = Flask(__name__)
app.config.from_pyfile('imgtl.cfg')
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

db.init_app(app)
db.app = app
log_db.init_app(app)
log_db.app = app

login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.login_message = i18n('youmustlogin')
login_manager.init_app(app)

oauth = OAuth()
oauth.init_app(app)
twitter = oauth.remote_app('twitter',
    base_url='https://api.twitter.com/1.1/',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authorize',
    consumer_key=app.config['TWITTER_CONSUMER_KEY'],
    consumer_secret=app.config['TWITTER_CONSUMER_SECRET'],
    access_token_method='POST',
)


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(user_id)

@app.route('/')
def index():
    if current_user.is_anonymous():
        return render_template('index.html')
    else:
        return render_template('mypage.html', user=current_user)

@app.route('/settings', methods=['GET', 'POST'])
@login_required
def settings():
    if request.method == 'GET':
        return render_template('settings.html', user=current_user)
    elif request.method == 'POST':
        if request.form['what'] == 'token':
            while 1:
                try:
                    current_user.token = imgtl.lib.make_token()
                    db.session.commit()
                except IntegrityError:
                    db.session.rollback()
                    continue
                else:
                    break
            return jsonify({'token': current_user.token})
        elif request.form['what'] == 'update':
            if 'password' in request.form and request.form['password'] is not '':
                if not imgtl.validator.password(request.form['passowrd']):
                    flash(i18n('invalidpassowrd'))
                    return redirect(url_for('settings'))
                elif request.form['password'] != request.form['passwordconfirm']:
                    flash(i18n('passwordmismatch'))
                    return redirect(url_for('settings'))
                else:
                    current_user.password = request.form['password']
                    db.session.commit()
            new_email = request.form['email']
            new_username = request.form['username']
            if not imgtl.validator.email(new_email):
                flash(i18n('invalidemail'))
                return redirect(url_for('settings'))
            if not imgtl.validator.username(new_username):
                flash(i18n('invalidusername'))
                return redirect(url_for('settings'))
            if current_user.email != new_email:
                if User.query.filter_by(email=new_email).first():
                    flash(i18n('alreadyexistemail'))
                    return redirect(url_for('settings'))
            if current_user.name != new_username:
                if User.query.filter_by(name=new_username).first():
                    flash(i18n('alreadyexistname'))
                    return redirect(url_for('settings'))
            current_user.email = new_email
            current_user.name = new_username
            db.session.commit()
            return redirect(url_for('settings'))

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    if request.method == 'GET':
        return redirect(url_for('login', _anchor='register'))
    elif request.method == 'POST':
        if not imgtl.validator.email(request.form['email']):
            flash(i18n('invalidemail'))
            return redirect(url_for('signup'))
        if not imgtl.validator.username(request.form['username']):
            flash(i18n('invalidusername'))
            return redirect(url_for('signup'))
        if not imgtl.validator.password(request.form['password']):
            flash(i18n('invalidpassword'))
            return redirect(url_for('signup'))
        if request.form['password'] != request.form['passwordconfirm']:
            flash(i18n('passwordmismatch'))
            return redirect(url_for('signup'))
        user = User.query.filter(or_(User.email==request.form['email'], User.name==request.form['username'])).first()
        if user:
            if user.email == request.form['email']:
                flash(i18n('alreadyexistemail'))
                return redirect(url_for('signup'))
            elif user.name == request.form['username']:
                flash(i18n('alreadyexistname'))
                return redirect(url_for('signup'))
        user = User(email=request.form['email'], name=request.form['username'], password=imgtl.lib.pw_hash(request.form['password']))
        while 1:
            try:
                user.token = imgtl.lib.make_token()
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
                continue
            else:
                break
        db.session.add(user)
        db.session.commit()
        login_user(user)
        return redirect(url_for('index'))

@app.route('/signup/check', methods=['POST'])
def signup_check():
    if request.form['what'] not in ['email', 'username']:
        abort(400)
    res = False
    if 'except' not in request.form or request.form['except'] != request.form['value']:<commit_msg>clarify some codes<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os

from flask import Flask, request, redirect, url_for, render_template, abort, make_response, flash, jsonify, session
from flask.ext.login import LoginManager, login_user, logout_user, login_required, current_user
from flask.ext.oauthlib.client import OAuth

from sqlalchemy import or_
from sqlalchemy.exc import IntegrityError

from imgtl.db import *
from imgtl.const import *
from imgtl.i18n import i18n
from imgtl.common import do_upload_image, do_delete_image
import imgtl.lib
import imgtl.validator


app = Flask(__name__)
app.config.from_pyfile('imgtl.cfg')
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

db.init_app(app)
db.app = app
log_db.init_app(app)
log_db.app = app

login_manager = LoginManager()
login_manager.login_view = 'login'
login_manager.login_message = i18n('youmustlogin')
login_manager.init_app(app)

oauth = OAuth()
oauth.init_app(app)
twitter = oauth.remote_app('twitter',
    base_url='https://api.twitter.com/1.1/',
    request_token_url='https://api.twitter.com/oauth/request_token',
    access_token_url='https://api.twitter.com/oauth/access_token',
    authorize_url='https://api.twitter.com/oauth/authorize',
    consumer_key=app.config['TWITTER_CONSUMER_KEY'],
    consumer_secret=app.config['TWITTER_CONSUMER_SECRET'],
    access_token_method='POST',
)


@login_manager.user_loader
def load_user(user_id):
    return User.query.get(user_id)

@app.route('/')
def index():
    if current_user.is_anonymous():
        return render_template('index.html')
    else:
        return render_template('mypage.html', user=current_user)

@app.route('/settings', methods=['GET', 'POST'])
@login_required
def settings():
    if request.method == 'GET':
        return render_template('settings.html', user=current_user)
    elif request.method == 'POST':
        if request.form['what'] == 'token':
            while 1:
                try:
                    current_user.token = imgtl.lib.make_token()
                    db.session.commit()
                except IntegrityError:
                    db.session.rollback()
                    continue
                else:
                    break
            return jsonify({'token': current_user.token})
        elif request.form['what'] == 'update':
            if 'password' in request.form and request.form['password'] is not '':
                if not imgtl.validator.password(request.form['passowrd']):
                    flash(i18n('invalidpassowrd'))
                    return redirect(url_for('settings'))
                elif request.form['password'] != request.form['passwordconfirm']:
                    flash(i18n('passwordmismatch'))
                    return redirect(url_for('settings'))
                else:
                    current_user.password = request.form['password']
                    db.session.commit()
            new_email = request.form['email']
            new_username = request.form['username']
            if not imgtl.validator.email(new_email):
                flash(i18n('invalidemail'))
                return redirect(url_for('settings'))
            if not imgtl.validator.username(new_username):
                flash(i18n('invalidusername'))
                return redirect(url_for('settings'))
            if current_user.email != new_email:
                if User.query.filter_by(email=new_email).first():
                    flash(i18n('alreadyexistemail'))
                    return redirect(url_for('settings'))
            if current_user.name != new_username:
                if User.query.filter_by(name=new_username).first():
                    flash(i18n('alreadyexistname'))
                    return redirect(url_for('settings'))
            current_user.email = new_email
            current_user.name = new_username
            db.session.commit()
            return redirect(url_for('settings'))

@app.route('/signup', methods=['GET', 'POST'])
def signup():
    if request.method == 'GET':
        return redirect(url_for('login', _anchor='register'))
    elif request.method == 'POST':
        if not imgtl.validator.email(request.form['email']):
            flash(i18n('invalidemail'))
            return redirect(url_for('signup'))
        if not imgtl.validator.username(request.form['username']):
            flash(i18n('invalidusername'))
            return redirect(url_for('signup'))
        if not imgtl.validator.password(request.form['password']):
            flash(i18n('invalidpassword'))
            return redirect(url_for('signup'))
        if request.form['password'] != request.form['passwordconfirm']:
            flash(i18n('passwordmismatch'))
            return redirect(url_for('signup'))
        user = User.query.filter((User.email==request.form['email']) | (User.name==request.form['username'])).first()
        if user:
            if user.email == request.form['email']:
                flash(i18n('alreadyexistemail'))
                return redirect(url_for('signup'))
            elif user.name == request.form['username']:
                flash(i18n('alreadyexistname'))
                return redirect(url_for('signup'))
        user = User(email=request.form['email'], name=request.form['username'], password=imgtl.lib.pw_hash(request.form['password']))
        while 1:
            try:
                user.token = imgtl.lib.make_token()
                db.session.commit()
            except IntegrityError:
                db.session.rollback()
                continue
            else:
                break
        db.session.add(user)
        db.session.commit()
        login_user(user)
        return redirect(url_for('index'))

@app.route('/signup/check', methods=['POST'])
def signup_check():
    if request.form['what'] not in ['email', 'username']:
        abort(400)
    res = False
    if 'except' not in request.form or request.form['except'] != request.form['value']:
{'fraction_non_alphanumeric': 0.13003095975232198, 'fraction_numerical': 0.006359300476947536, 'mean_word_length': 2.536094674556213, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 8, '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': '164270', 'n_tokens_mistral': 3537, 'n_tokens_neox': 3412, 'n_words': 605}