<commit_before>from framework import session, create_session
import framework.mongo as Database
import framework.status as status
import framework.flask as web
import framework.bcrypt as bcrypt

from modularodm.query.querydialect import DefaultQueryDialect as Q

import helper

#import website.settings as settings

from model import User

from decorator import decorator
import datetime

def get_current_username():
    return session.get('auth_user_username', None)

def get_current_user_id():
    return session.get('auth_user_id', None)

def get_current_user():
    uid = session.get("auth_user_id", None)
    return User.load(uid)
    # if uid:
    #     return User.load(uid)
    # else:
    #     return None

def get_current_node():
    from website.models import Node
    nid = session.get('auth_node_id')
    if nid:
        return Node.load(nid)

def get_api_key():
    # Hack: Avoid circular import
    from website.project.model import ApiKey
    api_key = session.get('auth_api_key')
    return ApiKey.load(api_key)

def get_user_or_node():
    uid = get_current_user()
    if uid:
        return uid
    return get_current_node()

def check_password(actualPassword, givenPassword):
    return bcrypt.check_password_hash(actualPassword, givenPassword)

def get_user(id=None, username=None, password=None, verification_key=None):
    # tag: database
    query = []
    if id:
        query.append(Q('_id', 'eq', id))
    if username:
        username = username.strip().lower()
        query.append(Q('username', 'eq', username))
    if password:
        password = password.strip()
        try:
            user = User.find_one(*query)
        except:
            user = None
        if user and not check_password(user.password, password):
            return False
        return user
    if verification_key:
        query.append(Q('verification_key', 'eq', verification_key))
    try:
        return User.find_one(*query)
    except:
        return None

def login(username, password):
    username = username.strip().lower()
    password = password.strip()

    if username and password:
        user = get_user(
            username=username,
            password=password
        )
        if user:
            if not user.is_registered:
                return 2
            elif not user.is_claimed:
                return False
            else:
                response = web.redirect('/dashboard')
                response = create_session(response, data={
                    'auth_user_username': user.username,
                    'auth_user_id': user._primary_key,
                    'auth_user_fullname': user.fullname,
                })
                return response
    return False

def logout():
    for i in ['auth_user_username', 'auth_user_id', 'auth_user_fullname']:
        # todo leave username so login page can persist probable id
        del session[i]
    return True

def add_unclaimed_user(email, fullname):
    email = email.strip().lower()
    fullname = fullname.strip()

    user = get_user(username=email)
    if user:
        return user
    else:
        user_based_on_email = User.find_one(
            Q('emails', 'eq', email)
        )
        if user_based_on_email:
            return user_based_on_email
        newUser = User(<commit_msg>add trailing slash<commit_after>from framework import session, create_session
import framework.mongo as Database
import framework.status as status
import framework.flask as web
import framework.bcrypt as bcrypt

from modularodm.query.querydialect import DefaultQueryDialect as Q

import helper

#import website.settings as settings

from model import User

from decorator import decorator
import datetime

def get_current_username():
    return session.get('auth_user_username', None)

def get_current_user_id():
    return session.get('auth_user_id', None)

def get_current_user():
    uid = session.get("auth_user_id", None)
    return User.load(uid)
    # if uid:
    #     return User.load(uid)
    # else:
    #     return None

def get_current_node():
    from website.models import Node
    nid = session.get('auth_node_id')
    if nid:
        return Node.load(nid)

def get_api_key():
    # Hack: Avoid circular import
    from website.project.model import ApiKey
    api_key = session.get('auth_api_key')
    return ApiKey.load(api_key)

def get_user_or_node():
    uid = get_current_user()
    if uid:
        return uid
    return get_current_node()

def check_password(actualPassword, givenPassword):
    return bcrypt.check_password_hash(actualPassword, givenPassword)

def get_user(id=None, username=None, password=None, verification_key=None):
    # tag: database
    query = []
    if id:
        query.append(Q('_id', 'eq', id))
    if username:
        username = username.strip().lower()
        query.append(Q('username', 'eq', username))
    if password:
        password = <PASSWORD>()
        try:
            user = User.find_one(*query)
        except:
            user = None
        if user and not check_password(user.password, password):
            return False
        return user
    if verification_key:
        query.append(Q('verification_key', 'eq', verification_key))
    try:
        return User.find_one(*query)
    except:
        return None

def login(username, password):
    username = username.strip().lower()
    password = password.<PASSWORD>()

    if username and password:
        user = get_user(
            username=username,
            password=password
        )
        if user:
            if not user.is_registered:
                return 2
            elif not user.is_claimed:
                return False
            else:
                response = web.redirect('/dashboard/')
                response = create_session(response, data={
                    'auth_user_username': user.username,
                    'auth_user_id': user._primary_key,
                    'auth_user_fullname': user.fullname,
                })
                return response
    return False

def logout():
    for i in ['auth_user_username', 'auth_user_id', 'auth_user_fullname']:
        # todo leave username so login page can persist probable id
        del session[i]
    return True

def add_unclaimed_user(email, fullname):
    email = email.strip().lower()
    fullname = fullname.strip()

    user = get_user(username=email)
    if user:
        return user
    else:
        user_based_on_email = User.find_one(
            Q('emails', 'eq', email)
        )
        if user_based_on_email:
            return user_based_on_email
        newUser = User(
{'fraction_non_alphanumeric': 0.08866696928430927, 'fraction_numerical': 0.0003026176426085641, 'mean_word_length': 2.43733749349974, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4861698', 'n_tokens_mistral': 1898, 'n_tokens_neox': 1867, 'n_words': 501}