gremmie@1
|
1 """This file houses various core utility functions for GPP"""
|
gremmie@1
|
2
|
gremmie@1
|
3 import django.core.mail
|
gremmie@1
|
4 from django.contrib.sites.models import Site
|
gremmie@1
|
5 from django.conf import settings
|
gremmie@1
|
6
|
gremmie@1
|
7 from core import logging
|
gremmie@1
|
8 from lxml.html.clean import Cleaner
|
gremmie@1
|
9
|
gremmie@1
|
10 html_cleaner = Cleaner(scripts=True,
|
gremmie@1
|
11 javascript=True,
|
gremmie@1
|
12 comments=True,
|
gremmie@1
|
13 style=True,
|
gremmie@1
|
14 links=True,
|
gremmie@1
|
15 meta=True,
|
gremmie@1
|
16 page_structure=True,
|
gremmie@1
|
17 processing_instructions=True,
|
gremmie@1
|
18 embedded=True,
|
gremmie@1
|
19 frames=True,
|
gremmie@1
|
20 forms=True,
|
gremmie@1
|
21 annoying_tags=True,
|
gremmie@1
|
22 remove_unknown_tags=True,
|
gremmie@1
|
23 safe_attrs_only=True,
|
gremmie@1
|
24 host_whitelist=['www.youtube.com'],
|
gremmie@1
|
25 whitelist_tags=['object', 'param', 'embed'],
|
gremmie@1
|
26 )
|
gremmie@1
|
27
|
gremmie@1
|
28
|
gremmie@1
|
29 def send_mail(subject, message, from_email, recipient_list,
|
gremmie@1
|
30 fail_silently = False, auth_user = None, auth_password = None):
|
gremmie@1
|
31 """The main gpp send email function.
|
gremmie@1
|
32 Use this function to send email from the site. It will obey debug settings and
|
gremmie@1
|
33 log all emails.
|
gremmie@1
|
34 """
|
gremmie@1
|
35
|
gremmie@1
|
36 if settings.GPP_SEND_EMAIL:
|
gremmie@1
|
37 django.core.mail.send_mail(subject, message, from_email, recipient_list,
|
gremmie@1
|
38 fail_silently, auth_user, auth_password)
|
gremmie@1
|
39
|
gremmie@1
|
40 logging.info('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s' %
|
gremmie@1
|
41 (from_email, str(recipient_list), subject, message))
|
gremmie@1
|
42
|
gremmie@1
|
43
|
gremmie@1
|
44 def email_admins(subject, message):
|
gremmie@1
|
45 """Emails the site admins. Goes through the site send_mail function."""
|
gremmie@1
|
46 site = Site.objects.get_current()
|
gremmie@1
|
47 subject = '[%s] %s' % (site.name, subject)
|
gremmie@1
|
48 send_mail(subject,
|
gremmie@1
|
49 message,
|
gremmie@1
|
50 '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain),
|
gremmie@1
|
51 [mail_tuple[1] for mail_tuple in settings.ADMINS])
|
gremmie@1
|
52
|
gremmie@1
|
53
|
gremmie@1
|
54 def email_managers(subject, message):
|
gremmie@1
|
55 """Emails the site managers. Goes through the site send_mail function."""
|
gremmie@1
|
56 site = Site.objects.get_current()
|
gremmie@1
|
57 subject = '[%s] %s' % (site.name, subject)
|
gremmie@1
|
58 send_mail(subject,
|
gremmie@1
|
59 msg,
|
gremmie@1
|
60 '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain),
|
gremmie@1
|
61 [mail_tuple[1] for mail_tuple in settings.MANAGERS])
|
gremmie@1
|
62
|
gremmie@1
|
63
|
gremmie@1
|
64 def clean_html(s):
|
gremmie@1
|
65 """Cleans HTML of dangerous tags and content."""
|
gremmie@1
|
66 if s:
|
gremmie@1
|
67 return html_cleaner.clean_html(s)
|
gremmie@1
|
68 return s
|
gremmie@1
|
69
|
gremmie@1
|
70
|
gremmie@1
|
71 def get_full_name(user):
|
gremmie@1
|
72 """Returns the user's full name if available, otherwise falls back
|
gremmie@1
|
73 to the username."""
|
gremmie@1
|
74 full_name = user.get_full_name()
|
gremmie@1
|
75 if full_name:
|
gremmie@1
|
76 return full_name
|
gremmie@1
|
77 return user.username
|