Ajout de la commande 'sync' pour ajouter les commandes venant de l'API

This commit is contained in:
Michel Roux 2016-02-12 17:08:41 +01:00
parent 0d5876fda5
commit 83885ca114
6 changed files with 62 additions and 11 deletions

View File

View File

View File

View File

@ -0,0 +1,49 @@
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!'

View File

@ -5,22 +5,22 @@ from django.db import models
class Order(models.Model): class Order(models.Model):
# TODO: Revoir les max_length (difficile d'affirmer une limite précise avec le XML) # TODO: Revoir les max_length et null (difficile d'affirmer une limite précise avec le XML)
# TODO: Pas de PRIMARY KEY, du coup, le sync fera des doublons
marketplace = models.CharField(max_length=10) marketplace = models.CharField(max_length=10)
idFlux = models.IntegerField() idFlux = models.IntegerField()
order_id = models.CharField(max_length=10) order_id = models.CharField(max_length=20)
order_mrid = models.CharField(max_length=10) order_mrid = models.CharField(max_length=20)
order_refid = models.CharField(max_length=10) order_refid = models.CharField(max_length=20)
order_external_id = models.CharField(max_length=10) order_external_id = models.CharField(max_length=20, null=True)
# Colonne unique pour le order_purchase_date et order_purchase_heure because DateTime # Colonne unique pour le order_purchase_date et order_purchase_heure because DateTime
# TODO: Le prendre en compte lors de la réception du XML puis de l'insertion en base order_purchase = models.DateTimeField(null=True)
order_purchase = models.DateTimeField()
order_amount = models.FloatField() order_amount = models.FloatField()
order_tax = models.FloatField() order_tax = models.FloatField(null=True)
order_shipping = models.FloatField() order_shipping = models.FloatField()
order_commission = models.FloatField() order_commission = models.FloatField()
order_processing_fee = models.FloatField() order_processing_fee = models.FloatField()
order_comments = models.TextField() order_comments = models.TextField(null=True)
customer_id = models.CharField(max_length=10) customer_id = models.CharField(max_length=20, null=True)
order_ip = models.GenericIPAddressField() order_ip = models.GenericIPAddressField(null=True)
order_items = models.IntegerField() order_items = models.IntegerField()

2
requirements.txt Normal file
View File

@ -0,0 +1,2 @@
django
pytz