Files @ 9da41c06de54
Branch filter:

Location: symposion_app/registrasion/templatetags/registrasion_tags.py - annotation

Christopher Neugebauer
Adds first badge support
875f736d67c5
f7289c21019b
68aa9b067bdb
c192fef491c4
c192fef491c4
36ecf7fd5489
67ac01e599d4
c192fef491c4
c192fef491c4
c192fef491c4
db332da9584d
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
c192fef491c4
c192fef491c4
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
fd7fff7879b3
36ecf7fd5489
db332da9584d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
fd7fff7879b3
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
faa25c9b3a7d
2c94e7538a6e
2c94e7538a6e
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
875f736d67c5
fd7fff7879b3
2c94e7538a6e
2c94e7538a6e
2c94e7538a6e
2c94e7538a6e
2c94e7538a6e
36ecf7fd5489
36ecf7fd5489
9d25725514cb
9d25725514cb
9d25725514cb
9d25725514cb
fd7fff7879b3
36ecf7fd5489
db332da9584d
36ecf7fd5489
36ecf7fd5489
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
36ecf7fd5489
db332da9584d
36ecf7fd5489
4021aa3c8ebc
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
fd7fff7879b3
9d25725514cb
fd7fff7879b3
68aa9b067bdb
fd5cf50fabd8
67ac01e599d4
67ac01e599d4
9da41c06de54
9da41c06de54
9da41c06de54
9da41c06de54
9da41c06de54
9da41c06de54
9da41c06de54
9da41c06de54
9da41c06de54
9da41c06de54
9da41c06de54
67ac01e599d4
67ac01e599d4
67ac01e599d4
6a37134172c3
6a37134172c3
67ac01e599d4
67ac01e599d4
6a37134172c3
6a37134172c3
6a37134172c3
67ac01e599d4
from registrasion.models import commerce
from registrasion.controllers.category import CategoryController
from registrasion.controllers.item import ItemController

from django import template
from django.db.models import Sum
from urllib import urlencode

register = template.Library()


def user_for_context(context):
    ''' Returns either context.user or context.request.user if the former is
    not defined. '''
    try:
        return context["user"]
    except KeyError:
        return context.request.user


@register.assignment_tag(takes_context=True)
def available_categories(context):
    ''' Gets all of the currently available products.

    Returns:
        [models.inventory.Category, ...]: A list of all of the categories that
            have Products that the current user can reserve.

    '''
    return CategoryController.available_categories(user_for_context(context))


@register.assignment_tag(takes_context=True)
def missing_categories(context):
    ''' Adds the categories that the user does not currently have. '''
    user = user_for_context(context)
    categories_available = set(CategoryController.available_categories(user))
    items = ItemController(user).items_pending_or_purchased()

    categories_held = set()

    for product, quantity in items:
        categories_held.add(product.category)

    return categories_available - categories_held


@register.assignment_tag(takes_context=True)
def available_credit(context):
    ''' Calculates the sum of unclaimed credit from this user's credit notes.

    Returns:
        Decimal: the sum of the values of unclaimed credit notes for the
            current user.

    '''

    notes = commerce.CreditNote.unclaimed().filter(
        invoice__user=user_for_context(context),
    )
    ret = notes.values("amount").aggregate(Sum("amount"))["amount__sum"] or 0
    return 0 - ret


@register.assignment_tag(takes_context=True)
def invoices(context):
    '''

    Returns:
        [models.commerce.Invoice, ...]: All of the current user's invoices. '''
    return commerce.Invoice.objects.filter(user=user_for_context(context))


@register.assignment_tag(takes_context=True)
def items_pending(context):
    ''' Gets all of the items that the user from this context has reserved.

    The user will be either `context.user`, and `context.request.user` if
    the former is not defined.
    '''

    return ItemController(user_for_context(context)).items_pending()


@register.assignment_tag(takes_context=True)
def items_purchased(context, category=None):
    ''' Returns the items purchased for this user.

    The user will be either `context.user`, and `context.request.user` if
    the former is not defined.
    '''

    return ItemController(user_for_context(context)).items_purchased(
        category=category
    )


@register.assignment_tag(takes_context=True)
def total_items_purchased(context, category=None):
    ''' Returns the number of items purchased for this user (sum of quantities).

    The user will be either `context.user`, and `context.request.user` if
    the former is not defined.
    '''

    return sum(i.quantity for i in items_purchased(context, category))


@register.assignment_tag(takes_context=True)
def report_as_csv(context, section):

    old_query = context.request.META["QUERY_STRING"]
    query = dict([("section", section), ("content_type", "text/csv")])
    querystring = urlencode(query)

    if old_query:
        querystring = old_query + "&" + querystring

    return context.request.path + "?" + querystring