<commit_before>from flask import jsonify, request
from sqlalchemy import and_, or_

from server import app
from server.models import Account


@app.route('/studyspaces/user/search', methods=['GET'])
def get_nam():
    """
    Gets users that match search query
    """

    query = request.args.get('query')
    if not query:
        return jsonify({'error': 'Query argument not found.'}), 400

    if len(query) <= 1:
        return jsonify({'error': 'Query is too short. Minimum length is two characters.'}), 400

    first = None
    last = None
    if ' ' in query:
        split = query.split(' ')
        if len(split) >= 2:
            first = split[0]
            last = split[1]

    users = []
    if first and last:
        and_matches = Account.query.filter(and_(
            Account.first.like('{}%'.format(first)),
            Account.last.like('{}%'.format(last)))).all()
        users.extend(and_matches)

        if not users:
            or_matches = Account.query.filter(Account.last.like('{}%'.format(last))).all()
            users.extend(or_matches)
    else:
        starting_query = '{}%'.format(query)
        general_query = '%{}%'.format(query)

        starting_exact_matches = Account.query.filter(Account.first.like(query)).all()
        starting_exact_matches = sorted(starting_exact_matches, key=lambda x: x.last)

        starting_first_name_matches = Account.query.filter(Account.first.like(starting_query)).all()
        starting_first_name_matches = sorted(starting_first_name_matches, key=lambda x: x.last)

        starting_last_name_matches = Account.query.filter(Account.last.like(starting_query)).all()
        general_first_name_matches = Account.query.filter(Account.first.like(general_query)).all()
        general_last_name_matches = Account.query.filter(Account.last.like(general_query)).all()

        users.extend(starting_exact_matches)
        users.extend(starting_first_name_matches)
        users.extend(starting_last_name_matches)
        users.extend(general_first_name_matches)
        users.extend(general_last_name_matches)

    if not users:
        # If no users found by search first or last name, search the pennkey
        starting_pennkey_matches = Account.query.filter(Account.pennkey.like('{}%'.format(query))).all()
        general_pennkey_matches = Account.query.filter(Account.pennkey.like('%{}%'.format(query))).all()

        users.extend(starting_pennkey_matches)
        users.extend(general_pennkey_matches)
<commit_msg>sort by first letter, then by first name if only match on last name<commit_after>from flask import jsonify, request
from sqlalchemy import and_, or_

from server import app
from server.models import Account


@app.route('/studyspaces/user/search', methods=['GET'])
def get_nam():
    """
    Gets users that match search query
    """

    query = request.args.get('query')
    if not query:
        return jsonify({'error': 'Query argument not found.'}), 400

    if len(query) <= 1:
        return jsonify({'error': 'Query is too short. Minimum length is two characters.'}), 400

    first = None
    last = None
    if ' ' in query:
        split = query.split(' ')
        if len(split) >= 2:
            first = split[0]
            last = split[1]

    users = []
    if first and last:
        and_matches = Account.query.filter(and_(
            Account.first.like('{}%'.format(first)),
            Account.last.like('{}%'.format(last)))).all()
        users.extend(and_matches)

        if not users:
            first_letter_matches_and_last_name = Account.query.filter(and_(
                Account.first.like('{}%'.format(first[:1])),
                Account.last.like('{}%'.format(last)))).all()
            users.extend(first_letter_matches_and_last_name)

            last_name_matches = Account.query.filter(Account.last.like('{}%'.format(last))).all()
            last_name_matches = sorted(last_name_matches, key=lambda x: x.first)
            users.extend(last_name_matches)
    else:
        starting_query = '{}%'.format(query)
        general_query = '%{}%'.format(query)

        starting_exact_matches = Account.query.filter(Account.first.like(query)).all()
        starting_exact_matches = sorted(starting_exact_matches, key=lambda x: x.last)

        starting_first_name_matches = Account.query.filter(Account.first.like(starting_query)).all()
        starting_first_name_matches = sorted(starting_first_name_matches, key=lambda x: x.last)

        starting_last_name_matches = Account.query.filter(Account.last.like(starting_query)).all()
        general_first_name_matches = Account.query.filter(Account.first.like(general_query)).all()
        general_last_name_matches = Account.query.filter(Account.last.like(general_query)).all()

        users.extend(starting_exact_matches)
        users.extend(starting_first_name_matches)
        users.extend(starting_last_name_matches)
        users.extend(general_first_name_matches)
        users.extend(general_last_name_matches)

    if not users:
        # If no users found by search first or last name, search the pennkey
        starting_pennkey_matches = Account.query.filter(Account.pennkey.like('{}%'.format(query))).all()
        general_pennkey_matches = Account.query.filter(Account.pennkey.like('%{}%'.format(query))).all()

        users.extend(starting_pennkey_matches)
        users.extend(general_pennkey_matches)

{'fraction_non_alphanumeric': 0.1250463134494257, 'fraction_numerical': 0.003890329751759911, 'mean_word_length': 3.198289269051322, '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': '4911330', 'n_tokens_mistral': 1643, 'n_tokens_neox': 1563, 'n_words': 301}