Skip to content

Instantly share code, notes, and snippets.

@cguardia
Created February 2, 2018 21:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cguardia/9b36e90f5d03d91220ad266f8ddc092a to your computer and use it in GitHub Desktop.
Save cguardia/9b36e90f5d03d91220ad266f8ddc092a to your computer and use it in GitHub Desktop.
""" Export store data """
import random
import string
import os
import sys
import csv
import colander
from pyramid.paster import (
bootstrap,
setup_logging,
)
from substanced.util import find_catalog
from substanced.objectmap import find_objectmap
from ..interfaces import IClothingSKU
from ..product import get_price
ACCESSKEY = 'C'
def export_users(argv=sys.argv):
def usage():
cmd = os.path.basename(argv[0])
print 'usage: %s <config_uri>\n' % cmd
sys.exit()
if len(argv) < 2:
usage()
config_uri = argv[1]
setup_logging(config_uri)
env = bootstrap(config_uri)
root = env['root']
order = [
'accesskey',
'password',
'firstname',
'lastname',
'companyname',
'billingaddress1',
'billingaddress2',
'city',
'state',
'postalcode',
'country',
'phonenumber',
'faxnumber',
'emailaddress',
'paysstatetax',
'taxid',
'emailsubscriber',
'catalogsubscriber',
'lastmodified',
'percentdiscount',
'websiteaddress',
'discountlevel',
'customertype',
'lastmodby',
'customer_isanonymous',
'issuperadmin',
'news1',
'news2',
'news3',
'news4',
'news5',
'news6',
'news7',
'news8',
'news9',
'news10',
'news11',
'news12',
'news13',
'news14',
'news15',
'news16',
'news17',
'news18',
'news19',
'news20',
'allow_access_to_private_sections',
'customer_notes',
'salesrep_customerid',
'id_customers_groups',
'custom_field_custom1',
'custom_field_custom2',
'custom_field_custom3',
'custom_field_custom4',
'custom_field_custom5',
'removed_from_rewards',
'shipcompanyname',
'shipfirstname',
'shiplastname',
'shipaddress1',
'shipaddress2',
'shipcity',
'shipstate',
'shippostalcode',
'shipcountry',
'shipphonenumber',
'shipfaxnumber',
]
fields = {
'accesskey': 'C',
'password': '',
'firstname': None,
'lastname': None,
'companyname': '',
'billingaddress1': None,
'billingaddress2': None,
'city': None,
'state': None,
'postalcode': None,
'country': None,
'phonenumber': None,
'faxnumber': '',
'emailaddress': '',
'paysstatetax': '',
'taxid': '',
'emailsubscriber': 'N',
'catalogsubscriber': 'N',
'lastmodified': '10/9/2013 3:53:00 PM',
'percentdiscount': '',
'websiteaddress': '',
'discountlevel': '',
'customertype': '',
'lastmodby': '1',
'customer_isanonymous': 'N',
'issuperadmin': 'N',
'news1': 'N',
'news2': 'N',
'news3': 'N',
'news4': 'N',
'news5': 'N',
'news6': 'N',
'news7': 'N',
'news8': 'N',
'news9': 'N',
'news10': 'N',
'news11': 'N',
'news12': 'N',
'news13': 'N',
'news14': 'N',
'news15': 'N',
'news16': 'N',
'news17': 'N',
'news18': 'N',
'news19': 'N',
'news20': 'N',
'allow_access_to_private_sections': 'N',
'customer_notes': '',
'salesrep_customerid': '',
'id_customers_groups': '',
'custom_field_custom1': '',
'custom_field_custom2': '',
'custom_field_custom3': '',
'custom_field_custom4': '',
'custom_field_custom5': '',
'removed_from_rewards': 'N',
'shipcompanyname': '',
'shipfirstname': None,
'shiplastname': None,
'shipaddress1': None,
'shipaddress2': None,
'shipcity': None,
'shipstate': None,
'shippostalcode': None,
'shipcountry': None,
'shipphonenumber': None,
'shipfaxnumber': '',
}
mapped_fields = {
'firstname': 'first_name',
'lastname': 'last_name',
'billingaddress1': 'street1',
'billingaddress2': 'street2',
'city': 'city',
'state': 'state',
'postalcode': 'postal',
'country': 'country',
'phonenumber': 'phone',
'shipfirstname': 'first_name',
'shiplastname': 'last_name',
'shipaddress1': 'street1',
'shipaddress2': 'street2',
'shipcity': 'city',
'shipstate': 'state',
'shippostalcode': 'postal',
'shipcountry': 'country',
'shipphonenumber': 'phone',
}
chars = string.ascii_letters + string.digits
random.seed = (os.urandom(1024))
print ','.join(order)
users = root['principals']['users'].values()
used_emails = []
count = 0
for user in users:
if not user.first_name:
continue
count += 1
password = ''.join(random.choice(chars) for i in range(8))
email = user.email.lower()
customer_type = user.customer_type
if email in used_emails:
email = 'customer_%s%s@changeme.com' % (user.first_name[0],
user.last_name)
email = email.replace(' ', '_')
diff = 2
orig_email = email
while email in used_emails:
email = '%s_%s' % (orig_email, str(diff))
diff = diff + 1
used_emails.append(email)
address1 = None
if len(user['addresses'].keys()) > 0:
address1 = user['addresses'].values()[0]
line = '"%s"' % ACCESSKEY
for field in order:
value = fields[field]
if value is None:
attr = mapped_fields[field]
if field.startswith('ship') and address1 is not None:
value = getattr(address1, attr, '')
else:
value = getattr(user, attr, '')
if field == 'password':
value = password
if field == 'emailaddress':
value = email
if field == 'accesskey':
continue
value = value.replace(',', ' ')
value = value.replace('"', "'")
line = line + ',"%s"' % value.encode('utf8')
print line
def export_pricing(argv=sys.argv):
def usage():
cmd = os.path.basename(argv[0])
print 'usage: %s <config_uri>\n' % cmd
sys.exit()
if len(argv) < 2:
usage()
config_uri = argv[1]
setup_logging(config_uri)
env = bootstrap(config_uri)
root = env['root']
catalog = find_catalog(root, 'system')
q = ( catalog['interfaces'].eq(IClothingSKU) )
items = q.execute()
print "sku,default,guide,industry,military,pro,banquet,internalguide"
for item in items:
product = item.get_product()
sku = item.__name__
line = "%s,%s,%s,%s,%s,%s,%s,%s" % (
sku,
get_price(product, item, 'default'),
get_price(product, item, 'guide'),
get_price(product, item, 'industry'),
get_price(product, item, 'military'),
get_price(product, item, 'pro'),
get_price(product, item, 'banquet'),
get_price(product, item, 'internalguide'),
)
line = line.replace('<colander.null>','')
line = line.replace('None','')
print line
def export_orders(argv=sys.argv):
def usage():
cmd = os.path.basename(argv[0])
print 'usage: %s <config_uri> <cutomers_csv>\n' % cmd
sys.exit()
if len(argv) < 3:
usage()
config_uri = argv[1]
customers_csv = argv[2]
export_format = 'csv'
if len(argv) > 3:
export_format = argv[3]
setup_logging(config_uri)
env = bootstrap(config_uri)
root = env['root']
order_statuses = {
'unshipped': 'Processing',
'shipped': 'Shipped',
'cancelled': 'Cancelled',
}
field_order = [
'orderid',
'customerid',
'billingcompanyname',
'billingfirstname',
'billinglastname',
'billingaddress1',
'billingaddress2',
'billingcity',
'billingstate',
'billingpostalcode',
'billingcountry',
'billingphonenumber',
'shipcompanyname',
'shipfirstname',
'shiplastname',
'shipaddress1',
'shipaddress2',
'shipcity',
'shipstate',
'shippostalcode',
'shipcountry',
'shipphonenumber',
'shipfaxnumber',
'shippingmethodid',
'totalshippingcost',
'salestaxrate',
'paymentamount',
'paymentmethodid',
'creditcardauthorizationnumber',
'creditcardtransactionid',
'giftcardidused',
'orderdate',
'shipdate',
'orderstatus',
'orderid_third_party',
'cc_last4',
]
fields = {
'orderid': '',
'customerid': '',
'billingcompanyname': '',
'billingfirstname': None,
'billinglastname': None,
'billingaddress1': None,
'billingaddress2': None,
'billingcity': None,
'billingstate': None,
'billingpostalcode': None,
'billingcountry': None,
'billingphonenumber': None,
'shipcompanyname': '',
'shipfirstname': None,
'shiplastname': None,
'shipaddress1': None,
'shipaddress2': None,
'shipcity': None,
'shipstate': None,
'shippostalcode': None,
'shipcountry': None,
'shipphonenumber': None,
'shipfaxnumber': '',
'shippingmethodid': '103',
'totalshippingcost': None,
'salestaxrate': None,
'paymentamount': '',
'paymentmethodid': '5',
'creditcardauthorizationnumber': '',
'creditcardtransactionid': '',
'giftcardidused': '',
'orderdate': None,
'shipdate': None,
'orderstatus': '',
'orderid_third_party': '',
'cc_last4': None,
}
mapped_fields = {
'billingfirstname': 'billing_address:first_name',
'billinglastname': 'billing_address:last_name',
'billingaddress1': 'billing_address:street1',
'billingaddress2': 'billing_address:street2',
'billingcity': 'billing_address:city',
'billingstate': 'billing_address:state',
'billingpostalcode': 'billing_address:postal',
'billingcountry': 'billing_address:country',
'billingphonenumber': 'billing_address:phone',
'shipfirstname': 'shipping_address:first_name',
'shiplastname': 'shipping_address:last_name',
'shipaddress1': 'shipping_address:street1',
'shipaddress2': 'shipping_address:street2',
'shipcity': 'shipping_address:city',
'shipstate': 'shipping_address:state',
'shippostalcode': 'shipping_address:postal',
'shipcountry': 'shipping_address:country',
'shipphonenumber': 'shipping_address:phone',
'totalshippingcost': 'shipping',
'salestaxrate': 'tax_rate',
'orderdate': 'order_date',
'shipdate': 'ship_date',
'cc_last4': 'card_visible',
}
user_file = open(customers_csv)
reader = csv.reader(user_file)
users = {}
for line in reader:
users[line[1]] = line[0]
ORDERS = [
'1018423',
'1018424',
'1018426',
'1018427',
'1018429',
'1018430',
'1018432',
'1018433',
'1018434',
'1018436',
'1018437',
'1018438',
'1018439',
'1018440',
'1018441',
'1018443',
'1018444',
'1018445',
'1018446',
'1018447',
'1018449',
'1018450',
'1018451',
'1018452',
'1018453',
'1018455',
'1018457',
'1018458',
'1018459',
'1018460',
'1018461',
'1018462',
'1018463',
'1018464',
'1018465',
'1018466',
'1018467',
'1018468',
'1018470',
'1018471',
'1018473',
'1018474',
'1018475',
'1018476',
'1018477',
'1018478',
'1018479',
'1018480',
'1018481',
'1018482',
'1018483',
'1018484',
'1018486',
'1018487',
'1018488',
'1018489',
'1018490',
'1018491',
'1018492',
'1018493',
'1018494',
'1018495',
'1018496',
'1018497',
'1018498',
'1018500',
'1018501',
'1018502',
'1018503',
'1018504',
'1018505',
'1018506',
'1018507',
'1018508',
'1018509',
'1018511',
'1018512',
'1018514',
'1018515',
'1018516',
'1018518',
'1018519',
'1018520',
'1018521',
'1018522',
'1018524',
'1018525',
'1018526',
'1018527',
'1018528',
'1018529',
'1018530',
'1018531',
'1018532',
'1018533',
'1018534',
'1018536',
'1018538',
'1018539',
'1018540',
'1018541',
'1018542',
'1018543',
'1018544',
'1018545',
'1018546',
'1018547',
'1018548',
'1018549',
'1018550',
'1018551',
'1018552',
'1018553',
'1018554',
'1018555',
'1018556',
'1018557',
'1018558',
'1018559',
'1018560',
'1018561',
'1018562',
'1018563',
'1018564',
'1018565',
'1018566',
'1018567',
'1018569',
'1018570',
'1018571',
'1018573',
'1018574',
'1018575',
'1018576',
'1018577',
'1018578',
'1018579',
'1018580',
'1018581',
]
#orders = root['orders'].values()
orders = root['orders']
orders = [orders[o] for o in ORDERS]
if export_format == 'csv':
print ','.join(field_order)
count = 0
for order in orders:
count += 1
orderid = str(int(order.__name__))
customer = order.get_user()
if customer is None:
continue
customerid = users.get(customer.email, None)
if customerid is None:
continue
customerid = str(customerid)
amount = order.get_total() - order.get_giftcert_value_used()
if export_format == 'csv':
line = '"%s"' % orderid
else:
print """<?xml version="1.0" encoding="utf-8" ?>
<Volusion_API>
<Orders>
"""
for field in field_order:
value = fields[field]
if value is None:
query = order
attr = mapped_fields[field]
query_field = attr
if ':' in attr:
query = getattr(order, attr.split(':')[0])
query_field = attr.split(':')[1]
try:
value = query[query_field]
except (KeyError, TypeError):
value = getattr(query, query_field, None)
if field == 'orderid':
continue
if field == 'customerid':
value = customerid
if field == 'orderid_third_party':
value = orderid
if field == 'paymentamount':
value = amount
if field == 'orderstatus':
value = order_statuses.get(order.processing_status, 'Processing')
if field == 'cc_last4' and value:
value = value[-4:]
if field == 'orderdate' and value:
value = value.strftime('%m/%d/%Y')
if field == 'shipdate' and value:
value = value.strftime('%m/%d/%Y')
if value == colander.null:
value = ''
if value is None:
value = ''
try:
value = value.replace(',', ' ')
value = value.replace('"', "'")
except AttributeError:
value = str(value)
if export_format == 'csv':
line = line + ',"%s"' % value.encode('utf8')
else:
print "<%s>%s</%s>" % (field, value.encode('utf8'), field)
if export_format == 'csv':
print line
else:
items = order.values()
shipped = order.ship_date and 'Y' or 'N'
shipped_date = order.ship_date and order.ship_date.strftime('%m/%d/%Y') or ''
for item in items:
sku = item.get_sku()
if sku is not None:
sku = sku.__name__
else:
continue
print "<OrderDetails>"
print "<productcode>%s</productcode>" % sku
print "<productname>%s</productname>" % item.description
print "<quantity>%s</quantity>" % str(item.quantity)
print "<productprice>%s</productprice>" % str(item.price)
print "<totalprice>%s</totalprice>" % str(item.get_extended_price())
print "<shipped>%s</shipped>" % shipped
print "<shipdate>%s</shipdate>" % shipped_date
print "</OrderDetails>"
print """</Orders>
</Volusion_API>
&&@@------@@&&
"""
def export_order_details(argv=sys.argv):
def usage():
cmd = os.path.basename(argv[0])
print 'usage: %s <config_uri> <orders_csv>\n' % cmd
sys.exit()
if len(argv) < 3:
usage()
config_uri = argv[1]
orders_csv = argv[2]
config_uri = argv[1]
setup_logging(config_uri)
env = bootstrap(config_uri)
root = env['root']
field_order = [
'orderdetailid',
'orderid',
'productcode',
'productname',
'quantity',
'downloadfile',
'productprice',
'totalprice',
'shipped',
'shipdate',
]
print ','.join(field_order)
orders_file = open(orders_csv)
reader = csv.reader(orders_file)
original_orders = {}
for line in reader:
original_orders[line[1]] = line[0]
orders = root['orders'].values()
count = 0
for order in orders:
order_name = str(int(order.__name__))
orderid = original_orders.get(order_name, None)
if orderid is None:
continue
items = order.values()
shipped = order.ship_date and 'Y' or 'N'
shipped_date = order.ship_date and order.ship_date.strftime('%m/%d/%Y') or ''
for item in items:
sku = item.get_sku()
if sku is not None:
sku = sku.__name__
else:
continue
count += 1
line = '"%s","%s","%s","%s","%s","%s","%s","%s","%s","%s"' % (
str(count),
orderid,
sku,
item.description,
str(item.quantity),
'',
str(item.price),
str(item.get_extended_price()),
shipped,
shipped_date,
)
print line
def export_giftcerts(argv=sys.argv):
def usage():
cmd = os.path.basename(argv[0])
print 'usage: %s <config_uri> <customers_csv>\n' % cmd
sys.exit()
if len(argv) < 3:
usage()
config_uri = argv[1]
customers_csv = argv[2]
config_uri = argv[1]
setup_logging(config_uri)
env = bootstrap(config_uri)
root = env['root']
objectmap = find_objectmap(root)
field_order = [
'giftcardid',
'giftamount',
'giftamountleft',
'datecreated',
'byorderid',
'lastmodified',
'customerid',
'active',
'lastmodby',
]
print ','.join(field_order)
customers_file = open(customers_csv)
reader = csv.reader(customers_file)
customers = {}
for line in reader:
customers[line[1]] = line[0]
giftcerts = root['giftcerts'].values()
count = 0
for giftcert in giftcerts:
giftcardid = giftcert.__name__
userid = giftcert.purchasing_user_id
user = objectmap.object_for(userid)
customerid = customers.get(user.email, '')
datecreated = giftcert.purchase_date and giftcert.purchase_date.strftime('%m/%d/%Y') or ''
lastmodified = giftcert.claim_date and giftcert.claim_date.strftime('%m/%d/%Y') or ''
active = giftcert.current_value > 0 and 'Y' or 'N'
count += 1
line = '"%s","%s","%s","%s","%s","%s","%s","%s","%s"' % (
giftcardid,
str(giftcert.purchase_value),
str(giftcert.current_value),
datecreated,
'',
lastmodified,
customerid,
active,
'1',
)
print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment