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)

[((8, 7, 8, 34), 'flask.Blueprint', 'Blueprint', ({(8, 17, 8, 23): '"""main"""', (8, 25, 8, 33): '__name__'}, {}), "('main', __name__)", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((14, 13, 14, 45), 'flask.request.cookies.get', 'request.cookies.get', ({(14, 33, 14, 44): '"""isvisited"""'}, {}), "('isvisited')", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((24, 19, 24, 42), 'datetime.datetime.now', 'datetime.datetime.now', ({}, {}), '()', False, 'import datetime\n'), ((38, 11, 38, 70), 'flask.render_template', 'render_template', (), '', False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((42, 12, 42, 37), 'flask.request.form.get', 'request.form.get', ({(42, 29, 42, 36): '"""email"""'}, {}), "('email')", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((43, 11, 43, 35), 'flask.request.form.get', 'request.form.get', ({(43, 28, 43, 34): '"""name"""'}, {}), "('name')", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((44, 15, 44, 43), 'flask.request.form.get', 'request.form.get', ({(44, 32, 44, 42): '"""password"""'}, {}), "('password')", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((64, 11, 64, 66), 'flask.render_template', 'render_template', (), '', False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((49, 8, 49, 45), 'flask.flash', 'flash', ({(49, 14, 49, 44): '"""Email address already exists"""'}, {}), "('Email address already exists')", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((59, 20, 59, 41), 'flask.url_for', 'url_for', ({(59, 28, 59, 40): '"""auth.login"""'}, {}), "('auth.login')", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((19, 29, 19, 58), 'flask.render_template', 'render_template', ({(19, 45, 19, 57): '"""index.html"""'}, {}), "('index.html')", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((50, 24, 50, 46), 'flask.url_for', 'url_for', ({(50, 32, 50, 45): '"""auth.signup"""'}, {}), "('auth.signup')", False, 'from flask import Blueprint, render_template, redirect, url_for, request, flash, make_response\n'), ((53, 53, 53, 102), 'werkzeug.security.generate_password_hash', 'generate_password_hash', (), '', False, 'from werkzeug.security import generate_password_hash\n')]