Changeset - fd7fff7879b3
[Not reviewed]
0 2 0
Christopher Neugebauer - 7 years ago 2017-01-07 23:44:19
chrisjrn@gmail.com
Allows contexts to directly supply a user (so we can access registration data when e-mailing people.)
2 files changed with 27 insertions and 8 deletions:
0 comments (0 inline, 0 general)
registrasion/templatetags/registrasion_tags.py
Show inline comments
 
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(context.request.user)
 
    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 = context.request.user
 
    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=context.request.user,
 
        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=context.request.user)
 
    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.'''
 
    return ItemController(context.request.user).items_pending()
 
    ''' 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. '''
 
    ''' 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(context.request.user).items_purchased(
 
    return ItemController(user_for_context(context)).items_purchased(
 
        category=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:
registrasion/views.py
Show inline comments
...
 
@@ -942,24 +942,25 @@ def invoice_mailout(request):
 

	
 
    emails = []
 

	
 
    if form.is_valid():
 
        emails = []
 
        for invoice in form.cleaned_data["invoice"]:
 
            # datatuple = (subject, message, from_email, recipient_list)
 
            from_email = form.cleaned_data["from_email"]
 
            subject = form.cleaned_data["subject"]
 
            body = Template(form.cleaned_data["body"]).render(
 
                Context({
 
                    "invoice" : invoice,
 
                    "user" : invoice.user,
 
                })
 
            )
 
            recipient_list = [invoice.user.email]
 
            emails.append(Email(subject, body, from_email, recipient_list))
 

	
 
        if form.cleaned_data["action"] == forms.InvoiceEmailForm.ACTION_SEND:
 
            # Send e-mails *ONLY* if we're sending.
 
            send_mass_mail(emails)
 
            messages.info(request, "The e-mails have been sent.")
 

	
 
    data = {
 
        "form": form,
0 comments (0 inline, 0 general)