42 lines
1.5 KiB
Python
42 lines
1.5 KiB
Python
"""Expenses claim views."""
|
|
|
|
from django.shortcuts import render
|
|
from django.views.decorators.http import require_http_methods
|
|
from django.http import HttpResponse
|
|
from webapp.utils import send_email_with_attachment
|
|
from django.template.loader import render_to_string
|
|
from weasyprint import HTML
|
|
from .forms import ExpensesClaim
|
|
|
|
|
|
@require_http_methods(["GET", "POST"])
|
|
def claim(request):
|
|
"""Render expenses claim form."""
|
|
|
|
if request.method == 'POST':
|
|
form = ExpensesClaim(request.POST)
|
|
if form.is_valid():
|
|
name = form.cleaned_data['name']
|
|
amount = form.cleaned_data['amount']
|
|
iban = form.cleaned_data['iban']
|
|
html_string = render_to_string('expenses_claim:claim2pdf.html',
|
|
{'name': name, 'iban': iban,
|
|
'amount': amount}).encode('UTF-8')
|
|
html = HTML(string=html_string)
|
|
attachment = html.write_pdf()
|
|
response = HttpResponse(
|
|
attachment, content_type='application/pdf;'
|
|
)
|
|
response['Content-Disposition'] = 'filename=claim.pdf'
|
|
|
|
email = "leo.kivikunnas@aalto.fi"
|
|
subject = "Test expenses claim"
|
|
body = "Test"
|
|
send_email_with_attachment(email, subject, body, attachment)
|
|
return response
|
|
|
|
elif request.method == 'GET':
|
|
form = ExpensesClaim()
|
|
|
|
return render(request, 'expenses_claim:base.html', {'form': form})
|