Compare commits

...

5 Commits

Author SHA1 Message Date
王思川 4f0b163810 Merge remote-tracking branch 'origin/master' into wsc2 2024-06-16 14:14:07 +08:00
王思川 4dd45e6a02 修复问题
1. 将一级部门和二级部门做得通用化
2024-06-16 13:36:04 +08:00
王思川 139fe1863f 修复问题
1. 修复了运营管理,编辑modal没有数据加载
2. 给表单里添加了一级部门和二级部门的联动加载
2024-06-16 13:30:21 +08:00
王思川 97b3d99ce6 修复问题
1. 修复了账号运营管理记录的编辑表单不显示内容问题
2. 修复了对象表单日期时间组件不显示时间的问题
2024-06-16 06:06:51 +08:00
王思川 c5c2c8cbd5 修复问题
1. 新增对象时,错误将表单提交到modify视图
2024-06-16 05:37:00 +08:00
7 changed files with 90 additions and 15 deletions

View File

@ -1,21 +1,25 @@
from django import forms
from common.forms import DepartmentSelectionForm
from .models import *
class SocialMediaAccountRegistrationForm(forms.ModelForm):
class SocialMediaAccountRegistrationForm(DepartmentSelectionForm, forms.ModelForm):
class Meta:
model = SocialMediaAccountRegistration
fields = '__all__'
widgets = {
'platform': forms.TextInput(attrs={'class': 'form-control'}),
'account_name': forms.TextInput(attrs={'class': 'form-control'}),
'primary_department': forms.TextInput(attrs={'class': 'form-control'}),
'secondary_department': forms.TextInput(attrs={'class': 'form-control'}),
'operation_account': forms.TextInput(attrs={'class': 'form-control'}),
'password': forms.PasswordInput(attrs={'class': 'form-control'}),
'password': forms.TextInput(attrs={'class': 'form-control'}),
'operator': forms.TextInput(attrs={'class': 'form-control'}),
}
def __init__(self, *args, **kwargs):
super(SocialMediaAccountRegistrationForm, self).__init__(*args, **kwargs)
class AccountOperationManagementForm(forms.ModelForm):
class Meta:

View File

@ -51,7 +51,7 @@ def sma_reg_list_view(request):
}
},
"query_params": query_params,
"table_exclude_field_name": ['record_id'],
"table_exclude_field_name": ['record_id', 'password'],
"form_action_url": 'sma_reg_list',
"modify_url": reverse("sma_reg_list_modify"),
"add_url": reverse("sma_reg_list_add"),
@ -97,9 +97,9 @@ def sma_reg_list_modify(request):
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
if 'record_id' in request.GET:
if 'id' in request.GET:
try:
instance = SocialMediaAccountRegistration.objects.get(record_id=request.GET['record_id'])
instance = SocialMediaAccountRegistration.objects.get(record_id=request.GET['id'])
form = SocialMediaAccountRegistrationForm(instance=instance)
except SocialMediaAccountRegistration.DoesNotExist:
raise Http404("对象不存在")
@ -213,9 +213,9 @@ def acc_op_mgmt_list_modify(request):
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
if 'record_id' in request.GET:
if 'id' in request.GET:
try:
instance = AccountOperationManagement.objects.get(record_id=request.GET['record_id'])
instance = AccountOperationManagement.objects.get(record_id=request.GET['id'])
form = AccountOperationManagementForm(instance=instance)
except AccountOperationManagement.DoesNotExist:
raise Http404("对象不存在")
@ -329,9 +329,9 @@ def web_reg_list_modify(request):
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
if 'record_id' in request.GET:
if 'id' in request.GET:
try:
instance = WebsiteRegistration.objects.get(record_id=request.GET['record_id'])
instance = WebsiteRegistration.objects.get(record_id=request.GET['id'])
form = WebsiteRegistrationForm(instance=instance)
except WebsiteRegistration.DoesNotExist:
raise Http404("对象不存在")
@ -440,9 +440,9 @@ def web_maint_rec_list_modify(request):
form_html = render_to_string('form_partial.html', {'form': form}, request)
return JsonResponse({"form_html": form_html, "errors": form.errors}, status=400)
elif request.method == 'GET':
if 'record_id' in request.GET:
if 'id' in request.GET:
try:
instance = WebsiteMaintenanceRecord.objects.get(record_id=request.GET['record_id'])
instance = WebsiteMaintenanceRecord.objects.get(record_id=request.GET['id'])
form = WebsiteMaintenanceRecordForm(instance=instance)
except WebsiteMaintenanceRecord.DoesNotExist:
raise Http404("对象不存在")

31
common/forms.py Normal file
View File

