from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response
from werkzeug.security import generate_password_hash
from flask_login import login_required, current_user
from . import db
import datetime
from .models import Visitor, User

main = Blueprint('main', __name__)


@main.route('/')
def index():
    #control visitor with cookies
    cookie = request.cookies.get('isvisited')
    if cookie:
        #visit=True
        pass
    else:
        resp = make_response(render_template('index.html'))
        resp.set_cookie('isvisited', 'yess')
        return resp

    visitor_ip = request.remote_addr
    visited_time = datetime.datetime.now()
    visitors = Visitor.query.all()
    visited = Visitor.query.filter_by(ip=visitor_ip).first()
    visit = False
    if visited:
        difference = abs((visited_time - visited.last_visit).seconds)
        if difference > 60:
            visit = True
            visited.last_visit = visited_time
            db.session.commit()
    else:
        new_visitor = Visitor(ip=visitor_ip,last_visit=visited_time)
        db.session.add(new_visitor)
        db.session.commit()
    return render_template('index.html',visitors=visitors,visit=visit)

@main.route('/', methods=['POST'])
def index_post():
    email = request.form.get('email')
    name = request.form.get('name')
    password = request.form.get('password')

    user = User.query.filter_by(email=email).first() # if this returns a user, then the email already exists in database

    if user:  # if a user is found, we want to redirect back to signup page so user can try again
        flash('Email address already exists')
        return redirect(url_for('auth.signup'))

    # create a new user with the form data. Hash the password so the plaintext version isn't saved.
    new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256'))

    # add the new user to the database
    db.session.add(new_user)
    db.session.commit()

    return redirect(url_for('auth.login'))

@main.route('/profile')
@login_required
def profile():
    return render_template('profile.html', name=current_user.name)

[((276, 303), 'flask.Blueprint', 'Blueprint', (['"""main"""', '__name__'], {}), "('main', __name__)\n", (285, 303), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((383, 415), 'flask.request.cookies.get', 'request.cookies.get', (['"""isvisited"""'], {}), "('isvisited')\n", (402, 415), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((656, 679), 'datetime.datetime.now', 'datetime.datetime.now', ([], {}), '()\n', (677, 679), False, 'import datetime\n'), ((1165, 1226), 'flask.render_template', 'render_template', (['"""index.html"""'], {'visitors': 'visitors', 'visit': 'visit'}), "('index.html', visitors=visitors, visit=visit)\n", (1180, 1226), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((1291, 1316), 'flask.request.form.get', 'request.form.get', (['"""email"""'], {}), "('email')\n", (1307, 1316), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((1328, 1352), 'flask.request.form.get', 'request.form.get', (['"""name"""'], {}), "('name')\n", (1344, 1352), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((1368, 1396), 'flask.request.form.get', 'request.form.get', (['"""password"""'], {}), "('password')\n", (1384, 1396), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((2121, 2176), 'flask.render_template', 'render_template', (['"""profile.html"""'], {'name': 'current_user.name'}), "('profile.html', name=current_user.name)\n", (2136, 2176), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((1626, 1663), 'flask.flash', 'flash', (['"""Email address already exists"""'], {}), "('Email address already exists')\n", (1631, 1663), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((2031, 2052), 'flask.url_for', 'url_for', (['"""auth.login"""'], {}), "('auth.login')\n", (2038, 2052), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((503, 532), 'flask.render_template', 'render_template', (['"""index.html"""'], {}), "('index.html')\n", (518, 532), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((1688, 1710), 'flask.url_for', 'url_for', (['"""auth.signup"""'], {}), "('auth.signup')\n", (1695, 1710), False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((1866, 1915), 'werkzeug.security.generate_password_hash', 'generate_password_hash', (['password'], {'method': '"""sha256"""'}), "(password, method='sha256')\n", (1888, 1915), False, 'from werkzeug.security import generate_password_hash\n')]