Lengow/orders/management/commands/sync.py

52 lines
2.1 KiB
Python

import urllib2
import warnings
from xml.etree import ElementTree
from django.core.management.base import BaseCommand
from orders.models import Order
warnings.filterwarnings(
'ignore', r"DateTimeField .* received a naive datetime",
RuntimeWarning, r'django\.db\.models\.fields')
class Command(BaseCommand):
help = 'Add new orders on the database from the order\'s API'
def handle(self, *args, **options):
response = urllib2.urlopen('http://test.lengow.io/orders-test.xml')
html = response.read()
xml = ElementTree.fromstring(html)
orders = xml.find('orders').findall('order')
for child in orders:
order = Order()
order.marketplace = child.find('marketplace').text
order.idFlux = child.find('idFlux').text
order.customer_id = child.find('customer_id').text
order.order_amount = child.find('order_amount').text
order.order_comments = child.find('order_comments').text
order.order_commission = child.find('order_commission').text
order.order_external_id = child.find('order_external_id').text
order.order_id = child.find('order_id').text
order.order_ip = child.find('order_ip').text
order.order_items = child.find('order_items').text
order.order_mrid = child.find('order_mrid').text
order.order_processing_fee = child.find('order_processing_fee').text
order.order_refid = child.find('order_refid').text
order.order_shipping = child.find('order_shipping').text
order.order_tax = child.find('order_tax').text
order_purchase_date = child.find('order_purchase_date').text
order_purchase_heure = child.find('order_purchase_heure').text
if order_purchase_heure is None:
order_purchase_heure = '00:00'
if order_purchase_date is not None:
order.order_purchase = order_purchase_date + ' ' + order_purchase_heure
order.save()
print str(len(orders)) + ' order(s) added!'