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