family: read and write files on binary mode (#38781)
This commit is contained in:
parent
4201af31c2
commit
c28cf20668
|
@ -56,7 +56,7 @@ class Command(BaseCommand):
|
|||
|
||||
try:
|
||||
archive_name = os.path.basename(options['archive_file'])
|
||||
connector.archive.save(archive_name, File(file(options['archive_file'])))
|
||||
connector.archive.save(archive_name, File(open(options['archive_file'], 'rb')))
|
||||
except Exception as e:
|
||||
raise CommandError('Error occured: %s' % e)
|
||||
finally:
|
||||
|
|
|
@ -250,7 +250,7 @@ class GenericFamily(BaseResource):
|
|||
Invoice.objects.update_or_create(resource=self,
|
||||
family=family, external_id=invoice['id'], defaults=data)
|
||||
if 'invoices/%s.pdf' % invoice['id'] in archive_files:
|
||||
with open(os.path.join(invoices_dir, '%s.pdf' % invoice['id']), 'w') as fp:
|
||||
with open(os.path.join(invoices_dir, '%s.pdf' % invoice['id']), 'wb') as fp:
|
||||
fp.write(archive.read('invoices/%s.pdf' % invoice['id']))
|
||||
|
||||
# cleanup invoices
|
||||
|
@ -494,10 +494,10 @@ class Invoice(models.Model):
|
|||
if not self.has_pdf:
|
||||
raise Http404(_('PDF file not found'))
|
||||
|
||||
response = HttpResponse(open(self.pdf_filename()).read(), content_type='application/pdf')
|
||||
response = HttpResponse(open(self.pdf_filename(), 'rb').read(), content_type='application/pdf')
|
||||
response['Content-Disposition'] = 'attachment; filename=%s.pdf' % self.external_id
|
||||
return response
|
||||
|
||||
def write_pdf(self, contents):
|
||||
with open(self.pdf_filename(), 'w') as fp:
|
||||
with open(self.pdf_filename(), 'wb') as fp:
|
||||
fp.write(contents)
|
||||
|
|
|
@ -34,7 +34,7 @@ API_KEY = 'family'
|
|||
@pytest.fixture
|
||||
def resource(db):
|
||||
filepath = os.path.join(os.path.dirname(__file__), 'data', 'family_data.zip')
|
||||
with open(filepath) as fd:
|
||||
with open(filepath, 'rb') as fd:
|
||||
resource = GenericFamily.objects.create(title='test', slug='test', archive=File(fd, 'family_data.zip'))
|
||||
api = ApiUser.objects.create(username='family', keytype='API', key=API_KEY)
|
||||
obj_type = ContentType.objects.get_for_model(GenericFamily)
|
||||
|
@ -202,7 +202,7 @@ def test_pay_invoice(app, resource):
|
|||
def test_fondettes_concerto_loader():
|
||||
Invoice.objects.all().delete()
|
||||
filepath = os.path.join(os.path.dirname(__file__), 'data', 'family_data_fondettes.zip')
|
||||
with open(filepath) as fd:
|
||||
with open(filepath, 'rb') as fd:
|
||||
resource = GenericFamily.objects.create(title='test fondettes',
|
||||
slug='test-fondettes', archive=File(fd, 'family_data_fondettes.zip'),
|
||||
file_format='concerto_fondettes')
|
||||
|
@ -220,7 +220,7 @@ def test_archive_validation():
|
|||
|
||||
def test_fondettes_concerto_validation():
|
||||
filepath = os.path.join(os.path.dirname(__file__), 'data', 'family_data_fondettes.zip')
|
||||
with open(filepath) as fd:
|
||||
with open(filepath, 'rb') as fd:
|
||||
resource = GenericFamily.objects.create(title='test fondettes',
|
||||
slug='test fondettes', archive=File(fd, 'family_data_fondettes.zip'), file_format='concerto_fondettes')
|
||||
resource.clean()
|
||||
|
@ -236,7 +236,7 @@ def test_orleans_concerto_loader():
|
|||
filepath = os.path.join(os.path.dirname(__file__), 'data', 'orleans',
|
||||
'family_data_orleans.zip')
|
||||
resource = GenericFamily(title='test orleans',
|
||||
slug='test-orleans', archive=File(open(filepath), 'family_data_orleans.zip'), file_format='concerto_orleans')
|
||||
slug='test-orleans', archive=File(open(filepath, 'rb'), 'family_data_orleans.zip'), file_format='concerto_orleans')
|
||||
from passerelle.apps.family.loaders.concerto_orleans import Loader
|
||||
loader = Loader(resource)
|
||||
loader.archive = zipfile.ZipFile(filepath)
|
||||
|
@ -282,7 +282,6 @@ def test_orleans_concerto_loader():
|
|||
assert len([f for f in families.values() if f['invoices']]) == 4
|
||||
# and 14 families with no invoices
|
||||
assert len([f for f in families.values() if not f['invoices']]) == 14
|
||||
|
||||
resource.save()
|
||||
|
||||
assert Family.objects.filter(resource=resource).count() == 18
|
||||
|
@ -298,7 +297,7 @@ def test_orleans_data_import_command():
|
|||
cur_dir = os.path.dirname(__file__)
|
||||
data_dir = os.path.join(cur_dir, 'data', 'orleans')
|
||||
|
||||
with open(os.path.join(data_dir, 'family_data_orleans.zip')) as fd:
|
||||
with open(os.path.join(data_dir, 'family_data_orleans.zip'), 'rb') as fd:
|
||||
resource = GenericFamily.objects.create(title='test orleans',
|
||||
archive=File(fd, 'family_data_orleans.zip'),
|
||||
slug='test-orleans', file_format='concerto_orleans')
|
||||
|
@ -353,7 +352,7 @@ def test_incorrect_orleans_data(caplog):
|
|||
filepath = os.path.join(os.path.dirname(__file__), 'data',
|
||||
'family_incorrect_data_orleans.zip')
|
||||
resource = GenericFamily.objects.create(title='test orleans',
|
||||
slug='test-orleans', archive=File(open(filepath), 'family_incorrect_data_orleans.zip'),
|
||||
slug='test-orleans', archive=File(open(filepath, 'rb'), 'family_incorrect_data_orleans.zip'),
|
||||
file_format='concerto_orleans')
|
||||
for record in caplog.records:
|
||||
assert 'Error occured while importing data:' in record.message
|
||||
|
|
Loading…
Reference in New Issue