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