summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2020-01-13 18:42:32 +0100
committerÉtienne Loks <etienne.loks@iggdrasil.net>2020-01-13 18:42:32 +0100
commit0f9d54990a618c3c2908ff235299df7a64d29dd2 (patch)
treec2b9988a5d380b5518320c42d008f8c8e734d5e8 /ishtar_common/models.py
parentd2c210949ef59c138b9028e13ed2b1735e586dc7 (diff)
downloadIshtar-0f9d54990a618c3c2908ff235299df7a64d29dd2.tar.bz2
Ishtar-0f9d54990a618c3c2908ff235299df7a64d29dd2.zip
Close opened files
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r--ishtar_common/models.py16
1 files changed, 10 insertions, 6 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 831479cd4..28ffcf7b2 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -1164,6 +1164,7 @@ class ImageModel(models.Model, ImageContainerModel):
filename = os.path.splitext(os.path.split(self.image.name)[-1])[0]
old_path = self.image.path
filename = "%s.jpg" % filename
+ image = None
try:
image = Image.open(self.image.file)
# convert to RGB
@@ -1191,6 +1192,9 @@ class ImageModel(models.Model, ImageContainerModel):
save=False)
except IOError:
pass
+ finally:
+ if image:
+ image.close()
return super(ImageModel, self).save(*args, **kwargs)
def _get_thumb_name(self, filename):
@@ -1804,8 +1808,8 @@ class QRCodeItem(models.Model, ImageContainerModel):
tmpdir_created = True
filename = tmpdir + os.sep + 'qrcode.png'
qr.png(filename, scale=settings.ISHTAR_QRCODE_SCALE)
- self.qrcode.save(
- "qrcode.png", File(open(filename, 'rb')))
+ with open(filename, 'rb') as qrfile:
+ self.qrcode.save("qrcode.png", File(qrfile))
self.skip_history_when_saving = True
self._no_move = True
self.save()
@@ -3571,8 +3575,8 @@ class DocumentTemplate(models.Model):
raise TemplateSyntaxError(str(e), 0)
except Exception as e:
raise TemplateSyntaxError(str(e), 0)
- output = open(output_name, 'wb')
- output.write(result)
+ with open(output_name, 'wb') as output:
+ output.write(result)
return output_name
def publish_labels(self, objects):
@@ -3602,8 +3606,8 @@ class DocumentTemplate(models.Model):
raise TemplateSyntaxError(str(e), e.lineno)
output_name = main_output_name + "-" + str(idx) + suffix
names.append(output_name)
- output = open(output_name, 'wb')
- output.write(result)
+ with open(output_name, 'wb') as output:
+ output.write(result)
output_name = main_output_name + suffix
o = OOoPy(infile=names[0], outfile=output_name)
if len(names) > 1: