""" shRUnk - Rutgers University URL Shortener

Sets up a Flask application for shRUnk.
"""
import logging

from wtforms import Form, TextField, validators
from flask import Flask, render_template, request, redirect, g
from flask_login import LoginManager, login_required, current_user, logout_user
from flask_auth import Auth

from shrunk.client import ShrunkClient
from shrunk.user import User, get_user, RULoginForm


# Create application
app = Flask(__name__)

# Import settings in config.py
app.config.from_pyfile("config.py", silent=True)
app.secret_key = app.config['SECRET_KEY']

# Initialize logging
format = "%(levelname)s %(asctime)s: %(message)s [in %(pathname)s:%(lineno)d]"
handler = logging.FileHandler("shrunk.log")
handler.setLevel(logging.INFO)
app.logger.addHandler(handler)
handler.setFormatter(logging.Formatter(format))

# Initialize login manager
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = '/login'


### Helper Functions ###
def formattime(datetime):
    """Utility function for formatting datetimes.

    This formats datetimes to look like "09/24/2015 - 20:14:00".
    """
    return datetime.strftime("%m/%d/%Y - %H:%M:%S")

# Allows us to use the function in our templates
app.jinja_env.globals.update(formattime=formattime)


@login_manager.user_loader
def load_user(userid):
    """Loads user object for login.

    :Parameters:
      - `userid`: An id for the user (typically a NetID).
    """
    return User(userid)


def render_login(**kwargs):
    """Renders the login template.

    Takes a WTForm in the keyword arguments.
    """
    return render_template('login.html', **kwargs)


def login_success(user):
    """Function executed on successful login.

    Redirects the user to the homepage.

    :Parameters:
      - `user`: The user that has logged in.
    """
    return redirect('/')


def get_dbclient():
    """Gets a reference to a ShrunkClient for database operations."""
    if not hasattr(g, "client"):
        g.client = ShrunkClient(app.config["DB_HOST"], app.config["DB_PORT"])

    return g.client


### Views ###
@app.route("/")
def render_index(**kwargs):
    """Renders the homepage."""
    client = get_dbclient()

    try:
        links = client.get_urls(current_user.netid)
        app.logger.info("Rendering index for user {}".format(current_user))
    except AttributeError:
        links = []
        app.logger.info("Rendering index for anonymous user.")

    return render_template("index.html", links=links, **kwargs)


@app.route("/login", methods=['GET', 'POST'])
def login():
    """Handles authentication."""
    a = Auth(app.config['AUTH'], get_user)
    return a.login(request, RULoginForm, render_login, login_success)


@app.route("/logout")
@login_required
def logout():
    """Handles logging out."""
    logout_user()
    return redirect('/')


@app.route("/add", methods=["GET", "POST"])
@login_required
def add_link():
    """Adds a new link for the current user."""
    #TODO
    pass


@app.route("/delete", methods=["GET", "POST"])
@login_required
def delete_link():
    """Deletes a link."""
    # TODO
    client = get_dbclient()

    # TODO Handle the response intelligently, or put that logic somewhere else
    if request.method == "POST":
        client.delete_url(request.form["short_url"])
    return render_index(deleted_url=request.form["short_url"])


@app.route("/edit", methods=["GET", "POST"])
@login_required
def edit_link():
    """Edits a link."""
    # TODO
    pass

@@ -104,56 +104,8 @@
 ng%0A%0A
-from wtforms import Form, TextField, validators%0A
 from
@@ -163,16 +163,16 @@
 rect, g%0A
+
 from fla
@@ -363,16 +363,50 @@
 ginForm%0A
+from shrunk.forms import LinkForm%0A
 %0A%0A# Crea
@@ -745,39 +745,8 @@
 FO)%0A
-app.logger.addHandler(handler)%0A
 hand
@@ -788,16 +788,47 @@
 format))
+%0Aapp.logger.addHandler(handler)
 %0A%0A# Init

Update mistakes in app.py