def submission_billing(request): unbilled_submissions = list(Submission.objects.filter(billed_at=None, current_submission_form__acknowledged=True)) for submission in unbilled_submissions: submission.price = Price.objects.get_for_submission(submission) if request.method == 'POST': selected_for_billing = [] for submission in unbilled_submissions: if request.POST.get('bill_%s' % submission.pk, False): selected_for_billing.append(submission) xls = SimpleXLS() xls.write_row(0, (_(u'amt.'), _(u'EC-Number'), _(u'company'), _(u'Eudract-Nr.'), _(u'applicant'), _(u'clinic'), _(u'sum'))) for i, submission in enumerate(selected_for_billing): r = i + 1 submission_form = submission.current_submission_form xls.write_row(i + 1, [ "%s." % r, submission.get_ec_number_display(), _get_address(submission_form, submission_form.invoice_name and 'invoice' or 'sponsor'), submission_form.eudract_number or '?', submission_form.submitter_contact.full_name, _get_organizations(submission_form), submission.price.price, ]) r = len(selected_for_billing) + 1 xls.write(r, 6, xlwt.Formula('SUM(G2:G%s)' % r)) xls_buf = StringIO() xls.save(xls_buf) now = datetime.datetime.now() doc = Document.objects.create_from_buffer(xls_buf.getvalue(), mimetype='application/vnd.ms-excel', date=now) Submission.objects.filter(pk__in=[s.pk for s in selected_for_billing]).update(billed_at=now) htmlmail = unicode(render_html(request, 'billing/email/submissions.html', {})) plainmail = whitewash(htmlmail) deliver(subject=_(u'Billing request'), message=plainmail, message_html=htmlmail, attachments=[('billing-%s.xls' % now.strftime('%Y%m%d-%H%I%S'), xls_buf.getvalue(), 'application/vnd.ms-excel'),], from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=settings.BILLING_RECIPIENT_LIST, ) summary, total = collect_submission_billing_stats(selected_for_billing) return render(request, 'billing/submission_summary.html', { 'summary': summary, 'xls_doc': doc, 'total': total, }) return HttpResponseRedirect(reverse('ecs.billing.views.submission_billing')) return render(request, 'billing/submissions.html', { 'submissions': unbilled_submissions, })
def external_review_payment(request): submissions = Submission.objects.filter(external_reviewer=True, external_reviewer_billed_at=None, external_reviewer_name__isnull=False) price = Price.objects.get_review_price() if request.method == 'POST': selected_for_payment = [] for submission in submissions: if request.POST.get('pay_%s' % submission.pk, False): selected_for_payment.append(submission) reviewers = User.objects.filter(reviewed_submissions__in=selected_for_payment).distinct() xls = SimpleXLS() xls.write_row(0, (_(u'amt.'), _(u'reviewer'), _(u'EC-Nr.'), _(u'sum'))) for i, reviewer in enumerate(reviewers): submissions = reviewer.reviewed_submissions.filter(pk__in=[s.pk for s in selected_for_payment]) xls.write_row(i + 1, [ len(submissions), reviewer.get_full_name(), ", ".join(s.get_ec_number_display() for s in submissions), len(submissions) * price.price, ]) r = len(reviewers) + 1 xls.write_row(r, [ xlwt.Formula('SUM(A2:A%s)' % r), "", "", xlwt.Formula('SUM(D2:D%s)' % r), ]) xls_buf = StringIO() xls.save(xls_buf) now = datetime.datetime.now() doc = Document.objects.create_from_buffer(xls_buf.getvalue(), mimetype='application/vnd.ms-excel', date=now) Submission.objects.filter(pk__in=[s.pk for s in selected_for_payment]).update(external_reviewer_billed_at=now) htmlmail = unicode(render_html(request, 'billing/email/external_review.html', {})) plainmail = whitewash(htmlmail) deliver(subject=_(u'Payment request'), message=plainmail, message_html=htmlmail, attachments=[('externalreview-%s.xls' % now.strftime('%Y%m%d-%H%I%S'), xls_buf.getvalue(), 'application/vnd.ms-excel'),], from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=settings.BILLING_RECIPIENT_LIST, ) return render(request, 'billing/external_review_summary.html', { 'reviewers': reviewers, 'xls_doc': doc, }) return render(request, 'billing/external_review.html', { 'submissions': submissions, 'price': price, })
def __prepareBody(message): if not message.base.parts: body = message.body() else: body = None for part in message.walk(): if (not body) and ("text/plain" in part.content_encoding["Content-Type"]): body = part.body elif (not body) and ("html" in part.content_encoding["Content-Type"]): body = whitewash(part.body) if not body: body = message.body() return body
def create_mail(subject, message, from_email, recipient, message_html=None, attachments= None, msgid=None, **kwargs): ''' ''' if msgid is None: msgid = make_msgid() headers = {'Message-ID': msgid} if message is None: # make text version out of html if text version is missing message = whitewash(message_html) if message and message_html: msg = EmailMultiAlternatives(subject, message, from_email, [recipient], headers= headers) msg.attach_alternative(message_html, "text/html") else: msg = EmailMessage(subject, message, from_email, [recipient], headers= headers) if attachments: for attachment in attachments: filename = content = mimetype = encoding = None if isinstance(attachment, tuple) or isinstance(attachment, list): filename, content = attachment[0:2] if len(attachment) == 3: mimetype = attachment[2] else: mimetype, encoding = mimetypes.guess_type(filename) elif isinstance(attachment, basestring): filename = attachment if not os.path.exists(filename): raise IOError("attachment file not found %s" % filename) mimetype, encoding = mimetypes.guess_type(filename) content = open(filename, "rb").read() else: raise TypeError('dont know how to handle attachment from type %s' % (str(type(attachment)))) if not mimetype: raise TypeError("No content type given, and couldn't guess from the filename: %s" % filename) msg.attach(filename, content, mimetype) return msg
def agenda_htmlemail(request, meeting_pk=None): meeting = get_object_or_404(Meeting, pk=meeting_pk) filename = '%s-%s-%s.pdf' % ( meeting.title, meeting.start.strftime('%d-%m-%Y'), _('agenda') ) pdf = render_pdf(request, 'db/meetings/xhtml2pdf/agenda.html', { 'meeting': meeting, }) for recipient in settings.AGENDA_RECIPIENT_LIST: htmlmail = unicode(render_html(request, 'meetings/email/invitation-with-agenda.html', { 'meeting': meeting, 'recipient': recipient, })) plainmail = whitewash(htmlmail) deliver(subject=_('Invitation to meeting'), message=plainmail, message_html=htmlmail, attachments=[(filename, pdf,'application/pdf'),], from_email=settings.DEFAULT_FROM_EMAIL, recipient_list=settings.AGENDA_RECIPIENT_LIST) return HttpResponseRedirect(reverse('ecs.meetings.views.upcoming_meetings'))
def testWhitewashing(self): self.assertEqual(whitewash(self.testhtml, True), self.result_decoded_entities) self.assertEqual(whitewash(self.testhtml, False), self.result_encoded_entities)