
import click

from byceps.services.user_badge import service as badge_service
from byceps.util.system import get_config_filename_from_env_or_exit

from bootstrap.validators import validate_user_screen_name
from bootstrap.util import app_context


@click.command()
@click.argument('badge_slug')
@click.argument('user', callback=validate_user_screen_name)
def execute(badge_slug, user):
    badge = badge_service.find_badge_by_slug(badge_slug)

    if badge is None:
        raise click.BadParameter('Unknown badge slug "{}".'.format(badge_slug))

    click.echo('Awarding badge "{}" to user "{}" ... '
        .format(badge.label, user.screen_name), nl=False)

    badge_service.award_badge_to_user(badge.id, user.id)

    click.secho('done.', fg='green')


if __name__ == '__main__':
    config_filename = get_config_filename_from_env_or_exit()
    with app_context(config_filename):
        execute()


import click

from byceps.database import db
from byceps.services.user_badge.models.badge import Badge, BadgeID
from byceps.services.user_badge import service as badge_service
from byceps.util.system import get_config_filename_from_env_or_exit

from bootstrap.validators import validate_user_screen_name
from bootstrap.util import app_context


@click.command()
@click.argument('badge_slug')
@click.argument('user', callback=validate_user_screen_name)
def execute(badge_slug, user):
    badge_id = find_badge_id_for_badge_slug(badge_slug)

    click.echo('Awarding badge "{}" to user "{}" ... '
        .format(badge_slug, user.screen_name), nl=False)

    badge_service.award_badge_to_user(badge_id, user.id)

    click.secho('done.', fg='green')


def find_badge_id_for_badge_slug(slug: str) -> BadgeID:
    """Finde the badge with that slug and return its ID, or raise an
    error if not found.
    """
    badge_id = db.session \
        .query(Badge.id) \
        .filter_by(slug=slug) \
        .scalar()

    if badge_id is None:
        raise click.BadParameter('Unknown badge slug "{}".'.format(slug))

    return badge_id


if __name__ == '__main__':
    config_filename = get_config_filename_from_env_or_exit()
    with app_context(config_filename):
        execute()

Change script to avoid creation of badge URLs to make it work outside of a *party-specific* app context
Change script to avoid creation of badge URLs to make it work outside of a *party-specific* app context

  
  import click
  
+ from byceps.database import db
+ from byceps.services.user_badge.models.badge import Badge, BadgeID
  from byceps.services.user_badge import service as badge_service
  from byceps.util.system import get_config_filename_from_env_or_exit
  
  from bootstrap.validators import validate_user_screen_name
  from bootstrap.util import app_context
  
  
  @click.command()
  @click.argument('badge_slug')
  @click.argument('user', callback=validate_user_screen_name)
  def execute(badge_slug, user):
+     badge_id = find_badge_id_for_badge_slug(badge_slug)
-     badge = badge_service.find_badge_by_slug(badge_slug)
- 
-     if badge is None:
-         raise click.BadParameter('Unknown badge slug "{}".'.format(badge_slug))
  
      click.echo('Awarding badge "{}" to user "{}" ... '
-         .format(badge.label, user.screen_name), nl=False)
+         .format(badge_slug, user.screen_name), nl=False)
  
-     badge_service.award_badge_to_user(badge.id, user.id)
+     badge_service.award_badge_to_user(badge_id, user.id)
  
      click.secho('done.', fg='green')
+ 
+ 
+ def find_badge_id_for_badge_slug(slug: str) -> BadgeID:
+     """Finde the badge with that slug and return its ID, or raise an
+     error if not found.
+     """
+     badge_id = db.session \
+         .query(Badge.id) \
+         .filter_by(slug=slug) \
+         .scalar()
+ 
+     if badge_id is None:
+         raise click.BadParameter('Unknown badge slug "{}".'.format(slug))
+ 
+     return badge_id
  
  
  if __name__ == '__main__':
      config_filename = get_config_filename_from_env_or_exit()
      with app_context(config_filename):
          execute()
  
Change script to avoid creation of badge URLs to make it work outside of a *party-specific* app context
## Code Before:

import click

from byceps.services.user_badge import service as badge_service
from byceps.util.system import get_config_filename_from_env_or_exit

from bootstrap.validators import validate_user_screen_name
from bootstrap.util import app_context


@click.command()
@click.argument('badge_slug')
@click.argument('user', callback=validate_user_screen_name)
def execute(badge_slug, user):
    badge = badge_service.find_badge_by_slug(badge_slug)

    if badge is None:
        raise click.BadParameter('Unknown badge slug "{}".'.format(badge_slug))

    click.echo('Awarding badge "{}" to user "{}" ... '
        .format(badge.label, user.screen_name), nl=False)

    badge_service.award_badge_to_user(badge.id, user.id)

    click.secho('done.', fg='green')


if __name__ == '__main__':
    config_filename = get_config_filename_from_env_or_exit()
    with app_context(config_filename):
        execute()

## Instruction:
Change script to avoid creation of badge URLs to make it work outside of a *party-specific* app context
## Code After:

import click

from byceps.database import db
from byceps.services.user_badge.models.badge import Badge, BadgeID
from byceps.services.user_badge import service as badge_service
from byceps.util.system import get_config_filename_from_env_or_exit

from bootstrap.validators import validate_user_screen_name
from bootstrap.util import app_context


@click.command()
@click.argument('badge_slug')
@click.argument('user', callback=validate_user_screen_name)
def execute(badge_slug, user):
    badge_id = find_badge_id_for_badge_slug(badge_slug)

    click.echo('Awarding badge "{}" to user "{}" ... '
        .format(badge_slug, user.screen_name), nl=False)

    badge_service.award_badge_to_user(badge_id, user.id)

    click.secho('done.', fg='green')


def find_badge_id_for_badge_slug(slug: str) -> BadgeID:
    """Finde the badge with that slug and return its ID, or raise an
    error if not found.
    """
    badge_id = db.session \
        .query(Badge.id) \
        .filter_by(slug=slug) \
        .scalar()

    if badge_id is None:
        raise click.BadParameter('Unknown badge slug "{}".'.format(slug))

    return badge_id


if __name__ == '__main__':
    config_filename = get_config_filename_from_env_or_exit()
    with app_context(config_filename):
        execute()
