from django.contrib.auth.decorators import login_required, permission_required 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 from application.mkt_mgnt.forms import CustomerForm, ProjectResourcesForm from application.mkt_mgnt.models import * from common.utils.page_helper import paginate_query_and_assign_numbers @login_required @permission_required('mkt_mgnt.view_projectresources', raise_exception=True) def proj_res_list_view(request): # 声明查询集 query_set = ProjectResources.objects.filter().order_by('-resource_id') # 获取查询参数 name = request.GET.get('name', '') unit = request.GET.get('unit', '') resource_manager = request.GET.get('resource_manager', '') # 根据提供的参数进行筛选 if name: query_set = query_set.filter(name__icontains=name) if unit: query_set = query_set.filter(unit__icontains=unit) if resource_manager: query_set = query_set.filter(resource_manager__name__icontains=resource_manager) # 对查询结果进行分页,每页10条记录 items = paginate_query_and_assign_numbers( request=request, queryset=query_set, per_page=10 ) # 构建上下文查询参数字符串 query_params = '&name={}&unit={}&resource_manager={}'.format(name, unit, resource_manager) # Excel上传模板 template_name = "营销管理-项目资源表-Excel上传模板.xlsx" # 构建上下文 context = { "model_config": 'mkt_mgnt.ProjectResources', "items": items, "breadcrumb_list": [ {"title": "首页", "name": "index"}, {"title": "营销管理", "name": "index"}, {"title": "项目资源表", "name": "proj_res_list"} ], "filters": [ {"type": "text", "id": "name", "name": "name", "label": "姓名", "placeholder": "请输入姓名"}, {"type": "text", "id": "unit", "name": "unit", "label": "单位", "placeholder": "请输入单位"}, {"type": "text", "id": "resource_manager", "name": "resource_manager", "label": "资源维护人", "placeholder": "请输入资源维护人"} ], "excel_upload_config": { "template_url": reverse("download_template", kwargs={'template_name': template_name}), "parse_url": reverse("common_excel_parse"), "save_url": reverse("save_excel_table_data"), "fields_preview_config": { "resource_id": {"type": "text", "width": "180px"}, "name": {"type": "text", "width": "180px"}, "unit": {"type": "text", "width": "180px"}, "position": {"type": "text", "width": "180px"}, "contact_phone": {"type": "text", "width": "180px"}, "contact_address": {"type": "text", "width": "180px"}, "resource_manager": {"type": "text", "width": "180px"}, "actions": {"type": "actions", "width": "100px"} } }, "query_params": query_params, "form_action_url": 'proj_res_list', "modify_url": reverse("proj_res_list_modify"), "add_url": reverse("proj_res_list_add"), "delete_url": reverse("proj_res_list_delete"), "table_exclude_field_name": ['resource_id'], # Exclude primary key } return render(request, 'items_list.html', context) @login_required @permission_required('mkt_mgnt.add_projectresources', raise_exception=True) def proj_res_list_add(request): if request.method == 'POST': form = ProjectResourcesForm(request.POST) if form.is_valid(): form.save() return JsonResponse({"message": "添加成功"}) else: 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': form = ProjectResourcesForm() form_html = render_to_string('form_partial.html', {'form': form}, request) return JsonResponse({"form_html": form_html}) else: return JsonResponse({"message": "无效的请求方法"}, status=405) @login_required @permission_required('mkt_mgnt.change_projectresources', raise_exception=True) def proj_res_list_modify(request): if request.method == 'POST': if 'resource_id' in request.POST: instance = ProjectResources.objects.get(resource_id=request.POST['resource_id']) form = ProjectResourcesForm(request.POST, instance=instance) else: form = ProjectResourcesForm(request.POST) if form.is_valid(): form.save() return JsonResponse({"message": "保存成功"}) else: 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 'resource_id' in request.GET: try: instance = ProjectResources.objects.get(resource_id=request.GET['resource_id']) form = ProjectResourcesForm(instance=instance) except ProjectResources.DoesNotExist: raise Http404("对象不存在") else: form = ProjectResourcesForm() form_html = render_to_string('form_partial.html', {'form': form}, request) return JsonResponse({"form_html": form_html}) else: return JsonResponse({"message": "无效的请求方法"}, status=405) @login_required @permission_required('mkt_mgnt.delete_projectresources', raise_exception=True) def proj_res_list_delete(request): if request.method == 'GET': resource_id = request.GET.get('resource_id') ProjectResources.objects.filter(resource_id=resource_id).delete() return JsonResponse({"message": "删除成功"}) return JsonResponse({"message": "无效的请求方法"}, status=405) @login_required @permission_required('mkt_mgnt.view_customer', raise_exception=True) def cust_list_view(request): # 声明查询集 query_set = Customer.objects.filter().order_by('-customer_id') # 获取查询参数 name = request.GET.get('name', '') unit = request.GET.get('unit', '') # 根据提供的参数进行筛选 if name: query_set = query_set.filter(name__icontains=name) if unit: query_set = query_set.filter(unit__icontains=unit) # 对查询结果进行分页,每页10条记录 items = paginate_query_and_assign_numbers( request=request, queryset=query_set, per_page=10 ) # 构建上下文查询参数字符串 query_params = '&name={}&unit={}'.format(name, unit) # Excel上传模板 template_name = "营销管理-客户-Excel上传模板.xlsx" # 构建上下文 context = { "model_config": 'mkt_mgnt.Customer', "items": items, "breadcrumb_list": [ {"title": "首页", "name": "index"}, {"title": "营销管理", "name": "index"}, {"title": "客户", "name": "cust_list"} ], "filters": [ {"type": "text", "id": "name", "name": "name", "label": "姓名", "placeholder": "请输入姓名"}, {"type": "text", "id": "unit", "name": "unit", "label": "单位", "placeholder": "请输入单位"} ], "excel_upload_config": { "template_url": reverse("download_template", kwargs={'template_name': template_name}), "parse_url": reverse("common_excel_parse"), "save_url": reverse("save_excel_table_data"), "fields_preview_config": { "customer_id": {"type": "text", "width": "180px"}, "relationship_type": {"type": "text", "width": "180px"}, "detailed_description": {"type": "text", "width": "180px"}, "name": {"type": "text", "width": "180px"}, "unit": {"type": "text", "width": "180px"}, "position": {"type": "text", "width": "180px"}, "residence": {"type": "text", "width": "180px"}, "cooperation": {"type": "text", "width": "180px"}, "main_organizer": {"type": "text", "width": "180px"}, "assistant": {"type": "text", "width": "180px"}, "actions": {"type": "actions", "width": "100px"} } }, "query_params": query_params, "form_action_url": 'cust_list', "modify_url": reverse("cust_list_modify"), "add_url": reverse("cust_list_add"), "delete_url": reverse("cust_list_delete"), "table_exclude_field_name": ['customer_id'], # Exclude primary key } return render(request, 'items_list.html', context) @login_required @permission_required('mkt_mgnt.add_customer', raise_exception=True) def cust_list_add(request): if request.method == 'POST': form = CustomerForm(request.POST) if form.is_valid(): form.save() return JsonResponse({"message": "添加成功"}) else: 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': form = CustomerForm() form_html = render_to_string('form_partial.html', {'form': form}, request) return JsonResponse({"form_html": form_html}) else: return JsonResponse({"message": "无效的请求方法"}, status=405) @login_required @permission_required('mkt_mgnt.change_customer', raise_exception=True) def cust_list_modify(request): if request.method == 'POST': if 'customer_id' in request.POST: instance = Customer.objects.get(customer_id=request.POST['customer_id']) form = CustomerForm(request.POST, instance=instance) else: form = CustomerForm(request.POST) if form.is_valid(): form.save() return JsonResponse({"message": "保存成功"}) else: 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 'customer_id' in request.GET: try: instance = Customer.objects.get(customer_id=request.GET['customer_id']) form = CustomerForm(instance=instance) except Customer.DoesNotExist: raise Http404("对象不存在") else: form = CustomerForm() form_html = render_to_string('form_partial.html', {'form': form}, request) return JsonResponse({"form_html": form_html}) else: return JsonResponse({"message": "无效的请求方法"}, status=405) @login_required @permission_required('mkt_mgnt.delete_customer', raise_exception=True) def cust_list_delete(request): if request.method == 'GET': customer_id = request.GET.get('customer_id') Customer.objects.filter(customer_id=customer_id).delete() return JsonResponse({"message": "删除成功"}) return JsonResponse({"message": "无效的请求方法"}, status=405)