Satchmo Contributions
This page is used to show small code snippets that might be useful to others.
Cron script to export data
Taken from #149. This script is used from a cron job to export store data to a csv:
#!/usr/bin/env python import csv import sys,os def setup_environment(): pathname = os.path.dirname(sys.argv[0]) sys.path.append(os.path.abspath(pathname)) sys.path.append(os.path.normpath(os.path.join(os.path.abspath(pathname), '../'))) os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' setup_environment() from satchmo.contact.models import Order,OrderItem if __name__=='__main__': pending = Order.objects.filter(status="Pending") writer = csv.writer(file("pending-orders.csv","wb")) cols = ["Name","Address1","Address2","City","State","Zip","Item(s)","Qty","Order Date"] writer.writerow(cols) for order in pending: #order_info: items = [[x.item.id,x.quantity] for x in OrderItem.objects.filter(order__id=order.id)] idx=0 for item in items: if idx==0: line_item=[order.contact.first_name+""+order.contact.last_name, order.shipStreet1,order.shipStreet2, order.shipCity,order.shipState,order.shipPostalCode] else: line_item= ["",]*6 [line_item.append(x) for x in item] if idx==0: line_item.append(order.timeStamp) writer.writerow(line_item) idx+=1
Regular update of the stock from the provided Excel file
1. To run this you need python XLRD installed.
2. Setup CRON to run the update URL every 1 hour or so, based on the frequency of the feed changes (e.g.: wget http://mystore.com/update/).[[BR]]
3. Excel feed, which contains two columns: SKU in the first column and Stock in the second column.
If the SKUs are repeated in the feed, the stock for the entries will count. All products are zeroed on the start. Therefore if you don't have a product in the feed, it's stock is zeroed.
from django.shortcuts import render_to_response
from satchmo.product.models import Product
import xlrd
from decimal import Decimal
def update_inventory(request):
# Zero all the products stock
products = Product.objects.order_by('sku')
for product in products:
product.items_in_stock = 0
product.save()
# Open Excel sheet & loop over it
book = xlrd.open_workbook("_archive/data/freestocklist3.xls")
sh = book.sheet_by_index(0)
for rx in range(sh.nrows):
update_sku = sh.cell_value(rowx=rx, colx=0)
update_stock = sh.cell_value(rowx=rx, colx=1)
# Clean up the excel formatting
update_sku = str(update_sku).rstrip(".0")
if type(update_stock) == type(1.0):
update_stock = Decimal(int(update_stock)).quantize(0)
else:
update_stock = 0
# Find product by SKU, Get the stock
# Update Product in Stock = Stock volume from the database + stock volume from the feed
try:
p = Product.objects.get(sku=update_sku)
current_stock = int(p.items_in_stock)
p.items_in_stock = update_stock + current_stock
p.save()
except Product.DoesNotExist:
continue
products = Product.objects.order_by('sku')
return render_to_response('update_inventory.html', {'products': products})
templates/update_inventory.html
<html>
<head><title>Inventory Update</title></head>
<body>
<table>
<tr>
<th>SKU</th>
<th>Stock</th>
</tr>
{% for product in products %}
<tr>
<td class="col1"><a title="{{ product.id }}" href="{{ product.get_absolute_url }}">{{ product.sku }}</a></td>
<td>{{ product.items_in_stock }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
Add to your URLconf:
urlpatterns += patterns('',
...
(r'^update', 'views.update_inventory'),
...
