Clone wiki

satchmo / UsefulScripts

Some useful tips and scripts for Satchmo

Here are two shell aliases I use to make it a little easier to develop::

    alias delpyc='find ./ -type f -name "*.pyc" -exec rm -f {} \;'
    alias startserver='python runserver'

The delpyc is helpful if you want to clear out all your .pyc files and make sure there isn't any weirdness with old files.

The startserver just makes it easier to start up the development server with a given IP.

Doing category list in template

This is e.g. useful if you want to replace the infinite deep category listing with flat listing on the front page.

		        {% comment %}
				Optionally, category_list takes "slug" argument which corresponds human-readable category slug in Django admin.

				Category model for "cat" variable can be found in Satchmo/product/
			{% endcomment %}

                        {% category_list as var %}
				{% for cat in var %}
					<a href="{{ cat.get_absolute_url }}">{{ cat.translated_name }}</a>
				{% endfor %}
			{% endcategory_list %}

Making products taxable

Below is a snippet how to batch set tax base for many items from DJango management shell.


    Set all products taxable with the default tax class.

    (If the user did not this while creating products).

from product.models import Product, TaxClass
from django.db import transaction

default_tax = TaxClass.objects.filter(title="Default")[0]

for p in Product.objects.all():
    if not p.taxable:
         print "Was not taxable:" + str(p)
         p.taxable = True
         p.taxClass = default_tax

and run it

python shell

Fixing product price tax base

Below is a snippet to change a product price, so that if it has been inputted as tax inclusive price, the tax is excluded so that the final visitor visible price is tax inclusive if Satchmo "show taxed prices" setting is on.

Note: In Scandinavia stores deal with tax-inclusive prices and tax-exclusive prices are calculated in the receipt.

# Reduce tax base from all productunit prices (NOTE! Only quantity = 1 prices are considered)
# Correct tax-inclusive inputed prices to be tax-free prices

from product.models import Price, Product

from decimal import Decimal # Python decimal arithmetic

# 22% Finnish VAT
TAX_BASE = Decimal("0.22")

products = Product.objects.all()

for prod in products:
       price = Price.objects.get(product=prod, quantity='1')
   except Price.DoesNotExist:
       price = Price(product=prod, quantity='1')
   print "Got product " + str(prod) + " price " + str(price.price)

   # If Price is None the product is a variation
   # which uses its parent price
   if price.price != None:
      # Math is performed in Decimal preserving way
      price.price = price.price / (Decimal("1.0") + TAX_BASE)     
      print "New price:" + str(price.price)

      #price.price = value

and run it

python shell