comparison 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
comparison
equal deleted inserted replaced
699:d33bedc3be74 700:e888d627928f
1 """This file houses various core utility functions for GPP""" 1 """This file houses various core utility functions"""
2 from contextlib import contextmanager
2 import datetime 3 import datetime
4 import logging
5 import os
3 import re 6 import re
4 import logging
5 7
6 from django.contrib.sites.models import Site 8 from django.contrib.sites.models import Site
7 from django.conf import settings 9 from django.conf import settings
8 import django.core.mail 10 import django.core.mail
9 11
10 import core.tasks 12 import core.tasks
13
14
15 @contextmanager
16 def temp_open(path, mode):
17 """A context manager for closing and removing temporary files."""
18 fp = open(path, mode)
19 try:
20 yield fp
21 finally:
22 fp.close()
23 os.remove(path)
11 24
12 25
13 def send_mail(subject, message, from_email, recipient_list, defer=True, **kwargs): 26 def send_mail(subject, message, from_email, recipient_list, defer=True, **kwargs):
14 """ 27 """
15 The main send email function. Use this function to send email from the 28 The main send email function. Use this function to send email from the
50 def email_managers(subject, message): 63 def email_managers(subject, message):
51 """Emails the site managers. Goes through the site send_mail function.""" 64 """Emails the site managers. Goes through the site send_mail function."""
52 site = Site.objects.get_current() 65 site = Site.objects.get_current()
53 subject = '[%s] %s' % (site.name, subject) 66 subject = '[%s] %s' % (site.name, subject)
54 send_mail(subject, 67 send_mail(subject,
55 msg, 68 message,
56 '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain), 69 '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain),
57 [mail_tuple[1] for mail_tuple in settings.MANAGERS]) 70 [mail_tuple[1] for mail_tuple in settings.MANAGERS])
58 71
59 72
60 def get_full_name(user): 73 def get_full_name(user):