<commit_before># -*- coding: utf-8 -*-

"""
byceps.blueprints.user_badge.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from collections import defaultdict

from ...database import db

from .models import Badge, BadgeAwarding


def create_badge(label, image_filename, *, description=None):
    """Introduce a new badge."""
    badge = Badge(label, image_filename, description=description)

    db.session.add(badge)
    db.session.commit()

    return badge


def find_badge(badge_id):
    """Return the badge with that id, or `None` if not found."""
    return Badge.query.get(badge_id)


def get_awardings_of_badge(badge_id):
    """Return the awardings (user and date) of this badge."""
    return BadgeAwarding.query \
        .filter_by(badge_id=badge_id) \
        .all()


def get_badges_for_user(user_id):
    """Return all badges that have been awarded to the user."""
    return Badge.query \
        .join(BadgeAwarding).filter_by(user_id=user_id) \
        .all()


def get_badges_for_users(user_ids):
    """Return all badges that have been awarded to the users, indexed
    by user ID.
    """
    awardings = BadgeAwarding.query \
        .filter(BadgeAwarding.user_id.in_(user_ids)) \
        .options(
            db.joinedload(BadgeAwarding.badge),
        ) \
        .all()

    badges_by_user_id = defaultdict(set)
    for awarding in awardings:
        badges_by_user_id[awarding.user_id].add(awarding.badge)

    return dict(badges_by_user_id)

def get_all_badges():
    """Return all badges."""
    return Badge.query.all()


def award_badge_to_user(badge, user):
    """Award the badge to the user."""
    badge.recipients.add(user)
    db.session.commit()<commit_msg>Award a badge to a user without using the association proxy<commit_after># -*- coding: utf-8 -*-

"""
byceps.blueprints.user_badge.service
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2016 <NAME>
:License: Modified BSD, see LICENSE for details.
"""

from collections import defaultdict

from ...database import db

from .models import Badge, BadgeAwarding


def create_badge(label, image_filename, *, description=None):
    """Introduce a new badge."""
    badge = Badge(label, image_filename, description=description)

    db.session.add(badge)
    db.session.commit()

    return badge


def find_badge(badge_id):
    """Return the badge with that id, or `None` if not found."""
    return Badge.query.get(badge_id)


def get_awardings_of_badge(badge_id):
    """Return the awardings (user and date) of this badge."""
    return BadgeAwarding.query \
        .filter_by(badge_id=badge_id) \
        .all()


def get_badges_for_user(user_id):
    """Return all badges that have been awarded to the user."""
    return Badge.query \
        .join(BadgeAwarding).filter_by(user_id=user_id) \
        .all()


def get_badges_for_users(user_ids):
    """Return all badges that have been awarded to the users, indexed
    by user ID.
    """
    awardings = BadgeAwarding.query \
        .filter(BadgeAwarding.user_id.in_(user_ids)) \
        .options(
            db.joinedload(BadgeAwarding.badge),
        ) \
        .all()

    badges_by_user_id = defaultdict(set)
    for awarding in awardings:
        badges_by_user_id[awarding.user_id].add(awarding.badge)

    return dict(badges_by_user_id)

def get_all_badges():
    """Return all badges."""
    return Badge.query.all()


def award_badge_to_user(badge_id, user_id):
    """Award the badge to the user."""
    awarding = BadgeAwarding(badge_id, user_id)

    db.session.add(awarding)
    db.session.commit()

    return awarding
{'fraction_non_alphanumeric': 0.1329700272479564, 'fraction_numerical': 0.004904632152588556, 'mean_word_length': 3.3239104829210837, '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': '658252', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1226, 'n_words': 290}