annotate core/functions.py @ 694:d84aaf239182

For #53, use HTML5 DOCTYPE. Some markup cleanup also.
author Brian Neal <bgneal@gmail.com>
date Fri, 06 Sep 2013 21:50:53 -0500
parents ee87ea74d46b
children e888d627928f
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 from django.contrib.sites.models import Site
gremmie@1 7 from django.conf import settings
bgneal@522 8 import django.core.mail
gremmie@1 9
bgneal@513 10 import core.tasks
bgneal@181 11
gremmie@1 12
bgneal@522 13 def send_mail(subject, message, from_email, recipient_list, defer=True, **kwargs):
gremmie@1 14 """
bgneal@513 15 The main send email function. Use this function to send email from the
bgneal@513 16 site. All applications should use this function instead of calling
bgneal@513 17 Django's directly.
bgneal@522 18 If defer is True, the email will be sent to a Celery task to actually send
bgneal@522 19 the email. Otherwise it is sent on the caller's thread. In any event, the
bgneal@513 20 email will be logged at the DEBUG level.
bgneal@513 21
bgneal@513 22 """
bgneal@513 23 # Guard against empty email addresses
bgneal@418 24 recipient_list = [dest for dest in recipient_list if dest]
bgneal@418 25 if not recipient_list:
bgneal@418 26 logging.warning("Empty recipient_list in send_mail")
bgneal@418 27 return
gremmie@1 28
bgneal@316 29 logging.debug('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s',
bgneal@316 30 from_email, str(recipient_list), subject, message)
gremmie@1 31
bgneal@522 32 if defer:
bgneal@513 33 core.tasks.send_mail.delay(subject, message, from_email, recipient_list,
bgneal@513 34 **kwargs)
bgneal@522 35 else:
bgneal@522 36 django.core.mail.send_mail(subject, message, from_email, recipient_list,
bgneal@522 37 **kwargs)
bgneal@513 38
gremmie@1 39
gremmie@1 40 def email_admins(subject, message):
gremmie@1 41 """Emails the site admins. Goes through the site send_mail function."""
gremmie@1 42 site = Site.objects.get_current()
gremmie@1 43 subject = '[%s] %s' % (site.name, subject)
bgneal@316 44 send_mail(subject,
bgneal@316 45 message,
gremmie@1 46 '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain),
gremmie@1 47 [mail_tuple[1] for mail_tuple in settings.ADMINS])
gremmie@1 48
gremmie@1 49
gremmie@1 50 def email_managers(subject, message):
gremmie@1 51 """Emails the site managers. Goes through the site send_mail function."""
gremmie@1 52 site = Site.objects.get_current()
gremmie@1 53 subject = '[%s] %s' % (site.name, subject)
bgneal@316 54 send_mail(subject,
bgneal@316 55 msg,
gremmie@1 56 '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain),
gremmie@1 57 [mail_tuple[1] for mail_tuple in settings.MANAGERS])
gremmie@1 58
gremmie@1 59
gremmie@1 60 def get_full_name(user):
gremmie@1 61 """Returns the user's full name if available, otherwise falls back
gremmie@1 62 to the username."""
gremmie@1 63 full_name = user.get_full_name()
gremmie@1 64 if full_name:
gremmie@1 65 return full_name
gremmie@1 66 return user.username
bgneal@9 67
bgneal@176 68
bgneal@176 69 BASE_YEAR = 2010
bgneal@176 70
bgneal@176 71 def copyright_str():
bgneal@176 72 curr_year = datetime.datetime.now().year
bgneal@176 73 if curr_year == BASE_YEAR:
bgneal@176 74 year_range = str(BASE_YEAR)
bgneal@176 75 else:
bgneal@176 76 year_range = "%d - %d" % (BASE_YEAR, curr_year)
bgneal@176 77
bgneal@176 78 return 'Copyright (C) %s, SurfGuitar101.com' % year_range
bgneal@227 79
bgneal@227 80
bgneal@227 81 IP_PAT = re.compile('(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})')
bgneal@227 82
bgneal@227 83 def get_ip(request):
bgneal@227 84 """Returns the IP from the request or None if it cannot be retrieved."""
bgneal@227 85 ip = request.META.get('HTTP_X_FORWARDED_FOR',
bgneal@227 86 request.META.get('REMOTE_ADDR'))
bgneal@227 87
bgneal@227 88 if ip:
bgneal@227 89 match = IP_PAT.match(ip)
bgneal@227 90 ip = match.group(1) if match else None
bgneal@227 91
bgneal@227 92 return ip
bgneal@241 93
bgneal@241 94
bgneal@241 95 def get_page(qdict):
bgneal@241 96 """Attempts to retrieve the value for "page" from the given query dict and
bgneal@241 97 return it as an integer. If the key cannot be found or converted to an
bgneal@241 98 integer, 1 is returned.
bgneal@241 99 """
bgneal@241 100 n = qdict.get('page', 1)
bgneal@241 101 try:
bgneal@241 102 n = int(n)
bgneal@241 103 except ValueError:
bgneal@241 104 n = 1
bgneal@241 105 return n
bgneal@566 106
bgneal@566 107
bgneal@566 108 def quote_message(who, message):
bgneal@566 109 """
bgneal@566 110 Builds a message reply by quoting the existing message in a
bgneal@566 111 typical email-like fashion. The quoting is compatible with Markdown.
bgneal@566 112 """
bgneal@566 113 msg = "> %s" % message.replace('\n', '\n> ')
bgneal@566 114 if msg.endswith('\n> '):
bgneal@566 115 msg = msg[:-2]
bgneal@566 116
bgneal@566 117 return "*%s wrote:*\n\n%s\n\n" % (who, msg)