def make_message(name, to, ctx, headers={}): subject = render("emails/%s-subject.html" % name, ctx).strip() body = render("emails/%s-body-text.html" % name, ctx) html = render("emails/%s-body-html.html" % name, ctx) msg = EmailMultiAlternatives(subject, body, to=(to, ), headers=headers) msg.attach_alternative(html, "text/html") return msg
def invoice(to, ctx, filename, pdf_data): ctx["SITE_ROOT"] = settings.SITE_ROOT subject = render("emails/invoice-subject.html", ctx).strip() text = render("emails/invoice-body-text.html", ctx) html = render("emails/invoice-body-html.html", ctx) msg = EmailMultiAlternatives(subject, text, to=(to, )) msg.attach_alternative(html, "text/html") msg.attach(filename, pdf_data, "application/pdf") msg.send()
def invoice(to, ctx, filename, pdf_data): ctx["SITE_ROOT"] = settings.SITE_ROOT subject = render('emails/invoice-subject.html', ctx).strip() text = render('emails/invoice-body-text.html', ctx) html = render('emails/invoice-body-html.html', ctx) msg = EmailMultiAlternatives(subject, text, to=(to, )) msg.attach_alternative(html, "text/html") msg.attach(filename, pdf_data, "application/pdf") msg.send()
def send(name, to, ctx, headers={}): ctx["SITE_ROOT"] = settings.SITE_ROOT subject = render("emails/%s-subject.html" % name, ctx).strip() text = render("emails/%s-body-text.html" % name, ctx) html = render("emails/%s-body-html.html" % name, ctx) t = EmailThread(subject, text, html, to, headers) if hasattr(settings, "BLOCKING_EMAILS"): t.run() else: t.start()
def send(name, to, ctx, headers={}): ctx["SITE_ROOT"] = settings.SITE_ROOT subject = render('emails/%s-subject.html' % name, ctx).strip() text = render('emails/%s-body-text.html' % name, ctx) html = render('emails/%s-body-html.html' % name, ctx) t = EmailThread(subject, text, html, to, headers) if hasattr(settings, "BLOCKING_EMAILS"): t.run() else: t.start()
def run(self): self.ctx["SITE_ROOT"] = settings.SITE_ROOT subject = render('emails/%s-subject.html' % self.name, self.ctx) subject = subject.strip() text = render('emails/%s-body-text.html' % self.name, self.ctx) html = render('emails/%s-body-html.html' % self.name, self.ctx) msg = EmailMultiAlternatives(subject, text, to=(self.to, )) msg.attach_alternative(html, "text/html") msg.send()
def send(name, to, ctx, headers={}): ctx["SITE_ROOT"] = settings.SITE_ROOT subject = render("emails/%s-subject.html" % name, ctx).strip() text = render("emails/%s-body-text.html" % name, ctx) html = render("emails/%s-body-html.html" % name, ctx) t = EmailThread(subject, text, html, to, headers) if hasattr(settings, "BLOCKING_EMAILS"): # In tests, we send emails synchronously # so we can inspect the outgoing messages t.run() else: # Outside tests, we send emails on thread, # so there is no delay for the user. t.start()
def get(self, request): # Record starting time start = now() # Perform basic filters based on time range (if set) query = self.report_class.objects.active() # Extract information within a transaction with atomic(): data = self.generate_report(query) # Render the report name = self.report_name report_doc = render(template_name=self.template, context=RequestContext(request, locals()), request=request).encode('utf-8') # Count the elapsed time time = (now() - start).total_seconds() # Create the report output (save it if required to) filename = 'report_%s.html' % calendar.timegm(start.timetuple()) with open('reports/%s' % filename, 'w') as f: f.write(report_doc) # Send file as attachment ActionLog.objects.log_reports( 'Generated report (name: %s) in %s seconds' % (self.report_name, time), user=request.user) return HttpResponse(report_doc)
def to_simple_html(self, data, options): """ """ from django.template.loader import render_to_string as render # if isinstance(data, (list, tuple)): return render("api/listitem.html", {"data": [self.to_simple_html(item, options) for item in data]}) if isinstance(data, dict): return render("api/dictitem.html", {"data": dict((key, self.to_simple_html(val, options)) for (key, val) in data.items())}) elif isinstance(data, Bundle): return render("api/dictitem.html", {"data":dict((key, self.to_simple_html(val, options)) for (key, val) in data.data.items())}) elif hasattr(data, 'dehydrated_type'): debug.show('data') if getattr(data, 'dehydrated_type', None) == 'related' and data.is_m2m == False: return render("api/relitem.html", {"fk": data.fk_resource, "val": self.to_simple_html(data.value, options)}) elif getattr(data, 'dehydrated_type', None) == 'related' and data.is_m2m == True: render("api/listitem.html", {"data": [self.to_simple_html(bundle, options) for bundle in data.m2m_bundles]}) else: return self.to_simple_html(data.value, options) elif isinstance(data, datetime.datetime): return self.format_datetime(data) elif isinstance(data, datetime.date): return self.format_date(data) elif isinstance(data, datetime.time): return self.format_time(data) elif isinstance(data, bool): return data elif isinstance(data, (six.integer_types, float)): return data elif data is None: return None elif isinstance(data, basestring) and data.startswith("/api/v1/"): # XXX Will not work for Python 3 return render("api/relitem.html", {"fk": data, "val": data.split('/')[-2]}) else: return force_text(data)
def to_html(self, data, options=None): """ Reserved for future usage. The desire is to provide HTML output of a resource, making an API available to a browser. This is on the TODO list but not currently implemented. """ from django.template.loader import render_to_string as render options = options or {} serialized = self.to_simple_html(data, options) return render("api/base.html", {"data": serialized})
def handle(self, app, **options): command_name = options.get('command_name') CURRY_ROOT = path.abspath(path.dirname(currycore.__file__)) #: get curry core directory management_dir = path.join(app, "management") #: target project management directory commands_dir = path.join(management_dir, "commands") #: target project commands directory command_file = path.join(commands_dir, command_name + ".py") #: target project's command file if not path.isdir(commands_dir): system("mkdir -p %s" % (commands_dir)) touch_init_file(management_dir) touch_init_file(commands_dir) print "[curry] create management template to %s" % (management_dir) if not path.exists(command_file): output = render("currycore/command.py") f = open(command_file, 'w') f.write(output) f.close()
def output(self, template_name, output_file): content = render(template_name) with open(output_file, 'w') as f: f.write(content)