def minimize(template): """docstring for minimize""" from django.template import Template, Context # leave {{ for mustache template = template.replace('{{', _LEFT_BRACE_).replace(_RIGHT_BRACE_, '}}') template = Template(template).render(Context({})) template = template.replace(_LEFT_BRACE_, '{{').replace(_RIGHT_BRACE_, '}}') return template.replace('\n', '').replace("'","\\'")
def _confirm_action(context, label, url, confirm_msg="", icon="", method="POST", cls="", extra_cls="", disabled=False, inline=False): form_cls = "" if not confirm_msg: confirm_msg = _("Are you sure?") if icon == "remove" and cls == "": cls += " alert" if inline: form_cls += " inline" confirm_msg = unicode(confirm_msg) confirm_msg = Template(confirm_msg).render(context) confirm_msg = confirm_msg.replace('\n', ' '); confirm_code = """onsubmit="return confirm('%s');" """ % confirm_msg if "noconfirm" in cls: confirm_code = "" onclick = "" if not disabled: onclick = ("""onclick="$(this).closest('form').submit(); """ """return false" """) else: cls += " disabled" csrf_token = "" if 'csrf_token' in context: csrf_token = u"""<input type="hidden" """ + \ u"""value="%s" """ % unicode(context['csrf_token']) + \ u"""name="csrfmiddlewaretoken" />""" if "nocsrf" in cls: csrf_token = "" html = """ <form action="%(url)s" method="%(method)s" class="action-form %(form_cls)s" %(confirm_code)s> %(csrf_token)s <a href="#" %(onclick)s class="button foundicon-%(icon)s %(cls)s %(extra_cls)s"> %(label)s</a> </form> """ % { 'label': label, 'url': url, 'cls': cls, 'extra_cls': extra_cls, 'icon': icon, 'method': method, 'form_cls': form_cls, 'confirm_code': confirm_code, 'confirm_msg': confirm_msg, 'csrf_token': csrf_token, 'onclick': onclick } return html
def send_by_mail(self, subject, message, to=[], cc=[], bcc=[], issuer=None): """Send the :class:`~invoicing.models.InvoiceBase` by e-mail.""" if not self.is_issuable(): raise NotIssuableInvoice("Invoice is not issuable.") context = Context({ "reference": self.reference, "invoicing_date": self.current_revision.invoicing_date if self.is_invoice() and self.current_revision.invoicing_date else None, "signature": None }) for replace, search in InvoiceBase.SENDING_VARS: subject = Template(subject.replace(unicode(search), replace)).render(context) message = Template(message.replace(unicode(search), replace)).render(context) pdf = self.get_pdf(issuer) email = EmailMessage(subject, message, to=to, cc=cc, bcc=bcc) email.attach(pdf.name, pdf.file.read(), pdf.content_type) email.send() self.add_sent_history_entry(method='EMAIL', to=', '.join(to)) self.update(set__history=self.history)
def menu_confirm_action(context, label, url, confirm_msg="", icon="", method="POST", cls="", q=None): if not confirm_msg: confirm_msg = _("Are you sure?") if icon == "remove" and cls == "": cls += " alert" confirm_msg = unicode(confirm_msg) confirm_msg = Template(confirm_msg).render(context) confirm_msg = confirm_msg.replace('\n', ' ') icon_cls = "" if icon: icon_cls = "foundicon-%s" % icon confirm_code = """onsubmit="return confirm('%s');" """ % confirm_msg if "noconfirm" in cls: confirm_code = "" csrf_token = "" if 'csrf_token' in context: csrf_token = u"""<input type="hidden" """ + \ u"""value="%s" """ % unicode(context['csrf_token']) + \ u"""name="csrfmiddlewaretoken" />""" if "nocsrf" in cls: csrf_token = "" q_field = "" if q: q_field = """<input type="hidden" """ + \ """value="%s" """ % q + \ """name="q_param" \>""" html = """ <li> <form action="%(url)s" method="%(method)s" class="action-form" %(confirm_code)s> %(csrf_token)s %(q_hidden_field)s <a href="#" onclick="$(this).closest('form').submit(); return false" class="%(icon)s %(cls)s"> %(label)s</a> </form> </li> """ % { 'label': label, 'url': url, 'cls': cls, 'icon': icon_cls, 'method': method, 'confirm_code': confirm_code, 'confirm_msg': confirm_msg, 'csrf_token': csrf_token, 'q_hidden_field': q_field, } return html
def send_confirm_email(self): self.confirm_sent = True self.save() if self.email == settings.FROM_EMAIL: # ticket from ticket window return # email to admin tmpl = get_template('emails/confirm.txt') body = tmpl.render({'ticket': self}) email = EmailMessage(_('Confirmed / %s') % self.event(), body, settings.FROM_EMAIL, [self.event().admin], reply_to=[self.email]) email.send(fail_silently=False) # email to user e = self.event().get_email() extra = json.loads(self.extra_data) if e: subject = Template(e.subject).render( Context({ 'ticket': self, 'extra': extra })) body = Template(e.body).render( Context({ 'ticket': self, 'extra': extra })) else: tmpl = get_template('emails/subject-confirm-user.txt') subject = tmpl.render({'ticket': self, 'extra': extra}) tmpl = get_template('emails/confirm-user.txt') body = tmpl.render({'ticket': self, 'extra': extra}) body = body.replace('TICKETID', self.order) email = EmailMessage(subject.strip(), body, settings.FROM_EMAIL, [self.email]) if e: # adding attachments for att in e.attachs.all(): email.attach_file(att.attach.path) filename = 'ticket_%s.pdf' % self.order email.attach(filename, self.gen_pdf(), 'application/pdf') email.send(fail_silently=False)
def send_confirm_email(self): self.confirm_sent = True self.save() if self.email == settings.FROM_EMAIL: # ticket from ticket window return # email to admin tmpl = get_template("emails/confirm.txt") body = tmpl.render({"ticket": self}) email = EmailMessage( _("Confirmed / %s") % self.event(), body, settings.FROM_EMAIL, [self.event().admin], reply_to=[self.email] ) email.send(fail_silently=False) # email to user e = self.event().get_email() extra = json.loads(self.extra_data) if e: subject = Template(e.subject).render(Context({"ticket": self, "extra": extra})) body = Template(e.body).render(Context({"ticket": self, "extra": extra})) else: tmpl = get_template("emails/subject-confirm-user.txt") subject = tmpl.render({"ticket": self, "extra": extra}) tmpl = get_template("emails/confirm-user.txt") body = tmpl.render({"ticket": self, "extra": extra}) body = body.replace("TICKETID", self.order) email = EmailMessage(subject.strip(), body, settings.FROM_EMAIL, [self.email]) if e: # adding attachments for att in e.attachs.all(): email.attach_file(att.attach.path) filename = "ticket_%s.pdf" % self.order email.attach(filename, self.gen_pdf(), "application/pdf") email.send(fail_silently=False)
def panel(request, panel): from django.template import Context, Template p = Panel.objects.get(name = panel) t = Template(p.template.content) c = Context(simplejson.loads(p.template_configuration)) t = t.render(c) print t for element in p.template_elements.all(): b = Template(element.block.markup) j = Template(element.block.javascript) c = Context(simplejson.loads(element.block_configuration)) b = b.render(c) print b j = j.render(c) print j t = t.replace('@markup@@'+element.template_key+'@@@', b) t = t.replace('@script@@'+element.template_key+'@@@', j) return HttpResponse(t)
def send_broadcast_email(request): if not get_media_file_contents('generic_email.html'): return HttpResponseBadRequest( 'Generic email template not defined. Visit the NEMO customizable_key_values page to upload a template.' ) recipients = request.POST.getlist('recipient', None) form = EmailBroadcastForm(request.POST) if not form.is_valid(): return render(request, 'email/compose_email.html', {'form': form}) dictionary = { 'title': form.cleaned_data['title'], 'greeting': form.cleaned_data['greeting'], 'contents': form.cleaned_data['contents'], 'template_color': form.cleaned_data['color'], } content = get_media_file_contents('generic_email.html') content = Template(content).render(Context(dictionary)) users = None audience = form.cleaned_data['audience'] selection = form.cleaned_data['selection'] active_choice = form.cleaned_data['only_active_users'] try: users = User.objects.filter(id__in=recipients) if active_choice: users = users.filter(is_active=True) except Exception as error: warning_message = 'Your email was not sent. There was a problem finding the users to send the email to.' dictionary = {'error': warning_message} logger.warning( warning_message + ' audience: {}, only_active: {}. The error message that NEMO received is: {}' .format(audience, active_choice, str(error))) return render(request, 'email/compose_email.html', dictionary) if not users: dictionary = { 'error': 'The audience you specified is empty. You must send the email to at least one person.' } return render(request, 'email/compose_email.html', dictionary) subject = form.cleaned_data['subject'] users = [x.email for x in users] if form.cleaned_data['copy_me']: users += [request.user.email] try: email = EmailMultiAlternatives(subject, from_email=request.user.email, bcc=set(users)) content = content.replace("\r\n\r\n", "</p><p>") content = content.replace("\r\n", "<br />") email.attach_alternative(content, 'text/html') email.send() except SMTPException as error: error_message = 'NEMO was unable to send the email through the email server. The error message that NEMO received is: ' + str( error) logger.exception(error_message) dictionary = { 'title': 'Email not sent', 'heading': 'There was a problem sending your email', 'content': error_message, } return render(request, 'acknowledgement.html', dictionary) dictionary = { 'title': 'Email sent', 'heading': 'Your email was sent', } return render(request, 'acknowledgement.html', dictionary)