Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/xzzy/ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Moore committed Sep 25, 2020
2 parents 076a805 + 1479971 commit 7210bb4
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 10 deletions.
75 changes: 75 additions & 0 deletions ledger/emails/emails.py
Expand Up @@ -20,6 +20,12 @@ def _render(template, context):
template = Template(template)
return template.render(context)

def _render2(template, context):
if isinstance(context, dict):
context = context
if isinstance(template, six.string_types):
template = Template(template)
return template.render(context)

class EmailBase(object):
subject = ''
Expand Down Expand Up @@ -86,3 +92,72 @@ def send(self, to_addresses, from_address=None, context=None, attachments=None,
except Exception as e:
logger.exception("Error while sending email to {}: {}".format(to_addresses, e))
return None



class EmailBase2(object):
subject = ''
html_template = 'email/base_email.html'
# txt_template can be None, in this case a 'tag-stripped' version of the html will be sent. (see send)
txt_template = 'email/base-email.txt'

def send_to_user(self, user, context=None):
return self.send(user.email, context=context)

def send(self, to_addresses, from_address=None, context=None, attachments=None, cc=None, bcc=None, reply_to=None):
"""
Send an email using EmailMultiAlternatives with text and html.
:param to_addresses: a string or a list of addresses
:param from_address: if None the settings.DEFAULT_FROM_EMAIL is used
:param context: a dictionary or a Context object used for rendering the templates.
:param attachments: a list of (filepath, content, mimetype) triples
(see https://docs.djangoproject.com/en/1.9/topics/email/)
or Documents
:param bcc:
:param cc:
:param reply_to:
:return:
"""
# The next line will throw a TemplateDoesNotExist if html template cannot be found
html_template = loader.get_template(self.html_template)
# render html
html_body = _render2(html_template, context)
if self.txt_template is not None:
txt_template = loader.get_template(self.txt_template)
txt_body = _render2(txt_template, context)
else:
txt_body = strip_tags(html_body)

# build message
if isinstance(to_addresses, six.string_types):
to_addresses = [to_addresses]
if isinstance(reply_to, six.string_types):
reply_to = [reply_to]
if attachments is None:
attachments = []
if attachments is not None and not isinstance(attachments, list):
attachments = list(attachments)

if attachments is None:
attachments = []

# Convert Documents to (filename, content, mime) attachment
_attachments = []
for attachment in attachments:
if isinstance(attachment, Document):
filename = str(attachment)
content = attachment.file.read()
mime = mimetypes.guess_type(attachment.filename)[0]
_attachments.append((filename, content, mime))
else:
_attachments.append(attachment)
msg = EmailMultiAlternatives(self.subject, txt_body, from_email=from_address, to=to_addresses,
attachments=_attachments, cc=cc, bcc=bcc, reply_to=reply_to)
msg.attach_alternative(html_body, 'text/html')
try:
msg.send(fail_silently=False)
return msg
except Exception as e:
logger.exception("Error while sending email to {}: {}".format(to_addresses, e))
return None

3 changes: 1 addition & 2 deletions ledger/payments/bpoint/models.py
Expand Up @@ -162,7 +162,6 @@ def refund(self,info,user,matched=True):
from ledger.payments.models import TrackRefund, Invoice
LEDGER_REFUND_EMAIL = env('LEDGER_REFUND_EMAIL', False)
LEDGER_REFUND_TRANSACTION_CALLBACK_MODULE =env('LEDGER_REFUND_TRANSACTION_CALLBACK_MODULE', '')

with transaction.atomic():
amount = info['amount']
details = info['details']
Expand Down Expand Up @@ -202,10 +201,10 @@ def refund(self,info,user,matched=True):
except Exception as e:
print (e)


if LEDGER_REFUND_EMAIL is True:
# Disabled as requested by Walter and then enabled again for parkstay
send_refund_email(Invoice.objects.get(reference=self.crn1),'card',txn.amount,card_ending=self.last_digits)
pass
else:
raise ValidationError('The refund amount is greater than the amount refundable on this card.')
else:
Expand Down
20 changes: 14 additions & 6 deletions ledger/payments/emails.py
@@ -1,4 +1,5 @@
from ledger.emails.emails import EmailBase
from ledger.emails.emails import EmailBase, EmailBase2
import django

ledger_email = 'no-reply@dbca.wa.gov.au'

Expand All @@ -8,23 +9,30 @@ class TemplateEmailBase(EmailBase):
# txt_template can be None, in this case a 'tag-stripped' version of the html will be sent. (see send)
txt_template = 'dpaw_payments/emails/base.txt'

class TemplateEmailBase2(EmailBase2):
subject = ''
html_template = 'dpaw_payments/emails/base.html'
# txt_template can be None, in this case a 'tag-stripped' version of the html will be sent. (see send)
txt_template = 'dpaw_payments/emails/base.txt'

def send_refund_email(invoice,refund_type,amount,card_ending=None):
if refund_type == 'card;' and not card_ending:
raise ValidationError('The last four card numbers are required for a crad refund')

email_obj = TemplateEmailBase()
django_version = int(str(django.VERSION[0])+''+str(django.VERSION[1]))
if django_version > 110:
email_obj = TemplateEmailBase2()
else:
email_obj = TemplateEmailBase()
email_obj.subject = 'Refund for invoice {}'.format(invoice.reference)
email_obj.html_template = 'dpaw_payments/emails/refund.html'
email_obj.txt_template = 'dpaw_payments/emails/refund.txt'

email = invoice.owner.email

if email:
context = {
'reference': invoice.reference,
'amount': amount,
'refund_type': refund_type,
'card_ending': card_ending
}

email_obj.send([email], from_address=ledger_email, context=context)
2 changes: 1 addition & 1 deletion ledger/payments/pdf.py
Expand Up @@ -194,7 +194,7 @@ def _create_header(canvas, doc, draw_page_number=True):
canvas.drawImage(dpaw_header_logo, PAGE_WIDTH / 3, current_y - (dpaw_header_logo_size[1]/2),width=dpaw_header_logo_size[0]/2, height=dpaw_header_logo_size[1]/2, mask='auto')

current_y -= 70
canvas.drawCentredString(PAGE_WIDTH / 2, current_y - LARGE_FONTSIZE, 'TAX INVOICE')
canvas.drawCentredString(PAGE_WIDTH / 2, current_y - LARGE_FONTSIZE, 'TAX INVOICE / RECEIPT')

current_y -= 20
canvas.drawCentredString(PAGE_WIDTH / 2, current_y - LARGE_FONTSIZE, 'ABN: 38 052 249 024')
Expand Down
2 changes: 1 addition & 1 deletion ledger/urls.py
Expand Up @@ -20,7 +20,7 @@


urlpatterns = [
url(r'^ledger/admin/', admin.site.urls),
#url(r'^ledger/admin/', admin.site.urls, name='ledger_admin'),
url(r'^ledger/', include('ledger.accounts.urls', namespace='accounts')),
url(r'^ledger/', include('ledger.payments.urls', namespace='payments')),
url(r'^ledger/', include('social_django.urls', namespace='social')),
Expand Down

0 comments on commit 7210bb4

Please sign in to comment.