comparison gpp/core/functions.py @ 1:dbd703f7d63a

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