gremmie@1: """This file houses various core utility functions for GPP""" gremmie@1: gremmie@1: import django.core.mail gremmie@1: from django.contrib.sites.models import Site gremmie@1: from django.conf import settings gremmie@1: gremmie@1: from core import logging gremmie@1: from lxml.html.clean import Cleaner gremmie@1: gremmie@1: html_cleaner = Cleaner(scripts=True, gremmie@1: javascript=True, gremmie@1: comments=True, gremmie@1: style=True, gremmie@1: links=True, gremmie@1: meta=True, gremmie@1: page_structure=True, gremmie@1: processing_instructions=True, gremmie@1: embedded=True, gremmie@1: frames=True, gremmie@1: forms=True, gremmie@1: annoying_tags=True, gremmie@1: remove_unknown_tags=True, gremmie@1: safe_attrs_only=True, gremmie@1: host_whitelist=['www.youtube.com'], gremmie@1: whitelist_tags=['object', 'param', 'embed'], gremmie@1: ) gremmie@1: gremmie@1: gremmie@1: def send_mail(subject, message, from_email, recipient_list, gremmie@1: fail_silently = False, auth_user = None, auth_password = None): gremmie@1: """The main gpp send email function. gremmie@1: Use this function to send email from the site. It will obey debug settings and gremmie@1: log all emails. gremmie@1: """ gremmie@1: gremmie@1: if settings.GPP_SEND_EMAIL: gremmie@1: django.core.mail.send_mail(subject, message, from_email, recipient_list, gremmie@1: fail_silently, auth_user, auth_password) gremmie@1: gremmie@1: logging.info('EMAIL:\nFrom: %s\nTo: %s\nSubject: %s\nMessage:\n%s' % gremmie@1: (from_email, str(recipient_list), subject, message)) gremmie@1: gremmie@1: gremmie@1: def email_admins(subject, message): gremmie@1: """Emails the site admins. Goes through the site send_mail function.""" gremmie@1: site = Site.objects.get_current() gremmie@1: subject = '[%s] %s' % (site.name, subject) gremmie@1: send_mail(subject, gremmie@1: message, gremmie@1: '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain), gremmie@1: [mail_tuple[1] for mail_tuple in settings.ADMINS]) gremmie@1: gremmie@1: gremmie@1: def email_managers(subject, message): gremmie@1: """Emails the site managers. Goes through the site send_mail function.""" gremmie@1: site = Site.objects.get_current() gremmie@1: subject = '[%s] %s' % (site.name, subject) gremmie@1: send_mail(subject, gremmie@1: msg, gremmie@1: '%s@%s' % (settings.GPP_NO_REPLY_EMAIL, site.domain), gremmie@1: [mail_tuple[1] for mail_tuple in settings.MANAGERS]) gremmie@1: gremmie@1: gremmie@1: def clean_html(s): gremmie@1: """Cleans HTML of dangerous tags and content.""" gremmie@1: if s: gremmie@1: return html_cleaner.clean_html(s) gremmie@1: return s gremmie@1: gremmie@1: gremmie@1: def get_full_name(user): gremmie@1: """Returns the user's full name if available, otherwise falls back gremmie@1: to the username.""" gremmie@1: full_name = user.get_full_name() gremmie@1: if full_name: gremmie@1: return full_name gremmie@1: return user.username