Site Sections: Satchmo Main | Wiki | Demo Store |

Changeset 1001

Show
Ignore:
Timestamp:
01/15/08 18:52:40 (1 year ago)
Author:
bkroeze
Message:

using signals to catch the situation where a contact changes state or country in mid-order, updating tax appropriately. Also pushing tax-aware templates to the shipping option page.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • satchmo/trunk/satchmo/contact/forms.py

    r995 r1001  
    11from django import newforms as forms 
     2from django.dispatch import dispatcher 
    23from django.utils.translation import ugettext as _ 
    34from satchmo.configuration import config_value, config_get_group, SettingNotSet 
     
    56from satchmo.l10n.models import Country 
    67from satchmo.shop.models import Config 
     8from signals import satchmo_contact_location_changed 
    79import datetime 
    810 
     
    158160            bill_address = AddressBook(contact=customer) 
    159161                 
     162        changed_location = False 
    160163        address_keys = bill_address.__dict__.keys() 
    161164        for field in address_keys: 
     165            if (not changed_location) and field in ('state', 'country', 'city'): 
     166                if getattr(bill_address, field) != data[field]: 
     167                    changed_location = True 
    162168            try: 
    163169                setattr(bill_address, field, data[field]) 
     
    184190             
    185191            for field in address_keys: 
     192                if (not changed_location) and field in ('state', 'country', 'city'): 
     193                    if getattr(ship_address, field) != data[field]: 
     194                        changed_location = True 
    186195                try: 
    187196                    setattr(ship_address, field, data['ship_' + field]) 
     
    202211        phone.save() 
    203212         
     213        if changed_location: 
     214            dispatcher.send(signal=satchmo_contact_location_changed, contact=contact) 
     215         
    204216        return customer.id 
    205217 
  • satchmo/trunk/satchmo/contact/models.py

    r999 r1001  
    1818from satchmo.shop.templatetags.satchmo_currency import moneyfmt 
    1919from satchmo.shop.utils import load_module 
    20 from signals import order_success 
     20from signals import order_success, satchmo_contact_location_changed 
    2121import config 
    2222import datetime 
     
    908908        log.debug("caught cart changed signal - remove_order_on_cart_update") 
    909909        Order.objects.remove_partial_order(request) 
    910  
     910         
     911def _recalc_total_on_contact_change(contact=None): 
     912    #TODO: pull just the current order once we start using threadlocal middleware 
     913    log.debug("Recalculating all contact orders not in process") 
     914    orders = Order.objects.filter(contact=contact, status="") 
     915    log.debug("Found %i orders to recalc", orders.count()) 
     916    for order in orders: 
     917        order.copy_addresses() 
     918        order.recalculate_total() 
     919     
    911920dispatcher.connect(_remove_order_on_cart_update, signal=satchmo_cart_changed) 
     921dispatcher.connect(_recalc_total_on_contact_change, signal=satchmo_contact_location_changed) 
  • satchmo/trunk/satchmo/contact/signals.py

    r898 r1001  
    11"""Signals sent by the Cart system""" 
    2 order_success=object() 
     2order_success = object() 
     3satchmo_contact_location_changed = object() 
  • satchmo/trunk/satchmo/payment/common/forms.py

    r988 r1001  
    11from django import newforms as forms 
    22from django.conf import settings 
    3 from django.template import Context 
     3from django.template import RequestContext 
    44from django.template import loader 
    55from django.utils.translation import ugettext as _ 
     6from satchmo.configuration import config_value 
    67from satchmo.contact.forms import ContactInfoForm 
    78from satchmo.contact.models import Contact 
     
    1617import sys 
    1718 
    18 def _get_shipping_choices(paymentmodule, cart, contact): 
     19def _get_shipping_choices(request, paymentmodule, cart, contact, default_view_tax=False): 
    1920    """Iterate through legal shipping modules, building the list for display to the user. 
    2021     
     
    3132            t = loader.get_template(template) 
    3233            shipcost = method.cost() 
    33             c = Context(
     34            c = RequestContext(request,
    3435                'amount': shipcost, 
    3536                'description' : method.description(), 
    3637                'method' : method.method(), 
    37                 'expected_delivery' : method.expectedDelivery() }) 
     38                'expected_delivery' : method.expectedDelivery(), 
     39                'default_view_tax' : default_view_tax }) 
    3840            shipping_options.append((method.id, t.render(c))) 
    3941            shipping_dict[method.id] = shipcost 
     
    8486            self.tempContact = None 
    8587             
    86         shipping_choices, shipping_dict = _get_shipping_choices(paymentmodule, self.tempCart, self.tempContact) 
     88        if kwargs.has_key('default_view_tax'): 
     89            default_view_tax = kwargs['default_view_tax'] 
     90        else: 
     91            default_view_tax = config_value('TAX', 'DEFAULT_VIEW_TAX') 
     92             
     93        shipping_choices, shipping_dict = _get_shipping_choices(request, paymentmodule, self.tempCart, self.tempContact, default_view_tax=default_view_tax) 
    8794        self.fields['shipping'].choices = shipping_choices 
    8895        self.shipping_dict = shipping_dict 
  • satchmo/trunk/satchmo/tax/templatetags/satchmo_tax.py

    r996 r1001  
    4848            item = template.resolve_variable(self.cartitem, context) 
    4949        except template.VariableDoesNotExist: 
    50             raise tempalte.TemplateSyntaxError("No such variable: %s", self.cartitem) 
     50            raise template.TemplateSyntaxError("No such variable: %s", self.cartitem) 
    5151         
    5252        total = item.line_total + taxer.by_price(item.product.taxClass, item.line_total) 
     53         
    5354        if self.currency: 
    5455            return moneyfmt(total) 
     
    8485            return moneyfmt(total) 
    8586         
    86         if currency: 
    87             return moneyfmt(total) 
    8887        return total 
    8988 
     
    146145         
    147146    return TaxRateNode(taxclass, order, digits) 
     147     
     148class TaxedPriceNode(template.Node): 
     149    """Returns the taxed price for an amount. 
     150    """ 
     151    def __init__(self, price, currency, taxclass): 
     152        self.price = price 
     153        self.taxclass = taxclass 
     154        self.currency = currency 
     155 
     156    def render(self, context): 
     157        taxer = _get_taxprocessor(context['request']) 
     158        try: 
     159            price = template.resolve_variable(self.price, context) 
     160        except template.VariableDoesNotExist: 
     161            raise template.TemplateSyntaxError("No such variable: %s", self.price) 
     162 
     163        total = price + taxer.by_price(self.taxclass, price) 
     164         
     165        if self.currency: 
     166            return moneyfmt(total) 
     167                         
     168        return total 
     169         
     170def taxed_price(parser, token): 
     171    """Returns the taxed price for an amount.  If currency evaluates true, 
     172    then return the total formatted through moneyfmt. 
     173    Example: {% taxed_price amount [currency] [taxclass] %} 
     174    """ 
     175    tokens = token.contents.split() 
     176    if len(tokens) < 2: 
     177        raise template.TemplateSyntaxError, "'%s' tag requires an amount argument" % tokens[0] 
     178     
     179    price = tokens[1] 
     180    if len(tokens) > 2: 
     181        currency = tokens[2] 
     182    else: 
     183        currency = False 
     184     
     185    if len(tokens) > 3: 
     186        taxclass = tokens[3] 
     187    else: 
     188        taxclass = "Default" 
     189 
     190    return TaxedPriceNode(price, currency, taxclass) 
     191 
     192     
    148193 
    149194register.tag('cartitem_line_taxed_total', cartitem_line_taxed_total) 
    150195register.tag('cart_taxed_total', cart_taxed_total) 
     196register.tag('taxed_price', taxed_price) 
    151197register.tag('tax_rate', tax_rate) 
  • satchmo/trunk/satchmo/templates/shipping_options.html

    r727 r1001  
    11{% load i18n %} 
    22{% load satchmo_currency %} 
    3  
    4 {{amount|currency}} : {{ description }}<br> 
     3{% if default_view_tax %}{% load satchmo_tax %}{% endif %} 
     4{% if default_view_tax %}{% taxed_price amount 1 Shipping %}{% else %}{{amount|currency}}{% endif %} : {{ description }} 
     5<br> 
    56{% blocktrans %}Sent via {{ method }} arrives approximately {{ expected_delivery }}.{% endblocktrans %} 
    67<hr>