misc: fix warnings about unclosed files (#51516)
This commit is contained in:
parent
7bb4127fc7
commit
7f21d927d4
|
@ -31,7 +31,8 @@ def test_runjobs_command(db):
|
|||
|
||||
def test_runscript_command(tmpdir):
|
||||
script_path = '%s/script.py' % str(tmpdir)
|
||||
open(script_path, 'w').write('# N/A')
|
||||
with open(script_path, 'w') as fd:
|
||||
fd.write('# N/A')
|
||||
call_command('runscript', script_path)
|
||||
|
||||
|
||||
|
|
|
@ -52,13 +52,14 @@ class SynchronizeFederationsImport(object):
|
|||
self.delete_count = 0
|
||||
self.unknown_count = 0
|
||||
max_federation = 0
|
||||
for i, line in enumerate(default_storage.open(self.action.csv_filepath, 'r')):
|
||||
line = line.strip()
|
||||
self.federations.append(line)
|
||||
try:
|
||||
max_federation = max(max_federation, int(line))
|
||||
except ValueError as e:
|
||||
pass
|
||||
with default_storage.open(self.action.csv_filepath, 'r') as fd:
|
||||
for i, line in enumerate(fd):
|
||||
line = line.strip()
|
||||
self.federations.append(line)
|
||||
try:
|
||||
max_federation = max(max_federation, int(line))
|
||||
except ValueError:
|
||||
pass
|
||||
self.federations.sort()
|
||||
app_id = self.action.app_id
|
||||
individus = Entity.objects.filter(
|
||||
|
@ -254,6 +255,7 @@ class SynchronizeFederationsAction(object):
|
|||
stream = getattr(self, prefix)
|
||||
if not stream:
|
||||
return None
|
||||
stream.close()
|
||||
url_name = 'admin:synchronize-federations-%s-%s' % (action, prefix.replace('_', '-'))
|
||||
filename = getattr(self, prefix + '_csv_filename')
|
||||
return reverse(url_name, kwargs={
|
||||
|
|
|
@ -116,21 +116,22 @@ def synchronize_federations_report(request, job_id, model_admin, *args, **kwargs
|
|||
report = job.action.report
|
||||
if not report:
|
||||
raise Http404('no report')
|
||||
text_report = report
|
||||
if six.PY3:
|
||||
text_report = io.TextIOWrapper(text_report, encoding='utf-8')
|
||||
reader = csv.reader(text_report)
|
||||
next(reader)
|
||||
actions = [row for row in reader if row[6] != 'KEEP']
|
||||
context = dict(
|
||||
model_admin.admin_site.each_context(request),
|
||||
title=job.created,
|
||||
job=job,
|
||||
csv=actions,
|
||||
csv_url=job.action.download_report_url,
|
||||
csv_filesize=report.size,
|
||||
)
|
||||
return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context)
|
||||
with report:
|
||||
text_report = report
|
||||
if six.PY3:
|
||||
text_report = io.TextIOWrapper(text_report, encoding='utf-8')
|
||||
reader = csv.reader(text_report)
|
||||
next(reader)
|
||||
actions = [row for row in reader if row[6] != 'KEEP']
|
||||
context = dict(
|
||||
model_admin.admin_site.each_context(request),
|
||||
title=job.created,
|
||||
job=job,
|
||||
csv=actions,
|
||||
csv_url=job.action.download_report_url,
|
||||
csv_filesize=report.size,
|
||||
)
|
||||
return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context)
|
||||
|
||||
|
||||
@permission_required('zoo_data.action1_entity')
|
||||
|
@ -148,23 +149,24 @@ def synchronize_federations_apply_report(request, job_id, model_admin, *args, **
|
|||
jobs = SynchronizeFederationsAction.get_jobs()
|
||||
job = get_object_or_404(jobs, id=job_id)
|
||||
report = job.action.apply_report
|
||||
if not report:
|
||||
raise Http404('no report')
|
||||
text_report = report
|
||||
if six.PY3:
|
||||
text_report = io.TextIOWrapper(text_report, encoding='utf-8')
|
||||
reader = csv.reader(text_report)
|
||||
next(reader)
|
||||
actions = [row for row in reader if row[6] != 'KEEP']
|
||||
context = dict(
|
||||
model_admin.admin_site.each_context(request),
|
||||
title=u'Application - %s' % job.created,
|
||||
job=job,
|
||||
csv=actions,
|
||||
csv_url=job.action.download_apply_report_url,
|
||||
csv_filesize=report.size,
|
||||
)
|
||||
return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context)
|
||||
with report:
|
||||
if not report:
|
||||
raise Http404('no report')
|
||||
text_report = report
|
||||
if six.PY3:
|
||||
text_report = io.TextIOWrapper(text_report, encoding='utf-8')
|
||||
reader = csv.reader(text_report)
|
||||
next(reader)
|
||||
actions = [row for row in reader if row[6] != 'KEEP']
|
||||
context = dict(
|
||||
model_admin.admin_site.each_context(request),
|
||||
title=u'Application - %s' % job.created,
|
||||
job=job,
|
||||
csv=actions,
|
||||
csv_url=job.action.download_apply_report_url,
|
||||
csv_filesize=report.size,
|
||||
)
|
||||
return TemplateResponse(request, "admin/zoo_data/entity/synchronize_federations_report.html", context)
|
||||
|
||||
|
||||
@permission_required('zoo_data.action1_entity')
|
||||
|
|
Loading…
Reference in New Issue