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

from flask import render_template, jsonify, request, flash, session, url_for, redirect, g
from flask.ext.login import LoginManager, login_user, logout_user, login_required, current_user, AnonymousUserMixin

from web import app, db
from web.models import User, Station
from forms import SigninForm, ProfileForm, StationForm

login_manager = LoginManager()
login_manager.init_app(app)

#
# Management of the user's session.
#
@app.before_request
def before_request():
    g.user = current_user
    if g.user.is_authenticated():
        pass
        #g.user.last_seen = datetime.utcnow()
        #db.session.add(g.user)
        #db.session.commit()

@app.errorhandler(403)
def authentication_failed(e):
    flash('Authentication failed.', 'danger')
    return redirect(url_for('login'))

@app.errorhandler(401)
def authentication_failed(e):
    flash('Authentication required.', 'info')
    return redirect(url_for('login'))

@login_manager.user_loader
def load_user(email):
    # Return an instance of the User model
    return User.query.filter(User.email == email).first()

def redirect_url(default='map_view'):
    return request.args.get('next') or \
            request.referrer or \
            url_for(default)


#
# Views.
#
@app.route('/login/', methods=['GET', 'POST'])
def login():
    """
    Log in view.
    """
    g.user = AnonymousUserMixin()
    form = SigninForm()

    if form.validate_on_submit():
        user = User.query.filter(User.email == form.email.data).first()
        login_user(user)
        g.user = user
        flash("Logged in successfully.", 'success')
        return redirect(url_for('profile'))
    return render_template('login.html', form=form)

@app.route('/logout/')
@login_required
def logout():
    """
    Log out view. Removes the user information from the session.
    """
    logout_user()
    flash("Logged out successfully.", 'success')
    return redirect(url_for('map_view'))

@app.route('/', methods=['GET'])
@app.route('/map/', methods=['GET'])
def map_view():
    """
    Main view which displays all public stations.
    """
    return render_template('map.html')

@app.route('/profile/', methods=['GET', 'POST'])
@login_required
def profile():
    """
    Edit the profile of the user.
    """
    user = User.query.filter(User.email == g.user.email).first()
    form = ProfileForm()

    if request.method == 'POST':
        if form.validate():
            form.populate_obj(user)
            if form.password.data != "":
                user.set_password(form.password.data)
            db.session.commit()
            flash('User "' + user.firstname + '" successfully updated', 'success')
            return redirect(url_for('profile'))
        else:
            return render_template('profile.html', form=form)

    if request.method == 'GET':
        form = ProfileForm(obj=user)
        return render_template('profile.html', user=user, form=form)

@app.route('/edit_station/<int:station_id>', methods=['GET', 'POST'])
@login_required
def edit_station(station_id=None):
    """
    """
    user = User.query.filter(User.email == g.user.email).first()
    form = StationForm()

    station = None
    for station in user.stations:
        if station.id == station_id:
            station = station
            break

    if request.method == 'POST':
        if form.validate():
            form.populate_obj(station)
            db.session.commit()
            flash('Station "' + station.name + '" successfully updated', 'success')
        else:
            flash('Problem', 'danger')
        return redirect(redirect_url())

    if request.method == 'GET':
        form = StationForm(obj=station)
        return render_template('edit_station.html', user=user, station=station, form=form)
@@ -3082,24 +3082,44 @@
     %22%22%22%0A
+    Edit a station.%0A
     %22%22%22%0A
     user
@@ -3102,32 +3102,32 @@
 tation.%0A    %22%22%22%0A
-
     user = User.
@@ -3338,16 +3338,115 @@
    break
+%0A    else:%0A        flash('This station does not exist.', 'danger')%0A        redirect(redirect_url())
 %0A%0A    if
@@ -3695,16 +3695,31 @@
 'Problem
+ with the form.
 ', 'dang
@@ -3828,32 +3828,32 @@
 rm(obj=station)%0A
-
         return r
@@ -3914,20 +3914,588 @@
 =station, form=form)
+%0A%0A@app.route('/delete_station/%3Cint:station_id%3E', methods=%5B'GET'%5D)%0A@login_required%0Adef delete_station(station_id=None):%0A    %22%22%22%0A    Delete a station.%0A    %22%22%22%0A    user = User.query.filter(User.email == g.user.email).first()%0A%0A    for station in user.stations:%0A        if station.id == station_id:%0A            db.session.delete(station)%0A            db.session.commit()%0A            flash('Station %22' + station.name + '%22 successfully deleted', 'success')%0A            break%0A    else:%0A        flash('This station does not exist.', 'danger')%0A    return redirect(redirect_url())

Delete a station.