0001-family-always-create-invoices-with-orleans-loader-15.patch
passerelle/apps/family/loaders/concerto_orleans.py | ||
---|---|---|
168 | 168 | |
169 | 169 |
for invoice_data in family_data.get('invoices') or []: |
170 | 170 |
invoice_filename = self.invoice_filename_pattern % invoice_data['external_id'] |
171 |
# create invoice object only if associated pdf exists |
|
171 |
invoice, created = Invoice.objects.update_or_create(resource=self.connector, |
|
172 |
family=family, external_id=invoice_data['external_id'], defaults=invoice_data) |
|
172 | 173 |
if invoice_filename in archive.namelist(): |
173 |
invoice, created = Invoice.objects.update_or_create(resource=self.connector, |
|
174 |
family=family, external_id=invoice_data['external_id'], defaults=invoice_data) |
|
175 | 174 |
invoice.write_pdf(archive.read(invoice_filename)) |
176 | 175 | |
177 | 176 |
except Exception, e: |
passerelle/apps/family/management/commands/import_orleans_data.py | ||
---|---|---|
24 | 24 |
from django.core.files.base import File |
25 | 25 |
from django.core.management.base import BaseCommand, CommandError |
26 | 26 | |
27 |
from family.models import GenericFamily |
|
27 |
from family.models import GenericFamily, Invoice
|
|
28 | 28 | |
29 | 29 |
LOCK_FILENAME = os.path.join(tempfile.gettempdir(), 'import-orleans-data.lock') |
30 | 30 | |
... | ... | |
62 | 62 |
try: |
63 | 63 |
fd = open(LOCK_FILENAME, 'w') |
64 | 64 |
fcntl.lockf(fd, fcntl.LOCK_EX|fcntl.LOCK_NB) |
65 |
invoices_dir = os.path.join(options['data_dir'], 'factures') |
|
66 |
with zipfile.ZipFile(archive_path, 'a') as archive: |
|
67 | ||
68 |
for invoice_file in os.listdir(invoices_dir): |
|
69 |
if invoice_file.startswith('facture_'): |
|
70 |
archive.write(os.path.join(invoices_dir, invoice_file), |
|
71 |
'invoices/%s' % invoice_file) |
|
72 | 65 |
connector.archive.save(options['archive_name'], File(file(archive_path))) |
66 |
invoices_dir = os.path.join(options['data_dir'], 'factures') |
|
67 |
if not os.path.exists(invoices_dir): |
|
68 |
raise Exception('Directory "%s" does not exist' % invoices_dir) |
|
69 |
for invoice in connector.invoice_set.all(): |
|
70 |
invoice_file_path = os.path.join(invoices_dir, 'facture_%s.pdf' % invoice.external_id) |
|
71 |
if os.path.exists(invoice_file_path): |
|
72 |
invoice.write_pdf(file(invoice_file_path).read()) |
|
73 | 73 |
except IOError: |
74 | 74 |
raise CommandError('Command already running.') |
75 | 75 |
except Exception, e: |
tests/test_family.py | ||
---|---|---|
310 | 310 |
call_command('import_orleans_data', data_dir=data_dir, |
311 | 311 |
archive_name='family_data_orleans.zip', |
312 | 312 |
connector='test-orleans') |
313 |
assert "No such file or directory: '%s/factures'" % data_dir in str(error.value)
|
|
313 |
assert 'Directory "%s/factures" does not exist' % data_dir in str(error.value)
|
|
314 | 314 | |
315 | 315 |
# create temporary expected dir for invoices |
316 | 316 |
invoices_dir = os.path.join(data_dir, 'factures') |
317 | 317 |
os.mkdir(invoices_dir) |
318 |
Family.objects.filter(resource=resource).delete() |
|
319 |
Invoice.objects.filter(resource=resource).delete() |
|
318 | 320 |
call_command('import_orleans_data', data_dir=data_dir, |
319 | 321 |
archive_name='family_data_orleans.zip', |
320 | 322 |
connector='test-orleans') |
321 |
- |