[UPDT] EMPLOYEE: Bulk mail option in employee view

This commit is contained in:
Horilla
2024-05-30 10:01:49 +05:30
parent eefa2a220f
commit 0532636bb7
5 changed files with 108 additions and 52 deletions

View File

@@ -4,6 +4,7 @@ employee/context_processors.py
This module is used to write context processor methods
"""
import json
from datetime import date
from django import template
@@ -73,16 +74,20 @@ def not_out_yet(request):
@login_required
@manager_can_enter("employee.change_employee")
def send_mail(request, emp_id):
def send_mail(request, emp_id=None):
"""
This method used send mail to the employees
"""
employeee = Employee.objects.get(id=emp_id)
employee = None
if emp_id:
employee = Employee.objects.get(id=emp_id)
employees = Employee.objects.all()
templates = RecruitmentMailTemplate.objects.all()
return render(
request,
"employee/send_mail.html",
{"employee": employeee, "templates": templates},
{"employee": employee, "templates": templates, "employees": employees},
)
@@ -113,60 +118,71 @@ def send_mail_to_employee(request):
employee_id = request.POST["id"]
subject = request.POST.get("subject")
bdy = request.POST.get("body")
employee_ids = request.POST.getlist("employees")
employees = Employee.objects.filter(id__in=employee_ids)
other_attachments = request.FILES.getlist("other_attachments")
attachments = [
(file.name, file.read(), file.content_type) for file in other_attachments
]
email_backend = ConfiguredEmailBackend()
host = email_backend.dynamic_username
employee = Employee.objects.get(id=employee_id)
if employee_id:
employee_obj = Employee.objects.filter(id=employee_id)
else:
employee_obj = Employee.objects.none()
employees = (employees | employee_obj).distinct()
template_attachment_ids = request.POST.getlist("template_attachments")
bodys = list(
RecruitmentMailTemplate.objects.filter(
id__in=template_attachment_ids
).values_list("body", flat=True)
)
for html in bodys:
# due to not having solid template we first need to pass the context
template_bdy = template.Template(html)
for employee in employees:
bodys = list(
RecruitmentMailTemplate.objects.filter(
id__in=template_attachment_ids
).values_list("body", flat=True)
)
for html in bodys:
# due to not having solid template we first need to pass the context
template_bdy = template.Template(html)
context = template.Context(
{"instance": employee, "self": request.user.employee_get}
)
render_bdy = template_bdy.render(context)
attachments.append(
(
"Document",
generate_pdf(render_bdy, {}, path=False, title="Document").content,
"application/pdf",
)
)
template_bdy = template.Template(bdy)
context = template.Context(
{"instance": employee, "self": request.user.employee_get}
)
render_bdy = template_bdy.render(context)
attachments.append(
(
"Document",
generate_pdf(render_bdy, {}, path=False, title="Document").content,
"application/pdf",
)
send_to_mail = (
employee.employee_work_info.email
if employee.employee_work_info and employee.employee_work_info.email
else employee.email
)
template_bdy = template.Template(bdy)
context = template.Context(
{"instance": employee, "self": request.user.employee_get}
)
render_bdy = template_bdy.render(context)
send_to_mail = (
employee.employee_work_info.email
if employee.employee_work_info and employee.employee_work_info.email
else employee.email
)
email = EmailMessage(
subject,
render_bdy,
host,
[send_to_mail],
)
email.content_subtype = "html"
email = EmailMessage(
subject,
render_bdy,
host,
[send_to_mail],
)
email.content_subtype = "html"
email.attachments = attachments
try:
email.send()
if employee.employee_work_info.email or employee.email:
messages.success(request, f"Mail sent to {employee.get_full_name()}")
else:
messages.info(request, f"Email not set for {employee.get_full_name()}")
except Exception as e:
messages.error(request, "Something went wrong")
email.attachments = attachments
try:
email.send()
if employee.employee_work_info.email or employee.email:
messages.success(request, f"Mail sent to {employee.get_full_name()}")
else:
messages.info(request, f"Email not set for {employee.get_full_name()}")
except Exception as e:
messages.error(request, "Something went wrong")
return HttpResponse("<script>window.location.reload()</script>")

View File

@@ -1,10 +1,12 @@
{% load i18n %}
<div id="ack-message-{{employee.id}}">
</div>
<form hx-post='{% url "send-mail-to-employee" %} ' class="oh-general__tab-target oh-profile-section"
<form onsubmit="$(this).closest('.oh-modal--show').removeClass('oh-modal--show')"
hx-post='{% url "send-mail-to-employee" %} ' class="oh-general__tab-target oh-profile-section"
id='ack-form-{{employee.id}}' hx-target="#ack-message-{{employee.id}}" hx-encoding="multipart/form-data">
<input type="hidden" value="{{employee.id}}" name="id">
<div class="modal-body">
{% if employee %}
<div class="oh-timeoff-modal__profile-content">
<div class="oh-profile mb-2">
<div class="oh-profile__avatar">
@@ -19,6 +21,7 @@
</div>
</div>
</div>
{% endif %}
<div class="form-group mt-2">
<label for="subject">
<h6>{% trans "Subject" %}</h6>
@@ -26,6 +29,16 @@
<input required type="text" placeholder="Important Reminder" name='subject' class="oh-input w-100" id="subject"
placeholder="Subject">
</div>
<div class="form-group mt-2">
<label for="employees">
<h6>{% trans "Also send to" %}</h6>
</label>
<select class="oh-select oh-select-2" {% if not employee %} required {% endif %} name="employees" id="employees" multiple>
{% for employee in employees %}
<option value="{{employee.id}}">{{employee}}</option>
{% endfor %}
</select>
</div>
<div class="form-group mt-2">
<label for="template">
<h6>{% trans "Template" %}</h6>
@@ -91,14 +104,14 @@
`));
}
if (valid) {
$(`#messages`).html($(`
<div class="oh-alert oh-alert--animated oh-alert--info" role="alert">
Processing...
</div>
`));
Swal.fire({
title: "Processing",
text: "Mail will sent on the background",
icon: "info"
});
}
};
$(document).ready(function () {
$("#template").change(function (e) {
var id = $(this).val();
@@ -115,5 +128,8 @@
});
}
});
var selectedIds = JSON.parse($("#selectedInstances").attr("data-ids"));
$("#employees[name=employees]select[multiple]").val(selectedIds).change()
});
</script>

View File

@@ -444,6 +444,18 @@
{% trans "Un-Archive" %}
</a>
</li>
<li class="oh-dropdown__item">
<a
hx-get="{% url 'employee-bulk-mail' %}"
hx-target="#mail-content"
href="#"
class="oh-dropdown__link "
data-toggle="oh-modal-toggle"
data-target="#sendMailModal"
>
{% trans "Bulk Mail" %}
</a>
</li>
<li class="oh-dropdown__item">
<a
href="#"

View File

@@ -15,5 +15,14 @@
{% endif %}
</div>
</div>
<div class="oh-modal" id="sendMailModal" role="dialog" aria-labelledby="sendMailModal" aria-hidden="true">
<div class="oh-modal__dialog">
<div class="oh-modal__dialog-header">
<span class="oh-modal__dialog-title" id="sendMailModalLabel"><h5>{% trans 'Send Mail' %}</h5></span>
<button class="oh-modal__close" aria-label="Close"><ion-icon name="close-outline"></ion-icon></button>
</div>
<div class="oh-modal__dialog-body" id="mail-content"></div>
</div>
</div>
<script src="{% static 'employee/actions.js' %}"></script>
{% endblock %}

View File

@@ -257,6 +257,9 @@ urlpatterns = [
not_in_out_dashboard.send_mail,
name="send-mail-employee",
),
path(
"employee-bulk-mail", not_in_out_dashboard.send_mail, name="employee-bulk-mail"
),
path(
"send-mail",
not_in_out_dashboard.send_mail_to_employee,