import flask
import os.path
import datetime
from ... import auth
from ... import blog
from ... import configmanager
from ... import settings
from .. import webhelpers

TEMPLATE_PATH = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../../templates"))

blueprint = flask.Blueprint("user", __name__, template_folder = TEMPLATE_PATH)

@blueprint.route("/")
def showPosts():
	posts = blog.getPosts()
	return flask.render_template("posts.html", 
		posts = posts, **webhelpers.getPostsParameters())

@blueprint.route("/post/<int:postId>")
def showPost(postId):
	post = blog.getPostById(postId)
	if post == None:
		flask.abort(404)
	else:
		return flask.render_template("posts.html", 
			posts = [post], **webhelpers.getPostsParameters())

@blueprint.route("/tag/<tag>")
def showPostsWithTag(tag):
	posts = blog.getPostsWithTag(tag)
	return flask.render_template("posts.html", 
		posts = posts, **webhelpers.getPostsParameters())

@blueprint.route("/login", methods=["GET", "POST"])
def login():
	if flask.request.method == "GET":
		if webhelpers.checkForSession():
			return flask.redirect("/manage")	
		else:
			return flask.render_template("login.html")

	elif flask.request.method == "POST":
		if ("username" not in flask.request.form 
			or "password" not in flask.request.form):
			flask.flash("A username and password must be provided.")
			return flask.render_template("login.html")
		elif auth.validateUser(flask.request.form["username"], 
								flask.request.form["password"]):
			donePage = webhelpers.canRecoverFromRedirect()
			donePage = donePage if donePage is not None else "/manage"
			sessionId, expires = auth.createSession(flask.request.form["username"])
			flask.session["uid"] = sessionId
			flask.session.permanent = True
			flask.session.permanent_session_lifetime = datetime.datetime.now() - expires
			return flask.redirect(donePage)
		else:
			flask.flash("Invalid username or password.")
			return flask.render_template("login.html")

@blueprint.route("/logout", methods=["POST"])
def logout():
	if webhelpers.checkForSession():
		if "uid" in flask.session:
			sessionId = flask.session["uid"]
			auth.invalidateSession(sessionId)
			flask.session.clear()
	
	return flask.redirect("/login")

@@ -406,36 +406,104 @@
 ()%0A%09
-return flask
+templatePath = os.path.join(TEMPLATE_PATH, %22posts.html%22)%0A%09return webhelpers
 .render
-_
+Posts(
 template
 (%22po
@@ -494,37 +494,28 @@
 sts(template
-(%22posts.html%22
+Path
 , %0A%09%09posts =
@@ -702,36 +702,105 @@
 :%0A%09%09
-return flask
+templatePath = os.path.join(TEMPLATE_PATH, %22posts.html%22)%0A%09%09return webhelpers
 .render
-_
+Posts(
 template
 (%22po
@@ -791,37 +791,28 @@
 sts(template
-(%22posts.html%22
+Path
 , %0A%09%09%09posts 
@@ -817,14 +817,13 @@
 s = 
-%5B
 post
-%5D
+s
 , **
@@ -954,36 +954,104 @@
 g)%0A%09
-return flask
+templatePath = os.path.join(TEMPLATE_PATH, %22posts.html%22)%0A%09return webhelpers
 .render
-_
+Posts(
 template
 (%22po
@@ -1050,21 +1050,12 @@
 late
-(%22posts.html%22
+Path
 , %0A%09

Use renderPosts in user controller