Skip to content
Snippets Groups Projects
Commit 75315d77 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

in kit request view there is a button that can send emails

parent 42698332
No related branches found
No related tags found
1 merge request!47Resolve "Email to technician"
......@@ -88,10 +88,10 @@
<div class="col-sm-12">
{% if end_date == None %}
<a href="{% url 'web.views.kit_requests_send_mail' start_date|date:"Y-m-d" %}"
class="btn btn-block btn-default">Show email content</a>
class="btn btn-block btn-default">Send email</a>
{% else %}
<a href="{% url 'web.views.kit_requests_send_mail' start_date|date:"Y-m-d" end_date|date:"Y-m-d" %}"
class="btn btn-block btn-default">Show email content</a>
class="btn btn-block btn-default">Send email</a>
{% endif %}
</div>
</div><!-- /.box-footer -->
......
# coding=utf-8
import datetime
from django.contrib import messages
from django.utils.dateparse import parse_datetime
from notifications import get_filter_locations, get_today_midnight_date
from web.models import ConfigurationItem
from web.models.constants import KIT_RECIPIENT_EMAIL_CONFIGURATION_TYPE
from . import wrap_response
from ..email import EmailSender
from ..forms import KitRequestForm
from ..models import AppointmentType, Appointment
......@@ -16,8 +20,12 @@ def get_kit_requests(user, start_date=None, end_date=None):
else:
if isinstance(start_date, str):
start_date = parse_datetime(start_date)
if isinstance(start_date, unicode):
start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
if (end_date is not None) and (isinstance(end_date, str)):
end_date = parse_datetime(end_date)
if (end_date is not None) and (isinstance(end_date, unicode)):
end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
appointment_types = AppointmentType.objects.filter(required_equipment__disposable=True)
......@@ -59,5 +67,36 @@ def kit_requests(request):
def kit_requests_send_mail(request, start_date, end_date=None):
return wrap_response(request, 'equipment_and_rooms/kit_requests_send_mail.html',
get_kit_requests_data(request, start_date, end_date))
data = get_kit_requests_data(request, start_date, end_date)
end_date_str = " end of time"
if end_date is not None:
end_date_str = data["end_date"].strftime('%Y-%m-%d')
title = "Kits required between " + data["start_date"].strftime('%Y-%m-%d') + " and " + end_date_str
email_body = "<h1>" + title + "</h1>"
email_body += "<table><thead><tr><th>Date</th><th>Kits</th><th>Location</th><th>Person responsible</th></tr></thead>"
email_body += "<tbody>"
for appointment in data["appointments"]:
email_body += "<tr>"
email_body += "<td>" + appointment.datetime_when.strftime('%Y-%m-%d %H:%M') + "</td>"
email_body += "<td>"
for type in appointment.appointment_types.all():
for item in type.required_equipment.all():
if item.disposable:
email_body += item.name + ", "
email_body += "</td>"
email_body += "<td>" + str(appointment.location) + "</td>"
email_body += "<td>" + str(appointment.worker_assigned) + "</td>"
email_body += "</tr>"
email_body += "</tbody></table>"
recipients = ConfigurationItem.objects.get(type=KIT_RECIPIENT_EMAIL_CONFIGURATION_TYPE).value
sender = request.user.email
cc_recipients = []
if sender is not None:
cc_recipients.append(sender)
try:
EmailSender().send_email(title, email_body, recipients, cc_recipients)
messages.add_message(request, messages.SUCCESS, 'Mail sent')
except:
messages.add_message(request, messages.ERROR, 'There was problem with sending email')
return wrap_response(request, 'equipment_and_rooms/kit_requests.html', get_kit_requests_data(request))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment