From 83885ca1141b11d661c28aa8f9e57d788bf6003d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xe=CC=81fir=20Destiny?= Date: Fri, 12 Feb 2016 17:08:41 +0100 Subject: [PATCH] Ajout de la commande 'sync' pour ajouter les commandes venant de l'API --- orders/management/__init__.py | 0 orders/management/commands/__init__.py | 0 orders/management/commands/_private.py | 0 orders/management/commands/sync.py | 49 ++++++++++++++++++++++++++ orders/models.py | 22 ++++++------ requirements.txt | 2 ++ 6 files changed, 62 insertions(+), 11 deletions(-) create mode 100644 orders/management/__init__.py create mode 100644 orders/management/commands/__init__.py create mode 100644 orders/management/commands/_private.py create mode 100644 orders/management/commands/sync.py create mode 100644 requirements.txt diff --git a/orders/management/__init__.py b/orders/management/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/orders/management/commands/__init__.py b/orders/management/commands/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/orders/management/commands/_private.py b/orders/management/commands/_private.py new file mode 100644 index 0000000..e69de29 diff --git a/orders/management/commands/sync.py b/orders/management/commands/sync.py new file mode 100644 index 0000000..a219373 --- /dev/null +++ b/orders/management/commands/sync.py @@ -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!' diff --git a/orders/models.py b/orders/models.py index 4de4232..b698023 100644 --- a/orders/models.py +++ b/orders/models.py @@ -5,22 +5,22 @@ from django.db import models 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) idFlux = models.IntegerField() - order_id = models.CharField(max_length=10) - order_mrid = models.CharField(max_length=10) - order_refid = models.CharField(max_length=10) - order_external_id = models.CharField(max_length=10) + order_id = models.CharField(max_length=20) + order_mrid = models.CharField(max_length=20) + order_refid = models.CharField(max_length=20) + order_external_id = models.CharField(max_length=20, null=True) # 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() + order_purchase = models.DateTimeField(null=True) order_amount = models.FloatField() - order_tax = models.FloatField() + order_tax = models.FloatField(null=True) order_shipping = models.FloatField() order_commission = models.FloatField() order_processing_fee = models.FloatField() - order_comments = models.TextField() - customer_id = models.CharField(max_length=10) - order_ip = models.GenericIPAddressField() + order_comments = models.TextField(null=True) + customer_id = models.CharField(max_length=20, null=True) + order_ip = models.GenericIPAddressField(null=True) order_items = models.IntegerField() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..41c2c6d --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +django +pytz