<commit_before>__author__ = 'collinpetty'

from api import app, common

from api.annotations import *
import bcrypt


def get_user(username=None):
    db = common.get_conn()
    if username is not None:
        return db.users.find_one({'username': username})
    if 'uid' in session:
        return db.users.find_one({'uid': session['uid']})
    return None


def create_user(username, email, pwhash):
    db = common.get_conn()
    uid = common.token()
    try:
        db.users.insert({'uid': uid,
                         'username': username,
                         'email': email,
                         'pwhash': pwhash})
    except Exception as e:
        print("Error creating the user account.")
        return None
    return uid


def get_all_users():
    db = common.get_conn()
    return [{'uid': u['uid'],
             'username': u['username'],
             'email': u['email']} for u in db.users.find({})]


@app.route('/api/user/register', methods=['POST'])
@return_json
def register_user():
    """Register a new team.

    Checks that an email address, team name, adviser name, affiliation, and password were sent from the browser.
    If any of these are missing a status:0 is returned with a message saying that all fields must be provided.
    """
    email = request.form.get('email')
    username = request.form.get('username')
    pwd = request.form.get('pass')
    teamname = request.form.get('teamname')
    confirm = request.form.get('confirm', False)

    if '' in {email, username, pwd, teamname}:
        return 0, None, "Please fill out all required fields."
    if get_user(username) is not None:
        return 0, None, "A user with that name has already registered."
    uid = create_user(username, email, bcrypt.hashpw(str(pwd), bcrypt.gensalt(8)))
    if uid is None:
        return 0, None, "There was an error creating your account."
    return 1, None, "User '%s' created successfully!" % username


@app.route('/api/updatepass', methods=['POST'])
@return_json
@require_login
def update_password(uid, request):
    """Update account password.

    Gets the new password and the password entered into the 'confirm password' box and verifies that 1) The new pw is
    not empty and 2) the new pw and the conf pw are the same. We salt/hash the password and update the team object
    in mongo then return a status:1 with a success message.
    """

    db = common.get_conn()
    pwd = request.form.get('pwd', '')<commit_msg>added the stateful logic to user creation to handle joining/creating a team<commit_after>__author__ = 'collinpetty'

from api import app, common, team

from api.annotations import *
import bcrypt


def get_user(username=None):
    db = common.get_conn()
    if username is not None:
        return db.users.find_one({'username': username})
    if 'uid' in session:
        return db.users.find_one({'uid': session['uid']})
    return None


def create_user(username, email, pwhash):
    db = common.get_conn()
    uid = common.token()
    try:
        db.users.insert({'uid': uid,
                         'username': username,
                         'email': email,
                         'pwhash': pwhash})
    except Exception as e:
        print("Error creating the user account.")
        return None
    return uid


def get_all_users():
    db = common.get_conn()
    return [{'uid': u['uid'],
             'username': u['username'],
             'email': u['email']} for u in db.users.find({})]


@app.route('/api/user/create', methods=['POST'])
@return_json
def register_user():
    """Register a new team.

    Checks that an email address, team name, adviser name, affiliation, and password were sent from the browser.
    If any of these are missing a status:0 is returned with a message saying that all fields must be provided.
    """
    email = request.form.get('email')
    username = request.form.get('username')
    pwd = request.form.get('pass')
    teamname = request.form.get('teamname')
    confirm = request.form.get('confirm', False)

    db = common.get_conn()
    if None in {email, username, pwd, teamname}:
        return 0, None, "Please fill out all required fields."
    if get_user(username) is not None:
        return 0, None, "A user with that name has already registered."
    teamacct = team.get_team(teamname=teamname)
    if confirm and teamacct is not None:
        useracct = create_user(username, email, bcrypt.hashpw(str(pwd), bcrypt.gensalt(8)))
        if useracct is None:
            return 0, None, "There was an error during registration."
        db.users.update({'uid': useracct['uid']}, {'$set': {'tid': teamacct['tid']}})
        return 1, None, "User '%s' registered successfully!" % username
    elif not confirm and teamacct is not None:
        return 2, None, "The team name you have entered exists, would like to join it?"
    elif teamacct is None:
        return 3, None, "The specified team does not exist, would you like to create it?"


@app.route('/api/updatepass', methods=['POST'])
@return_json
@require_login
def update_password(uid, request):
    """Update account password.

    Gets the new password and the password entered into the 'confirm password' box and verifies that 1) The new pw is
    not empty and 2) the new pw and the conf pw are the same. We salt/hash the password and update the team object
    in mongo then return a status:1 with a success message.
    """

    db = common.get_conn()
    pwd = request.form.get('pwd', '')
{'fraction_non_alphanumeric': 0.11617727521429874, 'fraction_numerical': 0.003647638154295094, 'mean_word_length': 2.903202846975089, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6293829', 'n_tokens_mistral': 1619, 'n_tokens_neox': 1578, 'n_words': 592}