comparison core/functions.py @ 964:51a2051588f5

Image uploading now expects a file. Refactor image uploading to not expect a Django UploadedFile and use a regular file instead. This will be needed for the future feature of being able to save and upload images from the Internet.
author Brian Neal <bgneal@gmail.com>
date Wed, 02 Sep 2015 20:50:08 -0500
parents 79a71b9d0a2a
children f5aa74dcdd7a
comparison
equal deleted inserted replaced
963:4619290d171d 964:51a2051588f5
1 """This file houses various core utility functions""" 1 """This file houses various core utility functions"""
2 from contextlib import contextmanager
3 import datetime 2 import datetime
4 import logging 3 import logging
5 import os 4 import os
6 import re 5 import re
6 import tempfile
7 7
8 from django.contrib.sites.models import Site 8 from django.contrib.sites.models import Site
9 from django.conf import settings 9 from django.conf import settings
10 import django.core.mail 10 import django.core.mail
11 11
12 import core.tasks 12 import core.tasks
13 13
14 14
15 @contextmanager 15 class TemporaryFile(object):
16 def temp_open(path, mode): 16 """Context manager class for working with temporary files.
17 """A context manager for closing and removing temporary files.""" 17
18 fp = open(path, mode) 18 The file will be closed and removed when the context is exited.
19 try: 19 """
20 yield fp 20 def __init__(self, **kwargs):
21 finally: 21 """kwargs will be passed to mkstemp."""
22 fp.close() 22 self.kwargs = kwargs
23 os.remove(path) 23
24 def __enter__(self):
25 self.fd, self.filename = tempfile.mkstemp(**self.kwargs)
26 self.file = os.fdopen(self.fd, 'w+b')
27 return self
28
29 def __exit__(self, exc_type, exc_value, traceback):
30 self.file.close()
31 os.remove(self.filename)
24 32
25 33
26 def send_mail(subject, message, from_email, recipient_list, reply_to=None, defer=True): 34 def send_mail(subject, message, from_email, recipient_list, reply_to=None, defer=True):
27 """ 35 """
28 The main send email function. Use this function to send email from the 36 The main send email function. Use this function to send email from the