work on import_csv command, added ordering to models
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
import csv
|
||||
import collections
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
@@ -5,7 +6,7 @@ from core.models import Enregistrement, Etiquette
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Importe des enregistrement à partir d'un fichier .csv"
|
||||
mapping = ("date", "description", "etiquette", "montant")
|
||||
mapping = ("year", "month", "day", "etiquette", "description", "montant")
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument("files", nargs="+", type=str)
|
||||
@@ -18,9 +19,11 @@ class Command(BaseCommand):
|
||||
header += " ==\n%s" % (self.mapping,)
|
||||
self.stdout.write(self.style.MIGRATE_HEADING(header))
|
||||
|
||||
# Find recors in each files
|
||||
for filepath in options['files']:
|
||||
with open(filepath, 'r') as f:
|
||||
self.stdout.write(self.style.WARNING("[-] %s " % filepath), ending='')
|
||||
# Get all data from file
|
||||
self.stdout.write(self.style.WARNING("[-] %s. " % filepath), ending='')
|
||||
reader = csv.reader(f)
|
||||
results = list()
|
||||
for row in reader:
|
||||
@@ -28,18 +31,34 @@ class Command(BaseCommand):
|
||||
for i, field in enumerate(self.mapping):
|
||||
result[field] = row[i]
|
||||
results.append(result)
|
||||
self.stdout.write(self.style.SUCCESS("Ok (%i results):" % len(results)))
|
||||
|
||||
self.stdout.write(self.style.SUCCESS("Ok (found %i results)." % len(results)))
|
||||
# Then, create new objects if necessary
|
||||
self.errors = []
|
||||
for result in results:
|
||||
output = ", ".join(map(lambda t: "%s:%s" % (t[0][0:4], t[1]), result.items()))
|
||||
if not options['test']:
|
||||
# Création d'un nouvel enregistrement
|
||||
date = result['date'] # TODO: format date !
|
||||
date = datetime.date(int(result['year']), int(result['month']), int(result['day']))
|
||||
etiquette, created = Etiquette.objects.get_or_create(nom=result['etiquette'])
|
||||
Enregistrement.objects.create(date=result['date'],
|
||||
description=result['description'],
|
||||
etiquette=etiquette,
|
||||
montant=result['montant'])
|
||||
output = "+" + output
|
||||
if created: output += "(added %s)" % etiquette
|
||||
self.stdout.write(self.style.SUCCESS(" " + output))
|
||||
data = {'date':date,
|
||||
'description':str(result['description']),
|
||||
'etiquette':etiquette,
|
||||
'montant':float(result['montant'])}
|
||||
|
||||
output = "."
|
||||
formatter = self.style.WARNING
|
||||
if not options['test']:
|
||||
try: # Check that object does not already exists
|
||||
obj = Enregistrement.objects.get(**data)
|
||||
except Enregistrement.DoesNotExist:
|
||||
try:
|
||||
obj = Enregistrement(**data)
|
||||
obj.save()
|
||||
output = "+"
|
||||
formatter = self.style.SUCCESS
|
||||
except Exception as e:
|
||||
output = "E"
|
||||
formatter = self.style.ERROR
|
||||
if created:
|
||||
output += "(added %s)" % etiquette
|
||||
self.stdout.write(formatter(output), ending='')
|
||||
self.stdout.write('\n')
|
||||
for error in self.errors:
|
||||
self.stdout.write(self.style.ERROR('%s' % error))
|
||||
|
||||
@@ -81,6 +81,8 @@ class Enregistrement(models.Model):
|
||||
def __str__(self):
|
||||
return "<Depense: %s>" % self.etiquette
|
||||
|
||||
class Meta:
|
||||
ordering = ('date',)
|
||||
|
||||
class EnregistrementRecursif(models.Model):
|
||||
jour = models.IntegerField()
|
||||
@@ -89,6 +91,9 @@ class EnregistrementRecursif(models.Model):
|
||||
description = models.CharField(max_length=512)
|
||||
created_date = models.DateField()
|
||||
|
||||
class Meta:
|
||||
ordering = ('jour',)
|
||||
|
||||
def is_older(self, month, year):
|
||||
return (self.created_date.month <= month
|
||||
and self.created_date.year <= year)
|
||||
|
||||
Reference in New Issue
Block a user