<commit_before>"""
Handles routing and form processing for the PowerToken Flask app.\n
Created by <NAME>\n
Last modified by <NAME> on 3/15/2018
"""

from flask import flash, redirect, render_template, request, url_for
from flask_login import (
	current_user, login_user, logout_user, login_required
)
from werkzeug.urls import url_parse
from app import app, db, session
from app.forms import (
	AdminLoginForm, AdminRegistrationForm, UserLoginForm, UserWcLoginForm
)
from app.models import Admin, User, Log, Activity
from app.apis import login_to_wc, complete_fb_login

@app.route("/")
@app.route("/index")
@app.route("/home")
def user_home():
	if not "username" in session:
		return redirect(url_for("user_login"))
	else:
		return render_template("user_home.html", username=session["username"])

@app.route("/user_login", methods=["GET", "POST"])
def user_login():
	form = UserLoginForm()

	# POST: processes the PowerToken login form
	if form.validate_on_submit():
		username = form.username.data
		print(username)
		session["username"] = username
		session.modified = True
		session.permanent = True
		user = User.query.filter_by(username=username).first()

		# If the user has not been added to the database, adds the user to the
		# database
		if user is None:
			user = User(username=username)
			db.session.add(user)
			db.session.commit()

			# Redirects to the WC login
			return redirect(url_for("user_wc_login"))
		
		# If the user exists in the database, and the WEconnect and Fitbit info
		# is already filled out, skips the login process.
		if (not user.wc_id is None) and (not user.wc_token is None) and \
			(not user.fb_token is None):
			return redirect(url_for("user_home"))

		# If the user exists in the database, but the WEconnect (or Fitbit)
		# info isn't filled out, redirects to WEconnect login.
		return redirect(url_for("user_wc_login"))

	# GET: renders the PowerToken login page
	return render_template("user_login.html", form=form)

@app.route("/user_wc_login", methods=["GET", "POST"])
def user_wc_login():
	form = UserWcLoginForm()

	# If submitting the form (POST)
	if form.validate_on_submit():
		user = User.query.filter_by(username=session["username"]).first()
		if user is None:
			return redirect(url_for("user_login"), errors=["Invalid user"])

		# Gets WEconnect info and adds it to the database
		email = form.email.data
		password = form.password.data
		result = login_to_wc(email, password)
		if not result:
			err = ["Incorrect email or password"]
			return render_template("user_wc_login.html", form=form, errors=err)
		user.wc_id = result[0]
		user.wc_token = result[1]
		db.session.commit()

		# Redirects to the Fitbit login
		return redirect(url_for("user_fb_login"))

	# If not submitting the form (GET)
	return render_template("user_wc_login.html", form=form)

@app.route("/user_fb_login", methods=["GET", "POST"])
def user_fb_login():
	# If submitting the external form (POST)
	if request.method == "POST":
		if not "username" in session:
			return redirect(url_for("user_login"))
		print("Inside user_fb_login(), session['username'] =  {}".format(session["username"]))
		user = User.query.filter_by(username=session["username"]).first()
		if user is None:
			return redirect(url_for("user_login"), errors=["Invalid user"])
		
		# Gets FB info and adds it to db
		fb_token = complete_fb_login(request.data)
		print(fb_token)
		user.fb_token = fb_token
		db.session.commit()

		# Redirects to welcome page when setup is finished
		print("Will be redirecting to homepage...")
		return

	# If requesting the redirect page (GET)
	elif request.method == "GET":
		return render_template("user_fb_login.html")

@app.route("/admin")
@app.route("/admin/")
@app.route("/admin/index")
@app.route("/admin/home")
@login_required
def admin_home():
	users = User.query.order_by(User.registered_on).all()
	pt_users = []
	for user in users:
		last_log = Log(daily_progress=0.0, weekly_progress=0.0, step_count=0,
			user=user)
		logs = user.logs.all()
		if len(logs) > 0:
			last_log = logs[-1]
			print("Last log is {}".format(last_log))
		pt_users.append({"user": user, "last_log": last_log})
	return render_template("admin_home.html", pt_users=pt_users)

@app.route("/admin/login", methods=["GET", "POST"])
def admin_login():
	print("Called admin_login()")
	if current_user.is_authenticated:
		return redirect(url_for("admin_home"))
	form = AdminLoginForm()

	# POST: If a valid form was submitted
	if form.validate_on_submit():
		print("Submitted AdminLoginForm")
		admin = Admin.query.filter_by(username=form.username.data).first()
		if admin is None or not admin.check_password(form.password.data):
			flash("Invalid username or password")
			print("Invalid username or password")
			return redirect(url_for("admin_login"))
		login_user(admin, remember=form.remember_me.data)
		next_page = request.args.get("next")
		if not next_page or url_parse(next_page).netloc != '':
			next_page = url_for("admin_home")
		print("next_page = {}".format(str(next_page)))
		return redirect(next_page)

	# GET: Renders the admin login template
	return render_template("admin_login.html", form=form)

@app.route("/admin/logout")
def admin_logout():<commit_msg>I think I fixed the empty logs problem.<commit_after>"""
Handles routing and form processing for the PowerToken Flask app.\n
Created by <NAME>\n
Last modified by <NAME> on 3/15/2018
"""

