Guest User

Untitled

a guest
Feb 16th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.78 KB | None | 0 0
  1. """ Export store data """
  2.  
  3. import random
  4. import string
  5. import os
  6. import sys
  7. import csv
  8.  
  9. import colander
  10.  
  11. from pyramid.paster import (
  12. bootstrap,
  13. setup_logging,
  14. )
  15.  
  16. from substanced.util import find_catalog
  17. from substanced.objectmap import find_objectmap
  18.  
  19. from ..interfaces import IClothingSKU
  20. from ..product import get_price
  21.  
  22.  
  23. ACCESSKEY = 'C'
  24.  
  25. def export_users(argv=sys.argv):
  26.  
  27. def usage():
  28. cmd = os.path.basename(argv[0])
  29. print 'usage: %s <config_uri>\n' % cmd
  30. sys.exit()
  31.  
  32. if len(argv) < 2:
  33. usage()
  34.  
  35. config_uri = argv[1]
  36. setup_logging(config_uri)
  37. env = bootstrap(config_uri)
  38. root = env['root']
  39.  
  40. order = [
  41. 'accesskey',
  42. 'password',
  43. 'firstname',
  44. 'lastname',
  45. 'companyname',
  46. 'billingaddress1',
  47. 'billingaddress2',
  48. 'city',
  49. 'state',
  50. 'postalcode',
  51. 'country',
  52. 'phonenumber',
  53. 'faxnumber',
  54. 'emailaddress',
  55. 'paysstatetax',
  56. 'taxid',
  57. 'emailsubscriber',
  58. 'catalogsubscriber',
  59. 'lastmodified',
  60. 'percentdiscount',
  61. 'websiteaddress',
  62. 'discountlevel',
  63. 'customertype',
  64. 'lastmodby',
  65. 'customer_isanonymous',
  66. 'issuperadmin',
  67. 'news1',
  68. 'news2',
  69. 'news3',
  70. 'news4',
  71. 'news5',
  72. 'news6',
  73. 'news7',
  74. 'news8',
  75. 'news9',
  76. 'news10',
  77. 'news11',
  78. 'news12',
  79. 'news13',
  80. 'news14',
  81. 'news15',
  82. 'news16',
  83. 'news17',
  84. 'news18',
  85. 'news19',
  86. 'news20',
  87. 'allow_access_to_private_sections',
  88. 'customer_notes',
  89. 'salesrep_customerid',
  90. 'id_customers_groups',
  91. 'custom_field_custom1',
  92. 'custom_field_custom2',
  93. 'custom_field_custom3',
  94. 'custom_field_custom4',
  95. 'custom_field_custom5',
  96. 'removed_from_rewards',
  97. 'shipcompanyname',
  98. 'shipfirstname',
  99. 'shiplastname',
  100. 'shipaddress1',
  101. 'shipaddress2',
  102. 'shipcity',
  103. 'shipstate',
  104. 'shippostalcode',
  105. 'shipcountry',
  106. 'shipphonenumber',
  107. 'shipfaxnumber',
  108. ]
  109.  
  110. fields = {
  111. 'accesskey': 'C',
  112. 'password': '',
  113. 'firstname': None,
  114. 'lastname': None,
  115. 'companyname': '',
  116. 'billingaddress1': None,
  117. 'billingaddress2': None,
  118. 'city': None,
  119. 'state': None,
  120. 'postalcode': None,
  121. 'country': None,
  122. 'phonenumber': None,
  123. 'faxnumber': '',
  124. 'emailaddress': '',
  125. 'paysstatetax': '',
  126. 'taxid': '',
  127. 'emailsubscriber': 'N',
  128. 'catalogsubscriber': 'N',
  129. 'lastmodified': '10/9/2013 3:53:00 PM',
  130. 'percentdiscount': '',
  131. 'websiteaddress': '',
  132. 'discountlevel': '',
  133. 'customertype': '',
  134. 'lastmodby': '1',
  135. 'customer_isanonymous': 'N',
  136. 'issuperadmin': 'N',
  137. 'news1': 'N',
  138. 'news2': 'N',
  139. 'news3': 'N',
  140. 'news4': 'N',
  141. 'news5': 'N',
  142. 'news6': 'N',
  143. 'news7': 'N',
  144. 'news8': 'N',
  145. 'news9': 'N',
  146. 'news10': 'N',
  147. 'news11': 'N',
  148. 'news12': 'N',
  149. 'news13': 'N',
  150. 'news14': 'N',
  151. 'news15': 'N',
  152. 'news16': 'N',
  153. 'news17': 'N',
  154. 'news18': 'N',
  155. 'news19': 'N',
  156. 'news20': 'N',
  157. 'allow_access_to_private_sections': 'N',
  158. 'customer_notes': '',
  159. 'salesrep_customerid': '',
  160. 'id_customers_groups': '',
  161. 'custom_field_custom1': '',
  162. 'custom_field_custom2': '',
  163. 'custom_field_custom3': '',
  164. 'custom_field_custom4': '',
  165. 'custom_field_custom5': '',
  166. 'removed_from_rewards': 'N',
  167. 'shipcompanyname': '',
  168. 'shipfirstname': None,
  169. 'shiplastname': None,
  170. 'shipaddress1': None,
  171. 'shipaddress2': None,
  172. 'shipcity': None,
  173. 'shipstate': None,
  174. 'shippostalcode': None,
  175. 'shipcountry': None,
  176. 'shipphonenumber': None,
  177. 'shipfaxnumber': '',
  178. }
  179.  
  180. mapped_fields = {
  181. 'firstname': 'first_name',
  182. 'lastname': 'last_name',
  183. 'billingaddress1': 'street1',
  184. 'billingaddress2': 'street2',
  185. 'city': 'city',
  186. 'state': 'state',
  187. 'postalcode': 'postal',
  188. 'country': 'country',
  189. 'phonenumber': 'phone',
  190. 'shipfirstname': 'first_name',
  191. 'shiplastname': 'last_name',
  192. 'shipaddress1': 'street1',
  193. 'shipaddress2': 'street2',
  194. 'shipcity': 'city',
  195. 'shipstate': 'state',
  196. 'shippostalcode': 'postal',
  197. 'shipcountry': 'country',
  198. 'shipphonenumber': 'phone',
  199. }
  200.  
  201. chars = string.ascii_letters + string.digits
  202. random.seed = (os.urandom(1024))
  203.  
  204. print ','.join(order)
  205.  
  206. users = root['principals']['users'].values()
  207. used_emails = []
  208.  
  209. count = 0
  210. for user in users:
  211. if not user.first_name:
  212. continue
  213. count += 1
  214. password = ''.join(random.choice(chars) for i in range(8))
  215. email = user.email.lower()
  216. customer_type = user.customer_type
  217. if email in used_emails:
  218. email = 'customer_%s%s@changeme.com' % (user.first_name[0],
  219. user.last_name)
  220. email = email.replace(' ', '_')
  221. diff = 2
  222. orig_email = email
  223. while email in used_emails:
  224. email = '%s_%s' % (orig_email, str(diff))
  225. diff = diff + 1
  226. used_emails.append(email)
  227. address1 = None
  228. if len(user['addresses'].keys()) > 0:
  229. address1 = user['addresses'].values()[0]
  230.  
  231. line = '"%s"' % ACCESSKEY
  232. for field in order:
  233. value = fields[field]
  234. if value is None:
  235. attr = mapped_fields[field]
  236. if field.startswith('ship') and address1 is not None:
  237. value = getattr(address1, attr, '')
  238. else:
  239. value = getattr(user, attr, '')
  240. if field == 'password':
  241. value = password
  242. if field == 'emailaddress':
  243. value = email
  244. if field == 'accesskey':
  245. continue
  246. value = value.replace(',', ' ')
  247. value = value.replace('"', "'")
  248. line = line + ',"%s"' % value.encode('utf8')
  249.  
  250. print line
  251.  
  252.  
  253. def export_pricing(argv=sys.argv):
  254.  
  255. def usage():
  256. cmd = os.path.basename(argv[0])
  257. print 'usage: %s <config_uri>\n' % cmd
  258. sys.exit()
  259.  
  260. if len(argv) < 2:
  261. usage()
  262.  
  263. config_uri = argv[1]
  264. setup_logging(config_uri)
  265. env = bootstrap(config_uri)
  266. root = env['root']
  267.  
  268. catalog = find_catalog(root, 'system')
  269. q = ( catalog['interfaces'].eq(IClothingSKU) )
  270. items = q.execute()
  271.  
  272. print "sku,default,guide,industry,military,pro,banquet,internalguide"
  273. for item in items:
  274. product = item.get_product()
  275. sku = item.__name__
  276. line = "%s,%s,%s,%s,%s,%s,%s,%s" % (
  277. sku,
  278. get_price(product, item, 'default'),
  279. get_price(product, item, 'guide'),
  280. get_price(product, item, 'industry'),
  281. get_price(product, item, 'military'),
  282. get_price(product, item, 'pro'),
  283. get_price(product, item, 'banquet'),
  284. get_price(product, item, 'internalguide'),
  285. )
  286. line = line.replace('<colander.null>','')
  287. line = line.replace('None','')
  288. print line
  289.  
  290.  
  291. def export_orders(argv=sys.argv):
  292.  
  293. def usage():
  294. cmd = os.path.basename(argv[0])
  295. print 'usage: %s <config_uri> <cutomers_csv>\n' % cmd
  296. sys.exit()
  297.  
  298. if len(argv) < 3:
  299. usage()
  300.  
  301. config_uri = argv[1]
  302. customers_csv = argv[2]
  303. export_format = 'csv'
  304. if len(argv) > 3:
  305. export_format = argv[3]
  306.  
  307. setup_logging(config_uri)
  308. env = bootstrap(config_uri)
  309. root = env['root']
  310.  
  311. order_statuses = {
  312. 'unshipped': 'Processing',
  313. 'shipped': 'Shipped',
  314. 'cancelled': 'Cancelled',
  315. }
  316.  
  317. field_order = [
  318. 'orderid',
  319. 'customerid',
  320. 'billingcompanyname',
  321. 'billingfirstname',
  322. 'billinglastname',
  323. 'billingaddress1',
  324. 'billingaddress2',
  325. 'billingcity',
  326. 'billingstate',
  327. 'billingpostalcode',
  328. 'billingcountry',
  329. 'billingphonenumber',
  330. 'shipcompanyname',
  331. 'shipfirstname',
  332. 'shiplastname',
  333. 'shipaddress1',
  334. 'shipaddress2',
  335. 'shipcity',
  336. 'shipstate',
  337. 'shippostalcode',
  338. 'shipcountry',
  339. 'shipphonenumber',
  340. 'shipfaxnumber',
  341. 'shippingmethodid',
  342. 'totalshippingcost',
  343. 'salestaxrate',
  344. 'paymentamount',
  345. 'paymentmethodid',
  346. 'creditcardauthorizationnumber',
  347. 'creditcardtransactionid',
  348. 'giftcardidused',
  349. 'orderdate',
  350. 'shipdate',
  351. 'orderstatus',
  352. 'orderid_third_party',
  353. 'cc_last4',
  354. ]
  355.  
  356. fields = {
  357. 'orderid': '',
  358. 'customerid': '',
  359. 'billingcompanyname': '',
  360. 'billingfirstname': None,
  361. 'billinglastname': None,
  362. 'billingaddress1': None,
  363. 'billingaddress2': None,
  364. 'billingcity': None,
  365. 'billingstate': None,
  366. 'billingpostalcode': None,
  367. 'billingcountry': None,
  368. 'billingphonenumber': None,
  369. 'shipcompanyname': '',
  370. 'shipfirstname': None,
  371. 'shiplastname': None,
  372. 'shipaddress1': None,
  373. 'shipaddress2': None,
  374. 'shipcity': None,
  375. 'shipstate': None,
  376. 'shippostalcode': None,
  377. 'shipcountry': None,
  378. 'shipphonenumber': None,
  379. 'shipfaxnumber': '',
  380. 'shippingmethodid': '103',
  381. 'totalshippingcost': None,
  382. 'salestaxrate': None,
  383. 'paymentamount': '',
  384. 'paymentmethodid': '5',
  385. 'creditcardauthorizationnumber': '',
  386. 'creditcardtransactionid': '',
  387. 'giftcardidused': '',
  388. 'orderdate': None,
  389. 'shipdate': None,
  390. 'orderstatus': '',
  391. 'orderid_third_party': '',
  392. 'cc_last4': None,
  393. }
  394.  
  395. mapped_fields = {
  396. 'billingfirstname': 'billing_address:first_name',
  397. 'billinglastname': 'billing_address:last_name',
  398. 'billingaddress1': 'billing_address:street1',
  399. 'billingaddress2': 'billing_address:street2',
  400. 'billingcity': 'billing_address:city',
  401. 'billingstate': 'billing_address:state',
  402. 'billingpostalcode': 'billing_address:postal',
  403. 'billingcountry': 'billing_address:country',
  404. 'billingphonenumber': 'billing_address:phone',
  405. 'shipfirstname': 'shipping_address:first_name',
  406. 'shiplastname': 'shipping_address:last_name',
  407. 'shipaddress1': 'shipping_address:street1',
  408. 'shipaddress2': 'shipping_address:street2',
  409. 'shipcity': 'shipping_address:city',
  410. 'shipstate': 'shipping_address:state',
  411. 'shippostalcode': 'shipping_address:postal',
  412. 'shipcountry': 'shipping_address:country',
  413. 'shipphonenumber': 'shipping_address:phone',
  414. 'totalshippingcost': 'shipping',
  415. 'salestaxrate': 'tax_rate',
  416. 'orderdate': 'order_date',
  417. 'shipdate': 'ship_date',
  418. 'cc_last4': 'card_visible',
  419. }
  420.  
  421. user_file = open(customers_csv)
  422. reader = csv.reader(user_file)
  423. users = {}
  424. for line in reader:
  425. users[line[1]] = line[0]
  426.  
  427. ORDERS = [
  428. '1018423',
  429. '1018424',
  430. '1018426',
  431. '1018427',
  432. '1018429',
  433. '1018430',
  434. '1018432',
  435. '1018433',
  436. '1018434',
  437. '1018436',
  438. '1018437',
  439. '1018438',
  440. '1018439',
  441. '1018440',
  442. '1018441',
  443. '1018443',
  444. '1018444',
  445. '1018445',
  446. '1018446',
  447. '1018447',
  448. '1018449',
  449. '1018450',
  450. '1018451',
  451. '1018452',
  452. '1018453',
  453. '1018455',
  454. '1018457',
  455. '1018458',
  456. '1018459',
  457. '1018460',
  458. '1018461',
  459. '1018462',
  460. '1018463',
  461. '1018464',
  462. '1018465',
  463. '1018466',
  464. '1018467',
  465. '1018468',
  466. '1018470',
  467. '1018471',
  468. '1018473',
  469. '1018474',
  470. '1018475',
  471. '1018476',
  472. '1018477',
  473. '1018478',
  474. '1018479',
  475. '1018480',
  476. '1018481',
  477. '1018482',
  478. '1018483',
  479. '1018484',
  480. '1018486',
  481. '1018487',
  482. '1018488',
  483. '1018489',
  484. '1018490',
  485. '1018491',
  486. '1018492',
  487. '1018493',
  488. '1018494',
  489. '1018495',
  490. '1018496',
  491. '1018497',
  492. '1018498',
  493. '1018500',
  494. '1018501',
  495. '1018502',
  496. '1018503',
  497. '1018504',
  498. '1018505',
  499. '1018506',
  500. '1018507',
  501. '1018508',
  502. '1018509',
  503. '1018511',
  504. '1018512',
  505. '1018514',
  506. '1018515',
  507. '1018516',
  508. '1018518',
  509. '1018519',
  510. '1018520',
  511. '1018521',
  512. '1018522',
  513. '1018524',
  514. '1018525',
  515. '1018526',
  516. '1018527',
  517. '1018528',
  518. '1018529',
  519. '1018530',
  520. '1018531',
  521. '1018532',
  522. '1018533',
  523. '1018534',
  524. '1018536',
  525. '1018538',
  526. '1018539',
  527. '1018540',
  528. '1018541',
  529. '1018542',
  530. '1018543',
  531. '1018544',
  532. '1018545',
  533. '1018546',
  534. '1018547',
  535. '1018548',
  536. '1018549',
  537. '1018550',
  538. '1018551',
  539. '1018552',
  540. '1018553',
  541. '1018554',
  542. '1018555',
  543. '1018556',
  544. '1018557',
  545. '1018558',
  546. '1018559',
  547. '1018560',
  548. '1018561',
  549. '1018562',
  550. '1018563',
  551. '1018564',
  552. '1018565',
  553. '1018566',
  554. '1018567',
  555. '1018569',
  556. '1018570',
  557. '1018571',
  558. '1018573',
  559. '1018574',
  560. '1018575',
  561. '1018576',
  562. '1018577',
  563. '1018578',
  564. '1018579',
  565. '1018580',
  566. '1018581',
  567. ]
  568. #orders = root['orders'].values()
  569. orders = root['orders']
  570. orders = [orders[o] for o in ORDERS]
  571.  
  572. if export_format == 'csv':
  573. print ','.join(field_order)
  574.  
  575. count = 0
  576. for order in orders:
  577. count += 1
  578. orderid = str(int(order.__name__))
  579. customer = order.get_user()
  580. if customer is None:
  581. continue
  582. customerid = users.get(customer.email, None)
  583. if customerid is None:
  584. continue
  585. customerid = str(customerid)
  586. amount = order.get_total() - order.get_giftcert_value_used()
  587.  
  588. if export_format == 'csv':
  589. line = '"%s"' % orderid
  590. else:
  591. print """<?xml version="1.0" encoding="utf-8" ?>
  592. <Volusion_API>
  593. <Orders>
  594. """
  595.  
  596. for field in field_order:
  597. value = fields[field]
  598. if value is None:
  599. query = order
  600. attr = mapped_fields[field]
  601. query_field = attr
  602. if ':' in attr:
  603. query = getattr(order, attr.split(':')[0])
  604. query_field = attr.split(':')[1]
  605. try:
  606. value = query[query_field]
  607. except (KeyError, TypeError):
  608. value = getattr(query, query_field, None)
  609. if field == 'orderid':
  610. continue
  611. if field == 'customerid':
  612. value = customerid
  613. if field == 'orderid_third_party':
  614. value = orderid
  615. if field == 'paymentamount':
  616. value = amount
  617. if field == 'orderstatus':
  618. value = order_statuses.get(order.processing_status, 'Processing')
  619. if field == 'cc_last4' and value:
  620. value = value[-4:]
  621. if field == 'orderdate' and value:
  622. value = value.strftime('%m/%d/%Y')
  623. if field == 'shipdate' and value:
  624. value = value.strftime('%m/%d/%Y')
  625. if value == colander.null:
  626. value = ''
  627. if value is None:
  628. value = ''
  629. try:
  630. value = value.replace(',', ' ')
  631. value = value.replace('"', "'")
  632. except AttributeError:
  633. value = str(value)
  634.  
  635. if export_format == 'csv':
  636. line = line + ',"%s"' % value.encode('utf8')
  637. else:
  638. print "<%s>%s</%s>" % (field, value.encode('utf8'), field)
  639.  
  640. if export_format == 'csv':
  641. print line
  642. else:
  643. items = order.values()
  644. shipped = order.ship_date and 'Y' or 'N'
  645. shipped_date = order.ship_date and order.ship_date.strftime('%m/%d/%Y') or ''
  646. for item in items:
  647. sku = item.get_sku()
  648. if sku is not None:
  649. sku = sku.__name__
  650. else:
  651. continue
  652. print "<OrderDetails>"
  653. print "<productcode>%s</productcode>" % sku
  654. print "<productname>%s</productname>" % item.description
  655. print "<quantity>%s</quantity>" % str(item.quantity)
  656. print "<productprice>%s</productprice>" % str(item.price)
  657. print "<totalprice>%s</totalprice>" % str(item.get_extended_price())
  658. print "<shipped>%s</shipped>" % shipped
  659. print "<shipdate>%s</shipdate>" % shipped_date
  660. print "</OrderDetails>"
  661.  
  662. print """</Orders>
  663. </Volusion_API>
  664. &&@@------@@&&
  665. """
  666.  
  667.  
  668. def export_order_details(argv=sys.argv):
  669.  
  670. def usage():
  671. cmd = os.path.basename(argv[0])
  672. print 'usage: %s <config_uri> <orders_csv>\n' % cmd
  673. sys.exit()
  674.  
  675. if len(argv) < 3:
  676. usage()
  677.  
  678. config_uri = argv[1]
  679. orders_csv = argv[2]
  680.  
  681. config_uri = argv[1]
  682. setup_logging(config_uri)
  683. env = bootstrap(config_uri)
  684. root = env['root']
  685.  
  686. field_order = [
  687. 'orderdetailid',
  688. 'orderid',
  689. 'productcode',
  690. 'productname',
  691. 'quantity',
  692. 'downloadfile',
  693. 'productprice',
  694. 'totalprice',
  695. 'shipped',
  696. 'shipdate',
  697. ]
  698.  
  699. print ','.join(field_order)
  700.  
  701. orders_file = open(orders_csv)
  702. reader = csv.reader(orders_file)
  703. original_orders = {}
  704. for line in reader:
  705. original_orders[line[1]] = line[0]
  706.  
  707. orders = root['orders'].values()
  708.  
  709. count = 0
  710. for order in orders:
  711. order_name = str(int(order.__name__))
  712. orderid = original_orders.get(order_name, None)
  713. if orderid is None:
  714. continue
  715. items = order.values()
  716. shipped = order.ship_date and 'Y' or 'N'
  717. shipped_date = order.ship_date and order.ship_date.strftime('%m/%d/%Y') or ''
  718. for item in items:
  719. sku = item.get_sku()
  720. if sku is not None:
  721. sku = sku.__name__
  722. else:
  723. continue
  724. count += 1
  725. line = '"%s","%s","%s","%s","%s","%s","%s","%s","%s","%s"' % (
  726. str(count),
  727. orderid,
  728. sku,
  729. item.description,
  730. str(item.quantity),
  731. '',
  732. str(item.price),
  733. str(item.get_extended_price()),
  734. shipped,
  735. shipped_date,
  736. )
  737. print line
  738.  
  739.  
  740. def export_giftcerts(argv=sys.argv):
  741.  
  742. def usage():
  743. cmd = os.path.basename(argv[0])
  744. print 'usage: %s <config_uri> <customers_csv>\n' % cmd
  745. sys.exit()
  746.  
  747. if len(argv) < 3:
  748. usage()
  749.  
  750. config_uri = argv[1]
  751. customers_csv = argv[2]
  752.  
  753. config_uri = argv[1]
  754. setup_logging(config_uri)
  755. env = bootstrap(config_uri)
  756. root = env['root']
  757. objectmap = find_objectmap(root)
  758.  
  759. field_order = [
  760. 'giftcardid',
  761. 'giftamount',
  762. 'giftamountleft',
  763. 'datecreated',
  764. 'byorderid',
  765. 'lastmodified',
  766. 'customerid',
  767. 'active',
  768. 'lastmodby',
  769. ]
  770.  
  771. print ','.join(field_order)
  772.  
  773. customers_file = open(customers_csv)
  774. reader = csv.reader(customers_file)
  775. customers = {}
  776. for line in reader:
  777. customers[line[1]] = line[0]
  778.  
  779. giftcerts = root['giftcerts'].values()
  780.  
  781. count = 0
  782. for giftcert in giftcerts:
  783. giftcardid = giftcert.__name__
  784. userid = giftcert.purchasing_user_id
  785. user = objectmap.object_for(userid)
  786. customerid = customers.get(user.email, '')
  787. datecreated = giftcert.purchase_date and giftcert.purchase_date.strftime('%m/%d/%Y') or ''
  788. lastmodified = giftcert.claim_date and giftcert.claim_date.strftime('%m/%d/%Y') or ''
  789. active = giftcert.current_value > 0 and 'Y' or 'N'
  790. count += 1
  791. line = '"%s","%s","%s","%s","%s","%s","%s","%s","%s"' % (
  792. giftcardid,
  793. str(giftcert.purchase_value),
  794. str(giftcert.current_value),
  795. datecreated,
  796. '',
  797. lastmodified,
  798. customerid,
  799. active,
  800. '1',
  801. )
  802. print line
Add Comment
Please, Sign In to add comment