def get_formatted_message(formats, notice_type, context, medium): """ Returns a dictionary with the format identifier as the key. The values are are fully rendered templates with the given context. """ if not isinstance(context, Context): context = Context(context or {}) protocol = 'https' if USE_SSL else 'http' current_site = Site.objects.get_current() base_url = u"%s://%s" % (protocol, unicode(current_site)) context.update({ 'base_url': base_url, 'site': current_site, }) format_templates = {} context = apply_context_processors(context) for format in formats: # conditionally turn off autoescaping for .txt extensions in format if format.endswith(".txt") or format.endswith(".html"): context.autoescape = False else: context.autoescape = True format_templates[format] = render_to_string(( 'notification/%s/%s/%s' % ( notice_type.template_slug, medium, format), 'notification/%s/%s' % (notice_type.template_slug, format), 'notification/%s/%s' % (medium, format), 'notification/%s' % format), context_instance=context) return format_templates
def get_formatted_message(formats, notice_type, context, medium): """ Returns a dictionary with the format identifier as the key. The values are are fully rendered templates with the given context. """ if not isinstance(context, Context): context = Context(context or {}) protocol = 'https' if USE_SSL else 'http' current_site = Site.objects.get_current() base_url = u"%s://%s" % (protocol, unicode(current_site)) context.update({ 'base_url': base_url, 'site': current_site, }) format_templates = {} context = apply_context_processors(context) for format in formats: # conditionally turn off autoescaping for .txt extensions in format if format.endswith(".txt") or format.endswith(".html"): context.autoescape = False else: context.autoescape = True format_templates[format] = render_to_string( ('notification/%s/%s/%s' % (notice_type.template_slug, medium, format), 'notification/%s/%s' % (notice_type.template_slug, format), 'notification/%s/%s' % (medium, format), 'notification/%s' % format), context_instance=context) return format_templates
def deliver(self, recipient, sender, notice_type, extra_context): context = Context(extra_context) short = backends.format_notification("short.txt", notice_type.label, context).rstrip('\n').rstrip('\r') message_txt = backends.format_notification("full.txt", notice_type.label, context) message = backends.format_notification("full.html", notice_type.label, context) body = render_to_string(("notification/email_body.html", "notification/default/email_body.html"), {"message": message}, context) context.autoescape = False subject = render_to_string(("notification/email_subject.txt", "notification/default/email_subject.txt"), { "message": short }, context).rstrip('\n').rstrip('\r') body_txt = render_to_string(("notification/default/email_body.txt", "notification/email_body.html"), {"message": message_txt}, context) msg = EmailMultiAlternatives(subject, body_txt, settings.DEFAULT_FROM_EMAIL, [recipient.email]) msg.attach_alternative(body, "text/html") msg.send()
def deliver(self, recipient, sender, notice_type, extra_context): context = Context(extra_context) short = backends.format_notification("short.txt", notice_type.label, context).rstrip('\n').rstrip('\r') message_txt = backends.format_notification("full.txt", notice_type.label, context) message = backends.format_notification("full.html", notice_type.label, context) body = render_to_string(("notification/email_body.html", "notification/default/email_body.html"), {"message": message}, context) context.autoescape = False subject = render_to_string(("notification/email_subject.txt", "notification/default/email_subject.txt"), {"message": short}, context).rstrip('\n').rstrip('\r') body_txt = render_to_string(("notification/default/email_body.txt", "notification/email_body.html"), {"message": message_txt}, context) msg = EmailMultiAlternatives(subject, body_txt, settings.DEFAULT_FROM_EMAIL, [recipient.email]) msg.attach_alternative(body, "text/html") msg.send()
def render_to_string_with_autoescape_off(template_name, dictionary=None, context_instance=None): dictionary = dictionary or {} if not context_instance: context_instance = Context(dictionary) context_instance.autoescape = False return render_to_string(template_name, context_instance=context_instance)
def get_message_from_template(to, template_name, context=None, from_email=None, bcc=None): """ Build an e-mail message from a Django template, also looking for an optional additional HTML part. """ # Build the context context = context or {} context['site'] = site_models.Site.objects.get_current() context.update({'site': site_models.Site.objects.get_current()}) if not isinstance(context, Context): context = Context(context) context.autoescape = False # Get the text email body source, origin = FileSystemLoader().load_template_source('emails/%s.txt' % template_name) template = loader.get_template_from_string(source, origin, template_name) body = template.render(context) # Get the subject line match = RE_SUBJECT.search(source) if not match: raise ValueError('The email source did not contain a "SUBJECT:" line') subject_source = match.group(1) template = loader.get_template_from_string(subject_source, origin, template_name) subject = template.render(context) # Format the recipient and build the e-mail message if isinstance(to, basestring): to = [to] message = django_mail.EmailMultiAlternatives(subject, body, to=to, from_email=from_email, bcc=bcc) # See if we've got an HTML component too try: html = loader.render_to_string('emails/%s.html' % template_name, context) except TemplateDoesNotExist: pass else: message.attach_alternative(html, "text/html") return message
def notice_send( sent_by, sent_to, label, extra_context=None, email = None): file = 'notification/%s.txt' % label context = Context(extra_context) context.autoescape = False message = render_to_string( file, dictionary=context) # Strip newlines from subject subject = 'SpotBurn Notice' notice = Notice.objects.create( user=sent_to, sent_by = sent_by, message=message, notice_type = label ) if email != None: send_mail(subject, message, settings.DEFAULT_FROM_EMAIL, email )
def notify(recipient, template_prefix, note_type, url=None, extra_context={}): """ Notify a user of an event using the Notification system and email. """ from panda.models import Notification context = Context({ 'recipient': recipient, 'type': note_type, 'url': url, 'site_domain': config_value('DOMAIN', 'SITE_DOMAIN') }) context.update(extra_context) message = get_message_template(template_prefix).render(context) Notification.objects.create(recipient=recipient, message=message, type=note_type, url=url) # Don't HTML escape plain-text emails context.autoescape = False try: email_subject = get_email_subject_template(template_prefix).render( context) except TemplateDoesNotExist: email_subject = message try: email_message = get_email_body_template(template_prefix).render( context) except TemplateDoesNotExist: email_message = message send_mail(email_subject.strip(), email_message, [recipient.username])
def notify(recipient, template_prefix, note_type, url=None, extra_context={}): """ Notify a user of an event using the Notification system and email. """ from panda.models import Notification context = Context({ 'recipient': recipient, 'type': note_type, 'url': url, 'site_domain': config_value('DOMAIN', 'SITE_DOMAIN') }) context.update(extra_context) message = get_message_template(template_prefix).render(context) Notification.objects.create( recipient=recipient, message=message, type=note_type, url=url ) # Don't HTML escape plain-text emails context.autoescape = False try: email_subject = get_email_subject_template(template_prefix).render(context) except TemplateDoesNotExist: email_subject = message try: email_message = get_email_body_template(template_prefix).render(context) except TemplateDoesNotExist: email_message = message send_mail(email_subject.strip(), email_message, [recipient.username])
for i in ist: if i not in soll: # Ist, aber soll nicht if stop_func != None: stop_func(i) ### Clean-up old stuff rmtree("/etc/nginx/conf/sites-available") rmtree("/etc/nginx/conf/sites-enabled") ### GLOBALS fqdn = socket.getfqdn() ip = socket.gethostbyname_ex(socket.gethostname())[2][0] ctx = Context() ctx.autoescape = False ### STARTUP monit_list = [ os.path.basename(x) for x in glob("/opt/pysk/serverconfig/etc/monit.d/*") ] monit_list += [ os.path.basename(x) for x in glob("/opt/pysk/serverconfig/etc/monit.d/optional/*") ] monit_restart_list = [] ### SERVER CONFIG runprog(["/opt/pysk/serverconfig/copy.sh"]) makefile(
def _template(self, content): context = Context(self.context) context.autoescape = self.filetype == 'html' return DjangoTemplate(content).render(context)
def _template(self, content): context = Context({}) context.autoescape = self.filetype == 'html' return DjangoTemplate(content).render(context)
def run(self, *args, **kwargs): from panda.models import UserProxy log = logging.getLogger(self.name) log.info('Running admin alerts') # Disk space root_disk = os.stat('/').st_dev upload_disk = os.stat(settings.MEDIA_ROOT).st_dev indices_disk = os.stat(settings.SOLR_DIRECTORY).st_dev root_disk_total = get_total_disk_space('/') root_disk_free = get_free_disk_space('/') root_disk_percent_used = 100 - (float(root_disk_free) / root_disk_total * 100) if upload_disk != root_disk: upload_disk_total = get_total_disk_space(settings.MEDIA_ROOT) upload_disk_free = get_free_disk_space(settings.MEDIA_ROOT) upload_disk_percent_used = 100 - (float(upload_disk_free) / upload_disk_total * 100) else: upload_disk_total = None upload_disk_free = None upload_disk_percent_used = None if indices_disk != root_disk: indices_disk_total = get_total_disk_space(settings.SOLR_DIRECTORY) indices_disk_free = get_free_disk_space(settings.SOLR_DIRECTORY) indices_disk_percent_used = 100 - (float(indices_disk_free) / indices_disk_total * 100) else: indices_disk_total = None indices_disk_free = None indices_disk_percent_used = None notify = False for free in (root_disk_free, upload_disk_free, indices_disk_free): if free is None: continue if free < settings.PANDA_AVAILABLE_SPACE_WARN: notify = True if notify: context = Context({ 'root_disk': root_disk, 'upload_disk': upload_disk, 'indices_disk': indices_disk, 'root_disk_total': root_disk_total, 'root_disk_free': root_disk_free, 'root_disk_percent_used': root_disk_percent_used, 'upload_disk_total': upload_disk_total, 'upload_disk_free': upload_disk_free, 'upload_disk_percent_used': upload_disk_percent_used, 'indices_disk_total': indices_disk_total, 'indices_disk_free': indices_disk_free, 'indices_disk_percent_used': indices_disk_percent_used, 'settings': settings, 'site_domain': config_value('DOMAIN', 'SITE_DOMAIN') }) # Don't HTML escape plain-text emails context.autoescape = False email_subject = get_email_subject_template('disk_space_alert').render(context) email_message = get_email_body_template('disk_space_alert').render(context) recipients = UserProxy.objects.filter(is_superuser=True, is_active=True) send_mail(email_subject.strip(), email_message, [r.email for r in recipients]) log.info('Finished running admin alerts')
def run(self, *args, **kwargs): from panda.models import UserProxy log = logging.getLogger(self.name) log.info('Running admin alerts') # Disk space root_disk = os.stat('/').st_dev upload_disk = os.stat(settings.MEDIA_ROOT).st_dev indices_disk = os.stat(settings.SOLR_DIRECTORY).st_dev root_disk_total = get_total_disk_space('/') root_disk_free = get_free_disk_space('/') root_disk_percent_used = 100 - (float(root_disk_free) / root_disk_total * 100) if upload_disk != root_disk: upload_disk_total = get_total_disk_space(settings.MEDIA_ROOT) upload_disk_free = get_free_disk_space(settings.MEDIA_ROOT) upload_disk_percent_used = 100 - (float(upload_disk_free) / upload_disk_total * 100) else: upload_disk_total = None upload_disk_free = None upload_disk_percent_used = None if indices_disk != root_disk: indices_disk_total = get_total_disk_space(settings.SOLR_DIRECTORY) indices_disk_free = get_free_disk_space(settings.SOLR_DIRECTORY) indices_disk_percent_used = 100 - (float(indices_disk_free) / indices_disk_total * 100) else: indices_disk_total = None indices_disk_free = None indices_disk_percent_used = None notify = False for free in (root_disk_free, upload_disk_free, indices_disk_free): if free is None: continue if free < settings.PANDA_AVAILABLE_SPACE_WARN: notify = True if notify: context = Context({ 'root_disk': root_disk, 'upload_disk': upload_disk, 'indices_disk': indices_disk, 'root_disk_total': root_disk_total, 'root_disk_free': root_disk_free, 'root_disk_percent_used': root_disk_percent_used, 'upload_disk_total': upload_disk_total, 'upload_disk_free': upload_disk_free, 'upload_disk_percent_used': upload_disk_percent_used, 'indices_disk_total': indices_disk_total, 'indices_disk_free': indices_disk_free, 'indices_disk_percent_used': indices_disk_percent_used, 'settings': settings, 'site_domain': config_value('DOMAIN', 'SITE_DOMAIN') }) # Don't HTML escape plain-text emails context.autoescape = False email_subject = get_email_subject_template( 'disk_space_alert').render(context) email_message = get_email_body_template('disk_space_alert').render( context) recipients = UserProxy.objects.filter(is_superuser=True, is_active=True) send_mail(email_subject.strip(), email_message, [r.email for r in recipients]) log.info('Finished running admin alerts')
for i in ist: if i not in soll: # Ist, aber soll nicht if stop_func != None: stop_func(i) ### Clean-up old stuff rmtree("/etc/nginx/conf/sites-available") rmtree("/etc/nginx/conf/sites-enabled") ### GLOBALS fqdn = socket.getfqdn() ip = socket.gethostbyname_ex(socket.gethostname())[2][0] ctx = Context() ctx.autoescape = False ### STARTUP monit_list = [os.path.basename(x) for x in glob("/opt/pysk/serverconfig/etc/monit.d/*")] monit_list += [os.path.basename(x) for x in glob("/opt/pysk/serverconfig/etc/monit.d/optional/*")] monit_restart_list = [] ### SERVER CONFIG runprog(["/opt/pysk/serverconfig/copy.sh"]) makefile( "/etc/rc.conf", render_to_string("etc/rc.conf", {"hostname": fqdn, "ip": ip, "hypervisor_ip": HYPERVISOR_IP}, ctx) ) ### DOVECOT for m in Mailbox.objects.all():