[ADD] EMPLOYEE: Mail preview feature
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
import ldap
|
||||
from django.conf import settings
|
||||
from django.core.management.base import BaseCommand
|
||||
from employee.models import Employee
|
||||
from django.contrib.auth.models import User
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db.models import Q
|
||||
|
||||
from employee.models import Employee
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Imports employees from LDAP into the Django database"
|
||||
@@ -12,13 +13,19 @@ class Command(BaseCommand):
|
||||
def handle(self, *args, **kwargs):
|
||||
try:
|
||||
connection = ldap.initialize(settings.AUTH_LDAP_SERVER_URI)
|
||||
connection.simple_bind_s(settings.AUTH_LDAP_BIND_DN, settings.AUTH_LDAP_BIND_PASSWORD)
|
||||
|
||||
search_base = "ou=users,dc=test,dc=com" # Replace with your actual search base
|
||||
connection.simple_bind_s(
|
||||
settings.AUTH_LDAP_BIND_DN, settings.AUTH_LDAP_BIND_PASSWORD
|
||||
)
|
||||
|
||||
search_base = (
|
||||
"ou=users,dc=test,dc=com" # Replace with your actual search base
|
||||
)
|
||||
search_filter = "(objectClass=inetOrgPerson)"
|
||||
|
||||
results = connection.search_s(search_base, ldap.SCOPE_SUBTREE, search_filter)
|
||||
|
||||
|
||||
results = connection.search_s(
|
||||
search_base, ldap.SCOPE_SUBTREE, search_filter
|
||||
)
|
||||
|
||||
for dn, entry in results:
|
||||
|
||||
user_id = entry.get("uid", [b""])[0].decode("utf-8")
|
||||
@@ -31,35 +38,41 @@ class Command(BaseCommand):
|
||||
# Get the password from LDAP
|
||||
ldap_password = entry.get("userPassword", [b""])[0].decode("utf-8")
|
||||
|
||||
|
||||
# Create or update the Employee record, storing the LDAP password
|
||||
employee, created = Employee.objects.update_or_create(
|
||||
email = email,
|
||||
email=email,
|
||||
defaults={
|
||||
"employee_first_name": first_name,
|
||||
"employee_last_name": last_name,
|
||||
"email": email,
|
||||
"phone": phone,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
# Retrieve the associated User if it exists
|
||||
try:
|
||||
user = User.objects.get(Q(username=email) | Q(username=user_id) | Q(email=email))
|
||||
user = User.objects.get(
|
||||
Q(username=email) | Q(username=user_id) | Q(email=email)
|
||||
)
|
||||
user.username = user_id
|
||||
user.set_password(ldap_password) # Hash and set the password securely
|
||||
user.set_password(
|
||||
ldap_password
|
||||
) # Hash and set the password securely
|
||||
user.save() # Save the changes to the User instance
|
||||
action = "Updated"
|
||||
except User.DoesNotExist:
|
||||
# If the user does not exist, handle it accordingly (e.g., log a message or create a new user)
|
||||
self.stdout.write(self.style.WARNING(f"User for employee {name} does not exist."))
|
||||
self.stdout.write(
|
||||
self.style.WARNING(f"User for employee {name} does not exist.")
|
||||
)
|
||||
continue
|
||||
|
||||
|
||||
action = "Created" if created else "Updated"
|
||||
self.stdout.write(self.style.SUCCESS(f"{action} employee {name} with LDAP password"))
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(f"{action} employee {name} with LDAP password")
|
||||
)
|
||||
|
||||
connection.unbind_s()
|
||||
|
||||
except ldap.LDAPError as e:
|
||||
self.stderr.write(self.style.ERROR(f"LDAP Error: {e}"))
|
||||
self.stderr.write(self.style.ERROR(f"LDAP Error: {e}"))
|
||||
|
||||
@@ -1,26 +1,24 @@
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES
|
||||
from employee.models import Employee
|
||||
from django.core.management.base import BaseCommand
|
||||
from ldap3 import ALL, ALL_ATTRIBUTES, Connection, Server
|
||||
|
||||
from employee.models import Employee
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'Import users from Django to LDAP'
|
||||
|
||||
help = "Import users from Django to LDAP"
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
# LDAP server details
|
||||
ldap_server = 'localhost'
|
||||
bind_dn = 'cn=admin,dc=test,dc=com' # Replace with your bind DN
|
||||
bind_password = 'cool' # Change to your LDAP admin password
|
||||
ldap_server = "localhost"
|
||||
bind_dn = "cn=admin,dc=test,dc=com" # Replace with your bind DN
|
||||
bind_password = "cool" # Change to your LDAP admin password
|
||||
|
||||
# Connect to the LDAP server
|
||||
server = Server(ldap_server, get_info=ALL)
|
||||
|
||||
|
||||
try:
|
||||
conn = Connection(server, bind_dn, bind_password, auto_bind=True)
|
||||
|
||||
@@ -32,31 +30,44 @@ class Command(BaseCommand):
|
||||
# Prepare user data for LDAPclear
|
||||
dn = f"uid={user.employee_user_id.username},ou=users,dc=test,dc=com"
|
||||
attributes = {
|
||||
'objectClass': ['inetOrgPerson'],
|
||||
'givenName': user.employee_first_name,
|
||||
'sn': user.employee_last_name,
|
||||
'cn': f"{user.employee_first_name} {user.employee_last_name}",
|
||||
'uid': user.email,
|
||||
'mail': user.email,
|
||||
"telephoneNumber": user.phone,
|
||||
'userPassword': user.phone,
|
||||
"objectClass": ["inetOrgPerson"],
|
||||
"givenName": user.employee_first_name,
|
||||
"sn": user.employee_last_name,
|
||||
"cn": f"{user.employee_first_name} {user.employee_last_name}",
|
||||
"uid": user.email,
|
||||
"mail": user.email,
|
||||
"telephoneNumber": user.phone,
|
||||
"userPassword": user.phone,
|
||||
}
|
||||
|
||||
|
||||
# Check if the user already exists in LDAP
|
||||
conn.search('ou=users,dc=test,dc=com', f'(uid={user.employee_user_id.username})', attributes=ALL_ATTRIBUTES)
|
||||
|
||||
conn.search(
|
||||
"ou=users,dc=test,dc=com",
|
||||
f"(uid={user.employee_user_id.username})",
|
||||
attributes=ALL_ATTRIBUTES,
|
||||
)
|
||||
|
||||
if conn.entries:
|
||||
self.stdout.write(self.style.WARNING(f'{user.employee_first_name} {user.employee_last_name} already exists in LDAP. Skipping...'))
|
||||
self.stdout.write(
|
||||
self.style.WARNING(
|
||||
f"{user.employee_first_name} {user.employee_last_name} already exists in LDAP. Skipping..."
|
||||
)
|
||||
)
|
||||
else:
|
||||
# Add user to LDAP
|
||||
if not conn.add(dn, attributes=attributes):
|
||||
self.stdout.write(self.style.ERROR(f'Failed to add {user.employee_first_name} {user.employee_last_name}: {conn.result}'))
|
||||
self.stdout.write(
|
||||
self.style.ERROR(
|
||||
f"Failed to add {user.employee_first_name} {user.employee_last_name}: {conn.result}"
|
||||
)
|
||||
)
|
||||
else:
|
||||
self.stdout.write(self.style.SUCCESS(f'Successfully added {user.employee_first_name} {user.employee_last_name} to LDAP.'))
|
||||
|
||||
self.stdout.write(
|
||||
self.style.SUCCESS(
|
||||
f"Successfully added {user.employee_first_name} {user.employee_last_name} to LDAP."
|
||||
)
|
||||
)
|
||||
|
||||
conn.unbind()
|
||||
except Exception as e:
|
||||
self.stdout.write(self.style.ERROR(f'An error occurred: {e}'))
|
||||
self.stdout.write(self.style.ERROR(f"An error occurred: {e}"))
|
||||
|
||||
@@ -168,15 +168,23 @@ def get_template(request, emp_id):
|
||||
This method is used to return the mail template
|
||||
"""
|
||||
body = HorillaMailTemplate.objects.get(id=emp_id).body
|
||||
instance_id = request.GET.get("instance_id")
|
||||
if instance_id:
|
||||
instance = Employee.objects.get(id=instance_id)
|
||||
template_bdy = template.Template(body)
|
||||
context = template.Context(
|
||||
{"instance": instance, "self": request.user.employee_get}
|
||||
)
|
||||
body = template_bdy.render(context)
|
||||
return JsonResponse({"body": body})
|
||||
|
||||
|
||||
@login_required
|
||||
def get_mail_preview(request, emp_id=None):
|
||||
"""
|
||||
This method is used to return the mail template
|
||||
"""
|
||||
body = request.GET.get("body")
|
||||
template_bdy = template.Template(body)
|
||||
# candidate_id = request.GET.get("candidate_id")
|
||||
if emp_id:
|
||||
employee = Employee.objects.get(id=emp_id)
|
||||
context = template.Context(
|
||||
{"instance": employee, "self": request.user.employee_get}
|
||||
)
|
||||
body = template_bdy.render(context) or " "
|
||||
return JsonResponse({"body": body})
|
||||
|
||||
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
<label for="template">
|
||||
<h6>{% trans "Template" %}</h6>
|
||||
</label>
|
||||
<select name="template" class="w-100 oh-select" id="template">
|
||||
<select name="template" class="w-100 oh-select" id="template" onchange="templateChange($(this))">
|
||||
<option value="">----</option>
|
||||
{% for template in templates %}
|
||||
<option value="{{template.id}}">{{template.title}}</option>
|
||||
@@ -121,23 +121,49 @@
|
||||
};
|
||||
|
||||
$(document).ready(function () {
|
||||
$("#template").change(function (e) {
|
||||
var id = $(this).val();
|
||||
if (id.length) {
|
||||
$.ajax({
|
||||
type: "get",
|
||||
url: `/employee/get-template/${id}/`,
|
||||
data: { "instance_id": "{{employee.id}}" },
|
||||
dataType: "Json",
|
||||
success: function (response) {
|
||||
$('#ack-form-{{employee.id}} [name="body"]').html(response.body).change()
|
||||
$('#ack-form-{{employee.id}} [class="note-editable"]').html(response.body)
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
$(".note-dropzone").after('<button id="viewButton" background: #fefefe; border: solid #bfbaba 1px; border-radius: 10px; margin: 5px 2px 2px 2px; onclick="event.preventDefault();viewMail()">View</button>');
|
||||
$("#viewButton").after('<button id="previewButton" background: #fefefe; border: solid #bfbaba 1px; border-radius: 10px; margin: 5px 2px 2px 2px; onclick="event.preventDefault();previewMail()">Preview</button>');
|
||||
$('.note-toolbar').after('<div class = "note-editable d-none" id ="previewDiv"> </div>')
|
||||
|
||||
var selectedIds = JSON.parse($("#selectedInstances").attr("data-ids"));
|
||||
$("#employees[name=employees]select[multiple]").val(selectedIds).change()
|
||||
|
||||
});
|
||||
function templateChange(e) {
|
||||
var id = e.val();
|
||||
if (id.length) {
|
||||
$.ajax({
|
||||
type: "get",
|
||||
url: `/employee/get-template/${id}/`,
|
||||
data: { "instance_id": "{{employee.id}}" },
|
||||
dataType: "Json",
|
||||
success: function (response) {
|
||||
$('#ack-form-{{employee.id}} [name="body"]').html(response.body).change()
|
||||
$('#ack-form-{{employee.id}} [class="note-editable"]').html(response.body)
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
function previewMail(){
|
||||
var id = $('#template').val();
|
||||
var body = $('#ack-form-{{employee.id}} [name="body"]').val()
|
||||
if (id.length) {
|
||||
$.ajax({
|
||||
type: "get",
|
||||
url: `/employee/get-mail-preview/${employee.id}/`,
|
||||
data: {"body":body },
|
||||
dataType: "Json",
|
||||
success: function (response) {
|
||||
$('#ack-form-{{employee.id}} [class="note-editable"]').addClass('d-none')
|
||||
$('#previewDiv').html(response.body)
|
||||
$('#previewDiv').removeClass('d-none')
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function viewMail(){
|
||||
$('.note-editable').removeClass('d-none')
|
||||
$('#previewDiv').addClass('d-none')
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -259,6 +259,11 @@ urlpatterns = [
|
||||
not_in_out_dashboard.get_template,
|
||||
name="get-template-employee",
|
||||
),
|
||||
path(
|
||||
"get-mail-preview/<int:emp_id>/",
|
||||
not_in_out_dashboard.get_mail_preview,
|
||||
name="get-mail-preview",
|
||||
),
|
||||
path("view-policies/", policies.view_policies, name="view-policies"),
|
||||
path("search-policies", policies.search_policies, name="search-policies"),
|
||||
path("create-policy", policies.create_policy, name="create-policy"),
|
||||
|
||||
Reference in New Issue
Block a user