# -*- coding: utf-8 -*-

from slugify import slugify
from flask import Blueprint
from flask import jsonify
from flask_wtf import Form
from wtforms import StringField
from wtforms.validators import DataRequired
from wtforms.validators import ValidationError
from wtforms.validators import Length
from rio.utils.user import get_current_user_id
from rio.utils.user import login_required
from rio.utils.slugify import slugify
from rio.models  import add_instance
from rio.models  import delete_instance
from rio.models  import get_data_or_404

bp = Blueprint('dashboard', __name__)

class NewProjectForm(Form):
    name = StringField('Name', validators=[DataRequired(), Length(max=64)])

class ConfirmDeleteProjectForm(Form):
    name = StringField('Name', validators=[DataRequired(), Length(max=64)])

@bp.errorhandler(404)
def handle_not_found(exception):
    return jsonify(message='not found'), 404

@bp.route('/projects/new', methods=['POST'])
@login_required
def new_project():
    """New Project."""
    form = NewProjectForm()
    if not form.validate_on_submit():
        return jsonify(errors=form.errors), 400

    data = form.data
    data['slug'] = slugify(data['name'])
    data['owner_id'] = get_current_user_id()

    id = add_instance('project', **data)

    if not id:
        return jsonify(errors={'name': ['duplicated slug.']}), 400

    project = get_data_or_404('project', id)

    return jsonify(**project)


@bp.route('/projects/<int:project_id>', methods=['DELETE'])
@login_required
def delete_project(project_id):
    """Delete Project."""
    project = get_data_or_404('project', project_id)

    if project['owner_id'] != get_current_user_id():
        return jsonify(message='forbidden'), 403

    delete_instance('project', project_id)

    return jsonify({})

@bp.route('/projects/<int:project_id>/transfer', methods=['POST'])
def transfer_project(project_id):
    pass

@@ -411,24 +411,71 @@
 ort slugify%0A
+from rio.utils.token import password_generator%0A
 from rio.mod
@@ -491,32 +491,32 @@
 rt add_instance%0A
-
 from rio.models 
@@ -839,16 +839,120 @@
 =64)%5D)%0A%0A
+class NewSenderForm(Form):%0A    slug = StringField('slug', validators=%5BDataRequired(), Length(max=64)%5D)%0A%0A
 @bp.erro
@@ -1904,24 +1904,24 @@
 roject_id)%0A%0A
-
     return j
@@ -1932,16 +1932,706 @@
 fy(%7B%7D)%0A%0A
+%0A@bp.route('/projects/%3Cint:project_id%3E/senders', methods=%5B'POST'%5D)%0Adef add_sender(project_id):%0A    %22%22%22Add sender.%22%22%22%0A%0A    project = get_data_or_404('project', project_id)%0A%0A    if project%5B'owner_id'%5D != get_current_user_id():%0A        return jsonify(message='forbidden'), 403%0A%0A    form = NewSenderForm()%0A    if not form.validate_on_submit():%0A        return jsonify(errors=form.errors), 400%0A%0A    data = form.data%0A    data%5B'project_id'%5D = project_id%0A    data%5B'token'%5D = password_generator(40)%0A%0A    id = add_instance('sender', **data)%0A%0A    if not id:%0A        return jsonify(errors=%7B'name': %5B'duplicated slug.'%5D%7D), 400%0A%0A    sender = get_data_or_404('sender', id)%0A%0A    return jsonify(**sender)%0A%0A%0A%0A
 @bp.rout

add sender.