Compare commits
5 Commits
4ea98bedaf
...
4f0b163810
Author | SHA1 | Date |
---|---|---|
王思川 | 4f0b163810 | |
王思川 | 4dd45e6a02 | |
王思川 | 139fe1863f | |
王思川 | 97b3d99ce6 | |
王思川 | c5c2c8cbd5 |
|
@ -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:
|
||||
|
|
|
@ -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("对象不存在")
|
||||
|
|
|
@ -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')
|
|
@ -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'),
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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>
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue