annotate gpp/core/functions.py @ 499:1a09a7bea000

For #236, switch to a settings package scheme.
author Brian Neal <bgneal@gmail.com>
date Wed, 30 Nov 2011 02:41:18 +0000
parents 20af29454e83
children 9e7ae8462f3f
rev   line source
gremmie@1 1 """This file houses various core utility functions for GPP"""
bgneal@176 2 import datetime
bgneal@227 3 import re
bgneal@316 4 import logging
gremmie@1 5
gremmie@1 6 import django.core.mail
gremmie@1 7 from django.contrib.sites.models import Site
gremmie@1 8 from django.conf import settings
gremmie@1 9
bgneal@181 10 import mailer
bgneal@181 11
gremmie@1 12
bgneal@252 13 def send_mail(subject, message, from_email, recipient_list,
bgneal@252 14 fail_silently=False, auth_user=None, auth_password=None,
bgneal@252 15 expedite=False):
gremmie@1 16 """The main gpp send email function.
gremmie@1 17 Use this function to send email from the site. It will obey debug settings and
bgneal@252 18 log all emails. The expedite flag, when True, will bypass the mail queue.
gremmie@1 19 """
bgneal@418 20 recipient_list = [dest for dest in recipient_list if dest]
bgneal@418 21 if not recipient_list:
bgneal@418 22 logging.warning("Empty recipient_list in send_mail")
bgneal@418 23 return
gremmie@1 24
bgneal@252 25 if settings.MAILER_ENQUEUE_MAIL and not expedite:
bgneal@180 26 mailer.enqueue_mail(subject, message, from_email, recipient_list)
bgneal@180 27 elif settings.GPP_SEND_EMAIL:
gremmie@1 28 django.core.mail.send_mail(subject, message, from_email, recipient_list,
gremmie@1 29 fail_silently, auth_user, auth_password)
gremmie@1 30
bgneal@316 31 logging.debug('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s',
bgneal@316 32 from_email, str(recipient_list), subject, message)
gremmie@1 33
gremmie@1 34
gremmie@1 35 def email_admins(subject, message):
gremmie@1 36 """Emails the site admins. Goes through the site send_mail function."""
gremmie@1 37 site = Site.objects.get_current()
gremmie@1 38 subject = '[%s] %s' % (site.name, subject)
bgneal@316 39 send_mail(subject,
bgneal@316 40 message,
gremmie@1 41 '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain),
gremmie@1 42 [mail_tuple[1] for mail_tuple in settings.ADMINS])
gremmie@1 43
gremmie@1 44
gremmie@1 45 def email_managers(subject, message):
gremmie@1 46 """Emails the site managers. Goes through the site send_mail function."""
gremmie@1 47 site = Site.objects.get_current()
gremmie@1 48 subject = '[%s] %s' % (site.name, subject)
bgneal@316 49 send_mail(subject,
bgneal@316 50 msg,
gremmie@1 51 '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain),
gremmie@1 52 [mail_tuple[1] for mail_tuple in settings.MANAGERS])
gremmie@1 53
gremmie@1 54
gremmie@1 55 def get_full_name(user):
gremmie@1 56 """Returns the user's full name if available, otherwise falls back
gremmie@1 57 to the username."""
gremmie@1 58 full_name = user.get_full_name()
gremmie@1 59 if full_name:
gremmie@1 60 return full_name
gremmie@1 61 return user.username
bgneal@9 62
bgneal@176 63
bgneal@176 64 BASE_YEAR = 2010
bgneal@176 65
bgneal@176 66 def copyright_str():
bgneal@176 67 curr_year = datetime.datetime.now().year
bgneal@176 68 if curr_year == BASE_YEAR:
bgneal@176 69 year_range = str(BASE_YEAR)
bgneal@176 70 else:
bgneal@176 71 year_range = "%d - %d" % (BASE_YEAR, curr_year)
bgneal@176 72
bgneal@176 73 return 'Copyright (C) %s, SurfGuitar101.com' % year_range
bgneal@227 74
bgneal@227 75
bgneal@227 76 IP_PAT = re.compile('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
bgneal@227 77
bgneal@227 78 def get_ip(request):
bgneal@227 79 """Returns the IP from the request or None if it cannot be retrieved."""
bgneal@227 80 ip = request.META.get('HTTP_X_FORWARDED_FOR',
bgneal@227 81 request.META.get('REMOTE_ADDR'))
bgneal@227 82
bgneal@227 83 if ip:
bgneal@227 84 match = IP_PAT.match(ip)
bgneal@227 85 ip = match.group(1) if match else None
bgneal@227 86
bgneal@227 87 return ip
bgneal@241 88
bgneal@241 89
bgneal@241 90 def get_page(qdict):
bgneal@241 91 """Attempts to retrieve the value for "page" from the given query dict and
bgneal@241 92 return it as an integer. If the key cannot be found or converted to an
bgneal@241 93 integer, 1 is returned.
bgneal@241 94 """
bgneal@241 95 n = qdict.get('page', 1)
bgneal@241 96 try:
bgneal@241 97 n = int(n)
bgneal@241 98 except ValueError:
bgneal@241 99 n = 1
bgneal@241 100 return n