def get_initial(self): if not self.inv_class: raise ValueError( 'Improperly configured, needs an inv_class attribute') inv = self.inv_class.objects.get(pk=self.kwargs['pk']) out_file = os.path.join(os.getcwd(), 'media', 'temp', 'out.pdf') if os.path.exists(out_file): out_file = os.path.join(os.getcwd(), 'media', 'temp', f'out_{random.randint(1, 100000)}.pdf') #use the context for pagination and the object obj = self.inv_class.objects.get(pk=self.kwargs['pk']) context = { 'object': obj, } config = GlobalConfig.objects.first() context.update(config.__dict__) context.update({ 'logo': config.logo, 'logo_width': config.logo_width, 'business_name': config.business_name, 'business_address': config.business_address }) options = {'output': out_file} try: pdf_tools.render_pdf_from_template(self.pdf_template_name, None, None, apply_style(context), cmd_options=options) except Exception as e: print('Error occured creating pdf %s' % e) raise PDFException() return { 'owner': self.request.user.pk, 'folder': 'sent', 'attachment_path': out_file }
def post(self, request, *args, **kwargs): resp = super(EmailPlusPDFView, self).post(request, *args, **kwargs) form = self.form_class(request.POST) if not form.is_valid(): return resp config = GlobalConfig.objects.get(pk=1) msg = EmailMessage(subject=form.cleaned_data['subject'], body=form.cleaned_data['content'], from_email=config.email_user, to=[form.cleaned_data['recipient']]) if not self.pdf_template_name: raise ValueError( 'Improperly configured. Needs pdf_template_name attribute.') out_file = os.path.join(os.getcwd(), 'media', 'temp', 'out.pdf') #use the context for pagination and the object obj = self.inv_class.objects.get(pk=self.kwargs['pk']) context = { 'object': obj, } context.update(SalesConfig.objects.first().__dict__) options = {'output': out_file} try: pdf_tools.render_pdf_from_template(self.pdf_template_name, None, None, apply_style(context), cmd_options=options) except Exception as e: raise Exception('Error occured creating pdf %s' % e) if os.path.isfile(out_file): msg.attach_file(out_file) msg.send() os.remove(out_file) # if the message is successful delete it. return resp
def get_context_data(self, *args, **kwargs): context = super(ReceiptDetailView, self).get_context_data(*args, **kwargs) context.update(load_config()) context['title'] = 'Receipt' return apply_style(context)
def get_context_data(self, *args, **kwargs): context = super(OrderDetailView, self).get_context_data(*args, **kwargs) context.update(load_config()) return apply_style(context)