<reponame>banan039pl/flask_leaderboard
from flask import request, url_for, render_template
from werkzeug.utils import redirect
import pandas as pd
from Models import User
from files_lib import SHA256
from forms import RegisterForm


def register_route(db):
    registration_status = request.args.get("registration_status", "")
    reg_form = RegisterForm()

    if request.method == 'POST':
        ### REGISTRATION
        if reg_form.validate_on_submit():
            user = User.query.filter_by(username=reg_form.username.data).first()
            print(user)
            if user is None:  # only when user is not registered then proceed
                print("HALOOO")
                u = User(username=reg_form.username.data, password=<PASSWORD>(reg_form.password.data),
                         faction=reg_form.faction.data)
                db.session.add(u)
                db.session.commit()
                # flash('Congratulations, you are now a registered user!')
                registration_status = f"Welcome {reg_form.username.data}, Please Login at HOME page"
                return redirect(url_for('register_page', registration_status=registration_status))
            else:
                registration_status = "USER NAME ALREADY USED"
                return redirect(url_for('register_page', registration_status=registration_status))
        else:
            registration_status = "ERROR VALIDATION"
            print("ANEH")
            return redirect(url_for('register_page', registration_status=registration_status))

    if request.method == 'GET':
        return render_template('register.html', reg_form=reg_form, registration_status=registration_status)

def Logging_in(login_form, login_user):
    print(f'Login requested for user {login_form.username.data}, remember_me={login_form.remember_me.data}')
    user = User.query.filter_by(username=login_form.username.data).first()
    if user is None:  # USER is not registered
        login_status = "User is not registered / Password does not match"
        return redirect(url_for('home_page', login_status=login_status))
    elif user.check_password(login_form.password.data):  # Password True
        print('True pass')
        login_status = ""
        login_user(user, remember=login_form.remember_me.data)
        return redirect(url_for('home_page', login_status=login_status))
    else:  # WRONG PASSWORD
        print('WRONG PASS')
        login_status = "User is not registered / Password does not match"
        return redirect(url_for('home_page', login_status=login_status))
    login_status = ""
    login_user(user, remember=login_form.remember_me.data)
    return redirect(url_for('home_page', login_status=login_status))


def get_all_subject_types(db):
    """Get all subject types and return as a list"""
    query = """
            SELECT DISTINCT subject_type 
            FROM Results
            """
    df = pd.read_sql(query, db.session.bind)
    return [col for col in df['subject_type']]

def get_number_of_tasks_per_subject_type(db,subject_types=None):
    """Return number of tasks for each subject type"""
    if subject_types is None:
        subject_types = get_all_subject_types(db)
    query = """
            SELECT  subject_type, COUNT(*) as tasks_n
            FROM Results
            GROUP BY subject_type
            """
    df = pd.read_sql(query, db.session.bind)
    return df


from flask import request, url_for, render_template
from werkzeug.utils import redirect
import pandas as pd
from Models import User
from files_lib import SHA256
from forms import RegisterForm


def register_route(db):
    registration_status = request.args.get("registration_status", "")
    reg_form = RegisterForm()

    if request.method == 'POST':
        ### REGISTRATION
        if reg_form.validate_on_submit():
            user = User.query.filter_by(username=reg_form.username.data).first()
            print(user)
            if user is None:  # only when user is not registered then proceed
                print("HALOOO")
                u = User(username=reg_form.username.data, password=<PASSWORD>(reg_form.password.data),
                         faction=reg_form.faction.data)
                db.session.add(u)
                db.session.commit()
                # flash('Congratulations, you are now a registered user!')
                registration_status = f"Welcome {reg_form.username.data}, Please Login at HOME page"
                return redirect(url_for('register_page', registration_status=registration_status))
            else:
                registration_status = "USER NAME ALREADY USED"
                return redirect(url_for('register_page', registration_status=registration_status))
        else:
            registration_status = "ERROR VALIDATION"
            print("ANEH")
            return redirect(url_for('register_page', registration_status=registration_status))

    if request.method == 'GET':
        return render_template('register.html', reg_form=reg_form, registration_status=registration_status)

def Logging_in(login_form, login_user):
    print(f'Login requested for user {login_form.username.data}, remember_me={login_form.remember_me.data}')
    user = User.query.filter_by(username=login_form.username.data).first()
    if user is None:  # USER is not registered
        login_status = "User is not registered / Password does not match"
        return redirect(url_for('home_page', login_status=login_status))
    elif user.check_password(login_form.password.data):  # Password True
        print('True pass')
        login_status = ""
        login_user(user, remember=login_form.remember_me.data)
        return redirect(url_for('home_page', login_status=login_status))
    else:  # WRONG PASSWORD
        print('WRONG PASS')
        login_status = "User is not registered / Password does not match"
        return redirect(url_for('home_page', login_status=login_status))
    login_status = ""
    login_user(user, remember=login_form.remember_me.data)
    return redirect(url_for('home_page', login_status=login_status))


def get_all_subject_types(db):
    """Get all subject types and return as a list"""
    query = """
            SELECT DISTINCT subject_type 
            FROM Results
            """
    df = pd.read_sql(query, db.session.bind)
    return [col for col in df['subject_type']]

def get_number_of_tasks_per_subject_type(db,subject_types=None):
    """Return number of tasks for each subject type"""
    if subject_types is None:
        subject_types = get_all_subject_types(db)
    query = """
            SELECT  subject_type, COUNT(*) as tasks_n
            FROM Results
            GROUP BY subject_type
            """
    df = pd.read_sql(query, db.session.bind)
    return df
### REGISTRATION # only when user is not registered then proceed # flash('Congratulations, you are now a registered user!') # USER is not registered # Password True # WRONG PASSWORD Get all subject types and return as a list SELECT DISTINCT subject_type              FROM Results Return number of tasks for each subject type SELECT  subject_type, COUNT(*) as tasks_n             FROM Results             GROUP BY subject_type