<commit_before>from datetime import datetime

from ascoderu_webapp import db
from ascoderu_webapp.models import Email
from ascoderu_webapp.models import User


def user_exists(name_or_email):
    user = User.query.filter(User.name.ilike(name_or_email) |
                             User.email.ilike(name_or_email)).first()
    return user is not None


def outbox_emails_for(user):
    return Email.query.filter(Email.date.is_(None) &
                              (Email.sender.is_(user.email) |
                               Email.sender.is_(user.name))).all()


def sent_emails_for(user):
    return Email.query.filter(Email.date.isnot(None) &
                              (Email.sender.is_(user.email) |
                               Email.sender.is_(user.name))).all()


def inbox_emails_for(user):
    return [mail for mail in Email.query.all()
            if any(to == user.email or to == user.name for to in mail.to)]


def new_email_for(user, to, subject, body):
    is_to_local_user = user_exists(to)
    email_date = datetime.now() if is_to_local_user else None

    db.session.add(Email(
        date=email_date,
        to=[to],
        sender=user.email or user.name,
        subject=subject,
        body=body))
    db.session.commit()

    return is_to_local_user<commit_msg>Make sent and outbox queries case insensitive<commit_after>from datetime import datetime

from ascoderu_webapp import db
from ascoderu_webapp.models import Email
from ascoderu_webapp.models import User


def _query_emails_by(user):
    def query_user_email():
        return Email.sender.ilike(user.email)

    def query_user_name():
        return Email.sender.ilike(user.name)

    if user.email and user.name:
        return query_user_email() | query_user_name()
    elif user.email:
        return query_user_email()
    elif user.name:
        return query_user_name()
    raise ValueError('one of user.email or user.name must be set')


def user_exists(name_or_email):
    user = User.query.filter(User.name.ilike(name_or_email) |
                             User.email.ilike(name_or_email)).first()
    return user is not None


def outbox_emails_for(user):
    query = Email.date.is_(None) & _query_emails_by(user)
    return Email.query.filter(query).all()


def sent_emails_for(user):
    query = Email.date.isnot(None) & _query_emails_by(user)
    return Email.query.filter(query).all()


def inbox_emails_for(user):
    return [mail for mail in Email.query.all()
            if any(to == user.email or to == user.name for to in mail.to)]


def new_email_for(user, to, subject, body):
    is_to_local_user = user_exists(to)
    email_date = datetime.now() if is_to_local_user else None

    db.session.add(Email(
        date=email_date,
        to=[to],
        sender=user.email or user.name,
        subject=subject,
        body=body))
    db.session.commit()

    return is_to_local_user
{'fraction_non_alphanumeric': 0.1003448275862069, 'fraction_numerical': 0.0, 'mean_word_length': 2.748062015503876, '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': '1530106', 'n_tokens_mistral': 940, 'n_tokens_neox': 918, 'n_words': 201}