from flask import flash, redirect, render_template, request, url_for
from flask_login import (
	current_user, login_user, logout_user, login_required
)
from werkzeug.urls import url_parse
from app import app, db, session
from app.forms import (
	AdminLoginForm, AdminRegistrationForm, UserLoginForm, UserWcLoginForm
)
from app.models import Admin, User, Log, Activity
from app.apis import login_to_wc, complete_fb_login

@app.route("/")
@app.route("/index")
@app.route("/home")
def user_home():
	if not "username" in session:
		return redirect(url_for("user_login"))
	else:
		return render_template("user_home.html", username=session["username"])

@app.route("/user_login", methods=["GET", "POST"])
def user_login():
	form = UserLoginForm()

	# POST: processes the PowerToken login form
	if form.validate_on_submit():
		username = form.username.data
		print(username)
		session["username"] = username
		session.modified = True
		session.permanent = True
		user = User.query.filter_by(username=username).first()

		# If the user has not been added to the database, adds the user to the
		# database
		if user is None:
			user = User(username=username)
			db.session.add(user)
			db.session.commit()

			# Redirects to the WC login
			return redirect(url_for("user_wc_login"))
		
		# If the user exists in the database, and the WEconnect and Fitbit info
		# is already filled out, skips the login process.
		if (not user.wc_id is None) and (not user.wc_token is None) and \
			(not user.fb_token is None):
			return redirect(url_for("user_home"))

		# If the user exists in the database, but the WEconnect (or Fitbit)
		# info isn't filled out, redirects to WEconnect login.
		return redirect(url_for("user_wc_login"))

	# GET: renders the PowerToken login page
	return render_template("user_login.html", form=form)

@app.route("/user_wc_login", methods=["GET", "POST"])
def user_wc_login():
	form = UserWcLoginForm()

	# If submitting the form (POST)
	if form.validate_on_submit():
		user = User.query.filter_by(username=session["username"]).first()
		if user is None:
			return redirect(url_for("user_login"), errors=["Invalid user"])

		# Gets WEconnect info and adds it to the database
		email = form.email.data
		password = form.password.data
		result = login_to_wc(email, password)
		if not result:
			err = ["Incorrect email or password"]
			return render_template("user_wc_login.html", form=form, errors=err)
		user.wc_id = result[0]
		user.wc_token = result[1]
		db.session.commit()

		# Redirects to the Fitbit login
		return redirect(url_for("user_fb_login"))

	# If not submitting the form (GET)
	return render_template("user_wc_login.html", form=form)

@app.route("/user_fb_login", methods=["GET", "POST"])
def user_fb_login():
	# If submitting the external form (POST)
	if request.method == "POST":
		if not "username" in session:
			return redirect(url_for("user_login"))
		print("Inside user_fb_login(), session['username'] =  {}".format(session["username"]))
		user = User.query.filter_by(username=session["username"]).first()
		if user is None:
			return redirect(url_for("user_login"), errors=["Invalid user"])
		
		# Gets FB info and adds it to db
		fb_token = complete_fb_login(request.data)
		print(fb_token)
		user.fb_token = fb_token
		db.session.commit()

		# Redirects to welcome page when setup is finished
		print("Will be redirecting to homepage...")
		return

	# If requesting the redirect page (GET)
	elif request.method == "GET":
		return render_template("user_fb_login.html")

@app.route("/admin")
@app.route("/admin/")
@app.route("/admin/index")
@app.route("/admin/home")
@login_required
def admin_home():
	users = User.query.order_by(User.registered_on).all()
	pt_users = []
	for user in users:
		logs = user.logs.all()
		last_log = logs[-1] if len(logs) > 0 else Log(daily_progress=0.0, weekly_progress=0.0, step_count=0, user=user)
		pt_users.append({"user": user, "last_log": last_log})
	return render_template("admin_home.html", pt_users=pt_users)

@app.route("/admin/login", methods=["GET", "POST"])
def admin_login():
	print("Called admin_login()")
	if current_user.is_authenticated:
		return redirect(url_for("admin_home"))
	form = AdminLoginForm()

	# POST: If a valid form was submitted
	if form.validate_on_submit():
		print("Submitted AdminLoginForm")
		admin = Admin.query.filter_by(username=form.username.data).first()
		if admin is None or not admin.check_password(form.password.data):
			flash("Invalid username or password")
			print("Invalid username or password")
			return redirect(url_for("admin_login"))
		login_user(admin, remember=form.remember_me.data)
		next_page = request.args.get("next")
		if not next_page or url_parse(next_page).netloc != '':
			next_page = url_for("admin_home")
		print("next_page = {}".format(str(next_page)))
		return redirect(next_page)

	# GET: Renders the admin login template
	return render_template("admin_login.html", form=form)

@app.route("/admin/logout")
def admin_logout():
{'fraction_non_alphanumeric': 0.12941290260179902, 'fraction_numerical': 0.0030950768933165683, 'mean_word_length': 5.053864168618267, 'pattern_counts': {'":': 8, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1011228', 'n_tokens_mistral': 3594, 'n_tokens_neox': 3324, 'n_words': 922}