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