1. Chris Moffitt
  2. satchmo

Wiki

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 manage.py runserver 192.168.1.9:8000'

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/models.py
			{% 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.

fixtax.py

"""

    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
         p.save()

and run it

python manage.py shell
execfile("fixtax.py")

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.

fixprice.py

# 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:
   try:
       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
      price.save()

and run it

python manage.py shell
execfile("fixprice.py")

Updated