@ -0,0 +1,31 @@
from django import forms
from application.org_mgnt.models import PrimaryDepartment, SecondaryDepartment
class DepartmentSelectionForm(forms.Form):
primary_department = forms.ModelChoiceField(
queryset=PrimaryDepartment.objects.all(),
widget=forms.Select(attrs={'class': 'form-control'}),
empty_label="选择一级部门",
label="一级部门"
)
secondary_department = forms.ModelChoiceField(
queryset=SecondaryDepartment.objects.none(),
widget=forms.Select(attrs={'class': 'form-control'}),
empty_label="选择二级部门",
label="二级部门"
)
def __init__(self, *args, **kwargs):
super(DepartmentSelectionForm, self).__init__(*args, **kwargs)
if 'primary_department' in self.data:
try:
primary_department_id = int(self.data.get('primary_department'))
self.fields['secondary_department'].queryset = SecondaryDepartment.objects.filter(primary_department_id=primary_department_id).order_by('secondary_department_name')
except (ValueError, TypeError):
pass
elif 'instance' in kwargs and kwargs['instance']:
instance = kwargs['instance']
if hasattr(instance, 'primary_department'):
self.fields['secondary_department'].queryset = instance.primary_department.secondarydepartment_set.order_by('secondary_department_name')

View File

@ -7,6 +7,7 @@ urlpatterns = [
path('download_excel_template/<str:template_name>/<str:fields>/', download_excel_template, name='download_excel_template'),
path('common_excel_parse/', common_excel_parse, name='common_excel_parse'),
path('save_excel_data/', save_excel_table_data, name='save_excel_table_data'),
path('load_secondary_departments/', load_secondary_departments, name='load_secondary_departments'),
]

View File

@ -16,6 +16,8 @@ from django.http import FileResponse, HttpResponseNotFound, JsonResponse
from django.views.decorators.csrf import csrf_protect
from rest_framework.serializers import ModelSerializer
from application.org_mgnt.models import SecondaryDepartment
def error_page(request):
return render(request, 'error_page.html')
@ -241,3 +243,10 @@ def save_excel_table_data(request):
except Exception as e:
return JsonResponse({'error': f'服务器内部错误: {str(e)}'}, status=500)
return JsonResponse({'error': '无效的请求方法'}, status=400)
@login_required
def load_secondary_departments(request):
primary_department_id = request.GET.get('primary_department_id')
secondary_departments = SecondaryDepartment.objects.filter(primary_department_id=primary_department_id).order_by('secondary_department_name')
return JsonResponse(list(secondary_departments.values('secondary_department_id', 'secondary_department_name')), safe=False)

View File

@ -25,6 +25,8 @@
<textarea class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" rows="3">{{ field.value|default_if_none:'' }}</textarea>
{% elif field.field.widget.input_type == "date" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d" }}">
{% elif field.field.widget.input_type == "datetime-local" %}
<input type="datetime-local" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d\TH:i" }}">
{% elif field.field.widget.input_type == "email" %}
<input type="email" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|default_if_none:'' }}" placeholder="{{ field.label }}">
{% elif field.field.widget.input_type == "password" %}
@ -66,6 +68,8 @@
<textarea class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" rows="3">{{ field.value|default_if_none:'' }}</textarea>
{% elif field.field.widget.input_type == "date" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d" }}">
{% elif field.field.widget.input_type == "datetime-local" %}
<input type="datetime-local" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d\TH:i" }}">
{% elif field.field.widget.input_type == "month" %}
<input type="date" class="form-control" id="{{ field.id_for_label }}" name="{{ field.name }}" value="{{ field.value|date:"Y-m-d" }}">
{% elif field.field.widget.input_type == "email" %}
@ -97,3 +101,26 @@
<button type="submit" class="btn btn-primary" id="submitForm">保存</button>
</div>
</form>
<script type="text/javascript">
$(document).ready(function () {
$('#id_primary_department').change(function () {
var url = "{% url 'load_secondary_departments' %}"; // 更新为视图函数的URL
var primaryDepartmentId = $(this).val();
$.ajax({
url: url,
data: {
'primary_department_id': primaryDepartmentId
},
success: function (data) {
$('#id_secondary_department').html('');
$('#id_secondary_department').append('<option value="">选择二级部门</option>');
$.each(data, function (key, value) {
$('#id_secondary_department').append('<option value="' + value.id + '">' + value.secondary_department_name + '</option>');
});
}
});
});
});
</script>

View File

@ -177,6 +177,8 @@
$('#addEditModal .modal-body').html(response.form_html);
// 动态设置模态框标题
$('#formModalTitle').text('新增');
// 动态设置表单提交URL
$('#addEditForm').attr('action', add_url);
// 显示模态框
$('#addEditModal').modal('show');
@ -201,6 +203,8 @@
// 将表单 HTML 插入到模态框中
$('#addEditModal .modal-body').html(response.form_html);
$('#formModalTitle').text('编辑');
// 动态设置表单提交URL
$('#addEditForm').attr('action', modify_url);
// 显示模态框
$('#addEditModal').modal('show');
@ -219,14 +223,13 @@
$('#addEditForm').submit(function (e) {
e.preventDefault();
$.ajax({
url: modify_url, // 确保URL正确
url: $(this).attr('action'), // 动态获取URL
type: 'post', // 使用 POST 请求来提交表单
data: $(this).serialize(),
success: function (response) {
// 关闭模态框并显示成功消息
$('#addEditModal').modal('hide');
showAlert('success', response.message);
// 你可以在这里刷新表格或更新页面内容
location.reload();
},
error: function (xhr) {