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

"""
byceps.blueprints.shop_admin.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2014 <NAME>
"""

from datetime import datetime
from flask import request

from ...database import db
from ...util.framework import create_blueprint, flash_success
from ...util.templating import templated
from ...util.views import redirect_to

from ..authorization.decorators import permission_required
from ..authorization.registry import permission_registry
from ..shop.models import Article, Order, OrderItem, PaymentState
from ..shop.signals import order_canceled, order_paid
from ..ticket.service import find_ticket_for_user
from ..party.models import Party

from .authorization import ShopPermission


blueprint = create_blueprint('shop_admin', __name__)


permission_registry.register_enum(ShopPermission)


@blueprint.route('/articles')
@permission_required(ShopPermission.list_articles)
@templated
def article_index():
    """List parties to choose from."""
    parties = Party.query.all()
    return {'parties': parties}


@blueprint.route('/parties/<party_id>/articles', defaults={'page': 1})
@blueprint.route('/parties/<party_id>/articles/pages/<int:page>')
@permission_required(ShopPermission.list_articles)
@templated
def article_index_for_party(party_id, page):
    """List articles for that party."""
    party = Party.query.get_or_404(party_id)

    per_page = request.args.get('per_page', type=int, default=15)
    query = Article.query \
        .for_party(party) \
        .order_by(Article.description)

    articles = query.paginate(page, per_page)

    return {
        'party': party,
        'articles': articles,
    }


@blueprint.route('/articles/<id>')
@permission_required(ShopPermission.list_articles)
@templated
def article_view(id):
    """Show a single article."""
    article = Article.query.get_or_404(id)
    return {
        'article': article,
    }


@blueprint.route('/articles/<id>/ordered')
@permission_required(ShopPermission.list_articles)
@templated
def article_view_ordered(id):
    """List the people that have ordered this article, and the
    corresponding quantities.
    """
    article = Article.query.get_or_404(id)

    order_items = OrderItem.query \
        .filter_by(article=article) \
        .all()

    def transform(order_item):
        user = order_item.order.placed_by
        ticket = find_ticket_for_user(user, article.party)
        quantity = order_item.quantity
        return user, ticket, quantity

    users_tickets_quantities = map(transform, order_items)

    return {
        'article': article,
        'users_tickets_quantities': users_tickets_quantities,
        'now': datetime.now(),
    }


@blueprint.route('/orders')
@permission_required(ShopPermission.list_orders)
@templated
def order_index():<commit_msg>Only consider paid orders!<commit_after># -*- coding: utf-8 -*-

"""
byceps.blueprints.shop_admin.views
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

:Copyright: 2006-2014 <NAME>
"""

from datetime import datetime
from flask import request

from ...database import db
from ...util.framework import create_blueprint, flash_success
from ...util.templating import templated
from ...util.views import redirect_to

from ..authorization.decorators import permission_required
from ..authorization.registry import permission_registry
from ..shop.models import Article, Order, OrderItem, PaymentState
from ..shop.signals import order_canceled, order_paid
from ..ticket.service import find_ticket_for_user
from ..party.models import Party

from .authorization import ShopPermission


blueprint = create_blueprint('shop_admin', __name__)


permission_registry.register_enum(ShopPermission)


@blueprint.route('/articles')
@permission_required(ShopPermission.list_articles)
@templated
def article_index():
    """List parties to choose from."""
    parties = Party.query.all()
    return {'parties': parties}


@blueprint.route('/parties/<party_id>/articles', defaults={'page': 1})
@blueprint.route('/parties/<party_id>/articles/pages/<int:page>')
@permission_required(ShopPermission.list_articles)
@templated
def article_index_for_party(party_id, page):
    """List articles for that party."""
    party = Party.query.get_or_404(party_id)

    per_page = request.args.get('per_page', type=int, default=15)
    query = Article.query \
        .for_party(party) \
        .order_by(Article.description)

    articles = query.paginate(page, per_page)

    return {
        'party': party,
        'articles': articles,
    }


@blueprint.route('/articles/<id>')
@permission_required(ShopPermission.list_articles)
@templated
def article_view(id):
    """Show a single article."""
    article = Article.query.get_or_404(id)
    return {
        'article': article,
    }


@blueprint.route('/articles/<id>/ordered')
@permission_required(ShopPermission.list_articles)
@templated
def article_view_ordered(id):
    """List the people that have ordered this article, and the
    corresponding quantities.
    """
    article = Article.query.get_or_404(id)

    order_items = OrderItem.query \
        .filter_by(article=article) \
        .filter(Order._payment_state == PaymentState.paid) \
        .all()

    def transform(order_item):
        user = order_item.order.placed_by
        ticket = find_ticket_for_user(user, article.party)
        quantity = order_item.quantity
        return user, ticket, quantity

    users_tickets_quantities = map(transform, order_items)

    return {
        'article': article,
        'users_tickets_quantities': users_tickets_quantities,
        'now': datetime.now(),
    }


@blueprint.route('/orders')
@permission_required(ShopPermission.list_orders)
@templated
def order_index():
{'fraction_non_alphanumeric': 0.12819167541098286, 'fraction_numerical': 0.007345225603357817, 'mean_word_length': 4.199090909090909, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3391150', 'n_tokens_mistral': 1756, 'n_tokens_neox': 1667, 'n_words': 364}