diff core/functions.py @ 700:e888d627928f

Refactored the processing of image uploads. I suspect I will use this general algorithm in other places (like POTD), so I made it reusable.
author Brian Neal <bgneal@gmail.com>
date Wed, 11 Sep 2013 20:31:23 -0500
parents ee87ea74d46b
children 97f8fab9b1a3
line wrap: on
line diff
--- a/core/functions.py	Mon Sep 09 20:53:08 2013 -0500
+++ b/core/functions.py	Wed Sep 11 20:31:23 2013 -0500
@@ -1,7 +1,9 @@
-"""This file houses various core utility functions for GPP"""
+"""This file houses various core utility functions"""
+from contextlib import contextmanager
 import datetime
+import logging
+import os
 import re
-import logging
 
 from django.contrib.sites.models import Site
 from django.conf import settings
@@ -10,6 +12,17 @@
 import core.tasks
 
 
+@contextmanager
+def temp_open(path, mode):
+    """A context manager for closing and removing temporary files."""
+    fp = open(path, mode)
+    try:
+        yield fp
+    finally:
+        fp.close()
+        os.remove(path)
+
+
 def send_mail(subject, message, from_email, recipient_list, defer=True, **kwargs):
     """
     The main send email function. Use this function to send email from the
@@ -52,7 +65,7 @@
     site = Site.objects.get_current()
     subject = '[%s] %s' % (site.name, subject)
     send_mail(subject,
-            msg,
+            message,
             '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain),
             [mail_tuple[1] for mail_tuple in settings.MANAGERS])