<commit_before>import email
import smtplib

from flask import Blueprint, request, render_template, redirect, url_for, abort, current_app, flash
from flask.ext.babel import lazy_gettext as l_, _
from werkzeug.exceptions import ServiceUnavailable

from formencode import Schema, All
from formencode.validators import FieldsMatch, Email, String, NotEmpty
from sprox.formbase import AddRecordForm, Field
from sprox.validators import UniqueValue
from sprox.sa.provider import SAORMProvider
from tw.forms import PasswordField, TextField, HiddenField
from tw.forms.validators import UnicodeString

from sqlalchemy import func
from sqlalchemy.orm import joinedload

from skylines.model import DBSession, User, Group
from skylines.forms import BootstrapForm, club
from skylines.lib.decorators import validate

users_blueprint = Blueprint('users', 'skylines')


password_match_validator = FieldsMatch(
    'password', 'verify_password',
    messages={'invalidNoMatch': l_('Passwords do not match')})

user_validator = Schema(chained_validators=(password_match_validator,))


class NewUserForm(AddRecordForm):
    __base_widget_type__ = BootstrapForm
    __model__ = User
    __required_fields__ = ['password']
    __limit_fields__ = ['email_address', 'name', 'password', 'verify_password', 'club']
    __base_validator__ = user_validator
    __field_widget_args__ = {
        'email_address': dict(label_text=l_('eMail Address')),
        'name': dict(label_text=l_('Name')),
        'club': dict(label_text=l_('Club')),
        'password': dict(label_text=l_('Password')),
    }

    email_address = Field(TextField, All(UniqueValue(SAORMProvider(DBSession),
                                                     __model__, 'email_address'),
                                         Email(not_empty=True)))
    name = Field(TextField, NotEmpty)
    club = club.SelectField
    password = String(min=6)
    verify_password = PasswordField('<PASSWORD>password',
                                    label_text=l_('Verify Password'))

new_user_form = NewUserForm(DBSession)


@users_blueprint.route('/')
def index():
    users = User.query() \
        .options(joinedload(User.club)) \
        .order_by(func.lower(User.name))

    return render_template('users/list.jinja',
                           active_page='settings',
                           users=users)


@users_blueprint.route('/new')
def new():
    return render_template('users/new.jinja',
                           active_page='users',
                           form=new_user_form)


@users_blueprint.route('/new', methods=['POST'])
@validate(new_user_form, new)
def new_post():
    user = User(name=request.form['name'],
                email_address=request.form['email_address'],
                password=request.form['password'])

    if request.form['club']:
        user.club_id = request.form['club']

    user.created_ip = request.remote_addr
    user.generate_tracking_key()
    DBSession.add(user)

    pilots = Group.query(group_name='pilots').first()
    if pilots:
        pilots.users.append(user)

    DBSession.commit()

    return redirect(url_for('index'))


recover_email_form = BootstrapForm(
    'recover_email_form',
    submit_text=l_("Recover Password"),
    action='recover_email',
    children=[
        TextField('email_address',
                  validator=Email(not_empty=True),
                  label_text=l_('eMail Address'))
    ]
)


recover_password_form = BootstrapForm(
    'recover_password_form',
    submit_text=l_("Recover Password"),
    action='recover_password',
    validator=Schema(chained_validators=(password_match_validator,)),
    children=[
        HiddenField('key'),
        PasswordField('password',
                      validator=UnicodeString(min=6),
                      label_text=l_('Password')),
        PasswordField('verify_password',
                      label_text=l_('Verify Password')),
    ]
)


@users_blueprint.route('/recover')
def recover():
    try:
        key = int(request.values['key'], 16)
    except:
        key = None

    if key:
        user = User.by_recover_key(key)
        if not user:
            abort(404)

        return render_template(
            'generic/form.jinja', active_page='users',
            form=recover_password_form, values=dict(key='%x' % key))

    return render_template(
        'generic/form.jinja', active_page='users',
        form=recover_email_form, values={})


def recover_user_password(user):
    key = user.generate_recover_key(request.remote_addr)

    text = u"""Hi %s,

you have asked to recover your password (from IP %s).  To enter a new
password, click on the following link:

 http://skylines.xcsoar.org/users/recover?key=%x

The SkyLines Team
""" % (unicode(user), request.remote_addr, key)

    msg = email.mime.text.MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
    msg['Subject'] = 'SkyLines password recovery'
    msg['From'] = current_app.config['EMAIL_FROM']
    msg['To'] = user.email_address.encode('ascii')
    msg['Date'] = email.Utils.formatdate(localtime=1)

    try:
        smtp = smtplib.SMTP(current_app.config['SMTP_SERVER'])
        smtp.ehlo()
        smtp.sendmail(current_app.config['EMAIL_FROM'].encode('ascii'),
                      user.email_address.encode('ascii'), msg.as_string())<commit_msg>views/users: Fixed import issues<commit_after>from email.mime.text import MIMEText
from email.utils import formatdate
import smtplib

