This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

eParcel CSV generator

By: a guest on Apr 20th, 2012  |  syntax: Python  |  size: 3.97 KB  |  views: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. def generate_csv(orders):
  2.     """
  3.    Generate a consignment CSV from a list of orders
  4.  
  5.    Parameters:
  6.      orders - The list of orders to work with
  7.  
  8.    Returns:
  9.    An iterable that contains every (\\n terminated) line of the CSV as a string.
  10.    """
  11.     for order in orders:
  12.         # Each order from Shopify needs a Consignment line, and one or more
  13.         # Article line per physical parcel being sent.  Goods being sent
  14.         # overseas need one Goods line per Article line to pass customs.
  15.         # Additionally, overseas consignments can only have one
  16.         # article/physical parcel.  To simplify things, we simply print one
  17.         # consignment, article and goods line per order. This will work in
  18.         # all instances.
  19.         #
  20.         # See `docs/eParcel Consignment Import Guide.pdf`, page 8 for more
  21.         # information
  22.  
  23.         dest = order.shipping_address
  24.  
  25.         # International consignments have different requirements
  26.         international = dest.country_code != settings.COUNTRY_CODE
  27.  
  28.         weight_in_grams = int(order.total_weight) or settings.EPARCEL_DEFAULT_WEIGHT
  29.         weight = "%0.2f" % (weight_in_grams / 1000.0)
  30.         price = "%0.2f" % float(order.total_price)
  31.         item_count = sum([int(i.quantity) for i in order.line_items])
  32.  
  33.         data = [
  34.             ( # Consignment line
  35.                 'C',
  36.                 # B-D
  37.                 '', # Must be left blank
  38.                 settings.EPARCEL_ACCOUNT_NUMBER, # Optional, may be blank
  39.                 settings.EPARCEL_CHARGE_CODE['international' if international else 'domestic'],
  40.  
  41.                 # E-F: Create a new consignee
  42.                 '',
  43.                 dest.name,
  44.  
  45.                 # G-O: Shipping address
  46.                 dest.company or '',
  47.                 dest.address1 or '',
  48.                 dest.address2 or '',
  49.                 '', # address line 3
  50.                 '', # address line 4
  51.                 dest.city,
  52.                 dest.province_code,
  53.                 dest.zip,
  54.                 dest.country_code,
  55.  
  56.                 # P: Required for international consignments
  57.                 # TODO This is terrible
  58.                 dest.phone or '555 1234',
  59.  
  60.                 # Q-S: Display phone; fax number; special instructions
  61.                 '',
  62.                 '',
  63.                 '',
  64.  
  65.                 'N', # T: No signature required
  66.                 '',  # U: Part delivery
  67.                 '',  # V: No comments
  68.                 'N', # W: Do not add consignee to address book
  69.             ),
  70.  
  71.             ( # Article line
  72.                 'A',
  73.  
  74.                 weight,      # B: Weight of this article (sum of all weights)
  75.                 '', '', '',  # C-E: Length; width; height;
  76.                 item_count,  # F: Number of articles
  77.  
  78.                 'Clothing',  # G: Article description. What should go here?
  79.  
  80.                 'N',         # H: Goods are NOT dangerous
  81.  
  82.                 'N', '', '', # I-K: Insurance is not required
  83.             ),
  84.         ]
  85.         if international:
  86.             data.append(( # Goods line
  87.                 'G',
  88.  
  89.                 # B: Shipping from this country
  90.                 settings.COUNTRY_CODE,
  91.                 # C: HS Tarrif code. Dunno what this is?
  92.                 '',
  93.                 # D: Description
  94.                 'Clothing',
  95.  
  96.                 '', # E: Product type
  97.                 settings.EPARCEL_PRODUCT_CLASSIFICATION, # F
  98.  
  99.                 # G-H: Quantity and weight
  100.                 item_count, weight,
  101.                 # I-J: Unit value; total value.
  102.                 price, price,
  103.             ))
  104.  
  105.         # We can not out put this as `tablib.Dataset(*row).csv` as the
  106.         # documentation recommends, as each row has a different length, and
  107.         # tablib throws an error because of this. Outputting each row one by
  108.         # results in the same output, without a giant load of extra columns on
  109.         # some rows
  110.         for row in data:
  111.             yield tablib.Dataset(row).csv
clone this paste RAW Paste Data