新建account_profile
This commit is contained in:
parent
57be495664
commit
fb9dc0d69a
|
@ -21,7 +21,7 @@
|
|||
</div>
|
||||
<div class="col-md-4 mt-4 text-end">
|
||||
<button class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#modifyRecordsModal">修改记录</button>
|
||||
<button class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#addModifyModal">添加</button>
|
||||
<button class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#createAccountProfileModal">添加</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
@ -77,4 +77,119 @@
|
|||
|
||||
{% include "authority_modal.html" %}
|
||||
|
||||
<!-- 模态框 -->
|
||||
<div class="modal fade" id="createAccountProfileModal" tabindex="-1" aria-labelledby="createAccountProfileLabel" aria-hidden="true">
|
||||
<div class="modal-dialog modal-lg">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<h5 class="modal-title" id="createAccountProfileLabel">创建账户</h5>
|
||||
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<!-- 员工信息列表 -->
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col"><input type="checkbox" id="selectAll"></th>
|
||||
<th scope="col">名称</th>
|
||||
<th scope="col">邮箱</th>
|
||||
<th scope="col">手机号</th>
|
||||
<th scope="col">一级部门</th>
|
||||
<th scope="col">职务</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="employeeList">
|
||||
<!-- 员工信息行将通过服务器端代码动态生成 -->
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
|
||||
<button type="button" class="btn btn-primary" id="createAccountProfiles">创建账户</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- JavaScript 用于处理批量创建账户 -->
|
||||
<script>
|
||||
// 页面加载完成后获取员工信息列表
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
fetchEmployeeList();
|
||||
});
|
||||
|
||||
// 获取员工列表的函数
|
||||
function fetchEmployeeList() {
|
||||
fetch('{% url 'emp_list_for_create_account_profile' %}', { // 替换为实际的URL路径
|
||||
headers: {
|
||||
'X-CSRFToken': '{{ csrf_token }}' // 确保添加CSRF令牌
|
||||
}
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if(data.success) {
|
||||
const employeeList = document.getElementById('employeeList');
|
||||
employeeList.innerHTML = ''; // 清空现有列表
|
||||
data.employees.forEach(employee => {
|
||||
employeeList.innerHTML += `
|
||||
<tr>
|
||||
<td><input type="checkbox" value="${employee.employee_id}"></td>
|
||||
<td>${employee.name}</td>
|
||||
<td>${employee.email}</td>
|
||||
<td>${employee.mobile_number}</td>
|
||||
<td>${employee.primary_department}</td>
|
||||
<td>${employee.position}</td>
|
||||
</tr>
|
||||
`;
|
||||
});
|
||||
} else {
|
||||
alert("获取员工列表失败:" + data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
}
|
||||
|
||||
// 创建账户的事件处理函数
|
||||
document.getElementById('createAccountProfiles').addEventListener('click', function() {
|
||||
const selectedEmployeeIds = [];
|
||||
document.querySelectorAll('#employeeList input[type="checkbox"]:checked').forEach(checkbox => {
|
||||
selectedEmployeeIds.push(checkbox.value);
|
||||
});
|
||||
|
||||
fetch('{% url 'create_account_profile' %}', { // 替换为实际的URL路径
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ employee_ids: selectedEmployeeIds }),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': '{{ csrf_token }}' // 确保添加CSRF令牌
|
||||
},
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if(data.success) {
|
||||
// 处理成功逻辑
|
||||
alert(data.message);
|
||||
$('#createAccountProfileModal').modal('hide');
|
||||
fetchEmployeeList(); // 刷新员工列表
|
||||
} else {
|
||||
// 处理失败逻辑
|
||||
alert(data.message);
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
});
|
||||
|
||||
// 全选复选框的事件处理函数
|
||||
document.getElementById('selectAll').addEventListener('click', function(event) {
|
||||
const checked = event.target.checked;
|
||||
document.querySelectorAll('#employeeList input[type="checkbox"]').forEach(checkbox => {
|
||||
checkbox.checked = checked;
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
|
@ -2,9 +2,7 @@ from django.urls import path, include
|
|||
|
||||
from django.contrib.auth import views as auth_views
|
||||
|
||||
from application.accounts.views import logout_view, user_permissions_list, get_user_existing_permissions, \
|
||||
save_user_permissions, refresh_user_permissions, group_perm_list, get_group_permissions, save_group_permissions, \
|
||||
user_homepage_view
|
||||
from .views import *
|
||||
|
||||
urlpatterns = [
|
||||
path('login/', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='user_login'),
|
||||
|
@ -18,4 +16,5 @@ urlpatterns = [
|
|||
path('groups_permissions/<int:group_id>/', get_group_permissions, name='get_group_permissions'),
|
||||
path('groups_save_permissions/<int:group_id>/', save_group_permissions, name='save_group_permissions'),
|
||||
path('homepage', user_homepage_view, name='user_homepage'),
|
||||
path('create_account_profile', create_account_profile, name='create_account_profile'),
|
||||
]
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
# 标准库导入
|
||||
import json
|
||||
from datetime import datetime
|
||||
|
||||
from django.db import transaction
|
||||
from django.http import JsonResponse, HttpResponseBadRequest
|
||||
from django.shortcuts import redirect, render, get_object_or_404
|
||||
from django.utils.decorators import method_decorator
|
||||
|
@ -17,9 +19,12 @@ from django.views.decorators.http import require_http_methods
|
|||
from XH_Digital_Management import settings
|
||||
# 本地Django应用导入
|
||||
from application.accounts.models import AccountProfile
|
||||
from application.hrm_mgnt.models import EmployeeInformation
|
||||
from common.auth import group_required
|
||||
from common.utils.page_helper import paginate_query_and_assign_numbers
|
||||
|
||||
from pypinyin import lazy_pinyin
|
||||
|
||||
|
||||
def format_permissions(permissions):
|
||||
action_prefixes = ['Can add ', 'Can change ', 'Can delete ', 'Can view ']
|
||||
|
@ -84,7 +89,7 @@ class CustomLoginView(LoginView):
|
|||
def logout_view(request):
|
||||
logout(request)
|
||||
# Redirect to a success page, such as the home page
|
||||
return redirect('user_homepage')
|
||||
return redirect('user_login')
|
||||
|
||||
|
||||
@login_required
|
||||
|
@ -99,7 +104,7 @@ def user_permissions_list(request):
|
|||
query_set = query_set.filter(employee_information__name__icontains=name)
|
||||
if primary_department:
|
||||
query_set = query_set.filter(employee_information__primary_department=primary_department)
|
||||
query_set = query_set.filter(user__is_superuser=True)
|
||||
# query_set = query_set.filter(user__is_superuser=True)
|
||||
|
||||
items = paginate_query_and_assign_numbers(
|
||||
request=request,
|
||||
|
@ -444,3 +449,58 @@ def user_homepage_view(request):
|
|||
'user': request.user,
|
||||
}
|
||||
return render(request, 'user_homepage.html', context)
|
||||
|
||||
|
||||
@login_required
|
||||
@group_required('系统管理')
|
||||
def create_account_profile(request):
|
||||
if request.method == 'POST':
|
||||
data = json.loads(request.body)
|
||||
employee_info_ids = data.get('employee_ids') # 获取employee_ids列表
|
||||
|
||||
# 检查是否提供了必填信息
|
||||
if not employee_info_ids:
|
||||
return JsonResponse({'success': False, 'message': 'Employee Information IDs are required.'})
|
||||
|
||||
# 在事务中创建AccountProfile
|
||||
with transaction.atomic():
|
||||
created_profiles = []
|
||||
for employee_info_id in employee_info_ids:
|
||||
# 尝试获取 employee_information 实例
|
||||
try:
|
||||
employee_information = EmployeeInformation.objects.get(employee_id=employee_info_id)
|
||||
except EmployeeInformation.DoesNotExist as e:
|
||||
return JsonResponse({'success': False, 'message': str(e)})
|
||||
|
||||
# 检查 employee_information 是否已经有对应的AccountProfile
|
||||
if AccountProfile.objects.filter(employee_information=employee_information).exists():
|
||||
return JsonResponse(
|
||||
{'success': False, 'message': f'Employee Information ID {employee_info_id} is already bound to an AccountProfile.'})
|
||||
|
||||
# 将姓名转换为拼音作为用户名
|
||||
username_base = ''.join(lazy_pinyin(employee_information.name))
|
||||
username = username_base
|
||||
counter = 1
|
||||
while User.objects.filter(username=username).exists():
|
||||
username = f"{username_base}{counter}"
|
||||
counter += 1
|
||||
|
||||
# 创建默认密码
|
||||
date_str = datetime.now().strftime('%Y%m%d')
|
||||
password = f"{username_base}{date_str}"
|
||||
|
||||
# 创建新的 User 实例
|
||||
user = User.objects.create_user(
|
||||
username=username,
|
||||
email=employee_information.email,
|
||||
password=password
|
||||
)
|
||||
|
||||
# 创建新的AccountProfile实例
|
||||
account_profile = AccountProfile.objects.create(
|
||||
user=user,
|
||||
employee_information=employee_information
|
||||
)
|
||||
created_profiles.append(account_profile)
|
||||
|
||||
return JsonResponse({'success': True, 'message': f'Created {len(created_profiles)} AccountProfiles successfully.'})
|
||||
|
|
|
@ -27,4 +27,6 @@ urlpatterns = [
|
|||
path('pst_list/add/', pst_list_add, name='pst_list_add'),
|
||||
path('pst_list/modify/', pst_list_modify, name='pst_list_modify'),
|
||||
path('pst_list/delete/', pst_list_delete, name='pst_list_delete'),
|
||||
|
||||
path('emp_list_for_create_account_profile/', emp_list_for_create_account_profile, name='emp_list_for_create_account_profile'),
|
||||
]
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
from django.http import JsonResponse, Http404
|
||||
from django.core.paginator import Paginator
|
||||
from django.http import JsonResponse, Http404
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from django.template.loader import render_to_string
|
||||
from django.urls import reverse
|
||||
|
@ -669,3 +670,35 @@ def pst_list_delete(request):
|
|||
Position.objects.filter(position_id=position_id).delete()
|
||||
return JsonResponse({"message": "删除成功"})
|
||||
return JsonResponse({"message": "无效的请求方法"}, status=405)
|
||||
|
||||
|
||||
def emp_list_for_create_account_profile(request):
|
||||
# 查询还没有创建AccountProfile的员工信息
|
||||
query_set = EmployeeInformation.objects.exclude(account_profile__isnull=False).order_by('-employee_id')
|
||||
|
||||
# 获取查询参数
|
||||
name = request.GET.get('name', '')
|
||||
|
||||
# 根据提供的参数进行筛选
|
||||
if name:
|
||||
query_set = query_set.filter(name__icontains=name)
|
||||
|
||||
# 对查询结果进行分页
|
||||
paginator = Paginator(query_set, 10) # 每页显示10条记录
|
||||
page_number = request.GET.get('page')
|
||||
page_obj = paginator.get_page(page_number)
|
||||
|
||||
# 创建员工信息的列表,包含需要的字段
|
||||
employees = list(page_obj.object_list.values(
|
||||
'employee_id', 'name', 'email', 'mobile_number', 'primary_department', 'position'
|
||||
))
|
||||
|
||||
# 返回JSON响应
|
||||
return JsonResponse({
|
||||
'success': True,
|
||||
'employees': employees,
|
||||
'has_previous': page_obj.has_previous(),
|
||||
'has_next': page_obj.has_next(),
|
||||
'num_pages': paginator.num_pages,
|
||||
'current_page': page_obj.number
|
||||
})
|
||||
|
|
|
@ -7,3 +7,4 @@ sqlparse==0.5.0
|
|||
openpyxl==3.1.3
|
||||
numpy==1.26.4
|
||||
pytz==2024.1
|
||||
pypinyin==0.51.0
|
Loading…
Reference in New Issue