from flask import Blueprint, request, render_template, redirect, url_for, abort, current_app, flash
from flask.ext.babel import lazy_gettext as l_, _
from werkzeug.exceptions import ServiceUnavailable

from formencode import Schema, All
from formencode.validators import FieldsMatch, Email, String, NotEmpty
from sprox.formbase import AddRecordForm, Field
from sprox.validators import UniqueValue
from sprox.sa.provider import SAORMProvider
from tw.forms import PasswordField, TextField, HiddenField
from tw.forms.validators import UnicodeString

from sqlalchemy import func
from sqlalchemy.orm import joinedload

from skylines.model import DBSession, User, Group
from skylines.forms import BootstrapForm, club
from skylines.lib.decorators import validate

users_blueprint = Blueprint('users', 'skylines')


password_match_validator = FieldsMatch(
    'password', 'verify_password',
    messages={'invalidNoMatch': l_('Passwords do not match')})

user_validator = Schema(chained_validators=(password_match_validator,))


class NewUserForm(AddRecordForm):
    __base_widget_type__ = BootstrapForm
    __model__ = User
    __required_fields__ = ['password']
    __limit_fields__ = ['email_address', 'name', 'password', 'verify_password', 'club']
    __base_validator__ = user_validator
    __field_widget_args__ = {
        'email_address': dict(label_text=l_('eMail Address')),
        'name': dict(label_text=l_('Name')),
        'club': dict(label_text=l_('Club')),
        'password': dict(label_text=l_('Password')),
    }

    email_address = Field(TextField, All(UniqueValue(SAORMProvider(DBSession),
                                                     __model__, 'email_address'),
                                         Email(not_empty=True)))
    name = Field(TextField, NotEmpty)
    club = club.SelectField
    password = String(min=6)
    verify_password = PasswordField('<PASSWORD>',
                                    label_text=l_('Verify Password'))

new_user_form = NewUserForm(DBSession)


@users_blueprint.route('/')
def index():
    users = User.query() \
        .options(joinedload(User.club)) \
        .order_by(func.lower(User.name))

    return render_template('users/list.jinja',
                           active_page='settings',
                           users=users)


@users_blueprint.route('/new')
def new():
    return render_template('users/new.jinja',
                           active_page='users',
                           form=new_user_form)


@users_blueprint.route('/new', methods=['POST'])
@validate(new_user_form, new)
def new_post():
    user = User(name=request.form['name'],
                email_address=request.form['email_address'],
                password=request.form['password'])

    if request.form['club']:
        user.club_id = request.form['club']

    user.created_ip = request.remote_addr
    user.generate_tracking_key()
    DBSession.add(user)

    pilots = Group.query(group_name='pilots').first()
    if pilots:
        pilots.users.append(user)

    DBSession.commit()

    return redirect(url_for('index'))


recover_email_form = BootstrapForm(
    'recover_email_form',
    submit_text=l_("Recover Password"),
    action='recover_email',
    children=[
        TextField('email_address',
                  validator=Email(not_empty=True),
                  label_text=l_('eMail Address'))
    ]
)


recover_password_form = BootstrapForm(
    'recover_password_form',
    submit_text=l_("Recover Password"),
    action='recover_password',
    validator=Schema(chained_validators=(password_match_validator,)),
    children=[
        HiddenField('key'),
        PasswordField('password',
                      validator=UnicodeString(min=6),
                      label_text=l_('Password')),
        PasswordField('<PASSWORD>_password',
                      label_text=l_('Verify Password')),
    ]
)


@users_blueprint.route('/recover')
def recover():
    try:
        key = int(request.values['key'], 16)
    except:
        key = None

    if key:
        user = User.by_recover_key(key)
        if not user:
            abort(404)

        return render_template(
            'generic/form.jinja', active_page='users',
            form=recover_password_form, values=dict(key='%x' % key))

    return render_template(
        'generic/form.jinja', active_page='users',
        form=recover_email_form, values={})


def recover_user_password(user):
    key = user.generate_recover_key(request.remote_addr)

    text = u"""Hi %s,

you have asked to recover your password (from IP %s).  To enter a new
password, click on the following link:

 http://skylines.xcsoar.org/users/recover?key=%x

The SkyLines Team
""" % (unicode(user), request.remote_addr, key)

    msg = MIMEText(text.encode('utf-8'), 'plain', 'utf-8')
    msg['Subject'] = 'SkyLines password recovery'
    msg['From'] = current_app.config['EMAIL_FROM']
    msg['To'] = user.email_address.encode('ascii')
    msg['Date'] = formatdate(localtime=1)

    try:
        smtp = smtplib.SMTP(current_app.config['SMTP_SERVER'])
        smtp.ehlo()
        smtp.sendmail(current_app.config['EMAIL_FROM'].encode('ascii'),
                      user.email_address.encode('ascii'), msg.as_string())
{'fraction_non_alphanumeric': 0.11965491372843211, 'fraction_numerical': 0.0018754688672168042, 'mean_word_length': 3.167643610785463, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '479647', 'n_tokens_mistral': 3250, 'n_tokens_neox': 3144, 'n_words': 604}