XH_Digital_Management/application/mkt_mgnt/views.py

381 lines
15 KiB
Python
Raw Normal View History

from django.contrib.auth.decorators import login_required, permission_required
2024-06-07 03:47:15 +08:00
from django.http import JsonResponse, Http404
from django.shortcuts import render, get_object_or_404, redirect
from django.template.loader import render_to_string
2024-06-06 15:08:38 +08:00
from django.urls import reverse
2024-06-13 17:49:47 +08:00
from rest_framework import status
from rest_framework.decorators import api_view, permission_classes
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
2024-06-07 14:20:58 +08:00
from application.mkt_mgnt.forms import CustomerForm, ProjectResourcesForm
2024-05-31 20:17:40 +08:00
from application.mkt_mgnt.models import *
2024-06-13 17:49:47 +08:00
from application.mkt_mgnt.serializers import ProjectResourceGiftSerializer, ProjectResourceMaintenanceSerializer, \
ProjectResourceGiftListSerializer
2024-05-31 20:17:40 +08:00
from common.utils.page_helper import paginate_query_and_assign_numbers
2024-05-29 09:27:39 +08:00
@login_required
@permission_required('mkt_mgnt.view_projectresources', raise_exception=True)
2024-05-31 20:17:40 +08:00
def proj_res_list_view(request):
# 声明查询集
query_set = ProjectResources.objects.filter().order_by('-resource_id')
2024-05-29 09:27:39 +08:00
2024-05-31 20:17:40 +08:00
# 获取查询参数
name = request.GET.get('name', '')
unit = request.GET.get('unit', '')
resource_manager = request.GET.get('resource_manager', '')
# 根据提供的参数进行筛选
2024-06-06 15:08:38 +08:00
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)
2024-05-31 20:17:40 +08:00
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
2024-06-06 15:08:38 +08:00
query_params = '&name={}&unit={}&resource_manager={}'.format(name, unit, resource_manager)
2024-06-06 20:56:06 +08:00
# Excel上传模板
2024-06-09 17:14:10 +08:00
template_name = "营销管理-项目资源-Excel上传模板.xlsx"
2024-06-06 20:56:06 +08:00
# 构建上下文
2024-05-31 20:17:40 +08:00
context = {
2024-06-06 20:56:06 +08:00
"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": "请输入单位"},
2024-06-07 03:47:15 +08:00
{"type": "text", "id": "resource_manager", "name": "resource_manager", "label": "资源维护人",
"placeholder": "请输入资源维护人"}
2024-06-06 20:56:06 +08:00
],
"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"),
2024-06-07 03:47:15 +08:00
"table_exclude_field_name": ['resource_id'], # Exclude primary key
2024-05-31 20:17:40 +08:00
}
2024-06-13 17:49:47 +08:00
return render(request, 'pj_list.html', context)
2024-05-31 20:17:40 +08:00
@login_required
@permission_required('mkt_mgnt.add_projectresources', raise_exception=True)
2024-06-06 15:08:38 +08:00
def proj_res_list_add(request):
if request.method == 'POST':
2024-06-07 03:47:15 +08:00
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)
2024-06-06 15:08:38 +08:00
@login_required
@permission_required('mkt_mgnt.change_projectresources', raise_exception=True)
2024-06-06 15:08:38 +08:00
def proj_res_list_modify(request):
if request.method == 'POST':
2024-06-07 03:47:15 +08:00
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)
2024-06-06 15:08:38 +08:00
@login_required
@permission_required('mkt_mgnt.delete_projectresources', raise_exception=True)
2024-06-06 15:08:38 +08:00
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)
2024-05-31 20:17:40 +08:00
def cust_list_view(request):
# 声明查询集
query_set = Customer.objects.filter().order_by('-customer_id')
# 获取查询参数
name = request.GET.get('name', '')
unit = request.GET.get('unit', '')
# 根据提供的参数进行筛选
2024-06-06 15:08:38 +08:00
if name:
query_set = query_set.filter(name__icontains=name)
if unit:
query_set = query_set.filter(unit__icontains=unit)
2024-05-31 20:17:40 +08:00
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
2024-06-06 15:08:38 +08:00
query_params = '&name={}&unit={}'.format(name, unit)
2024-06-06 20:56:06 +08:00
# Excel上传模板
template_name = "营销管理-客户-Excel上传模板.xlsx"
# 构建上下文
2024-05-31 20:17:40 +08:00
context = {
2024-06-06 20:56:06 +08:00
"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"),
2024-06-07 03:47:15 +08:00
"table_exclude_field_name": ['customer_id'], # Exclude primary key
2024-05-31 20:17:40 +08:00
}
2024-06-07 03:47:15 +08:00
return render(request, 'items_list.html', context)
2024-06-06 15:08:38 +08:00
@login_required
@permission_required('mkt_mgnt.add_customer', raise_exception=True)
2024-06-06 15:08:38 +08:00
def cust_list_add(request):
if request.method == 'POST':
2024-06-07 03:47:15 +08:00
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)
2024-06-06 15:08:38 +08:00
@login_required
@permission_required('mkt_mgnt.change_customer', raise_exception=True)
2024-06-06 15:08:38 +08:00
def cust_list_modify(request):
if request.method == 'POST':
2024-06-07 03:47:15 +08:00
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)
2024-06-06 15:08:38 +08:00
@login_required
@permission_required('mkt_mgnt.delete_customer', raise_exception=True)
2024-06-06 15:08:38 +08:00
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)
2024-06-13 17:49:47 +08:00
class StandardResultsSetPagination(PageNumberPagination):
page_size = 5
page_size_query_param = 'page_size'
max_page_size = 100
# ProjectResourceGift Views
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def gift_list(request, resource_id):
resource = get_object_or_404(ProjectResources, pk=resource_id)
gifts = ProjectResourceGift.objects.filter(resource=resource).order_by('-gift_date')
paginator = StandardResultsSetPagination()
paginated_gifts = paginator.paginate_queryset(gifts, request)
serializer = ProjectResourceGiftListSerializer(paginated_gifts, many=True)
return paginator.get_paginated_response(serializer.data)
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def add_gift(request):
serializer = ProjectResourceGiftSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['PUT'])
@permission_classes([IsAuthenticated])
def update_gift(request, id):
try:
gift = ProjectResourceGift.objects.get(pk=id)
except ProjectResourceGift.DoesNotExist:
return Response({"detail": "记录不存在"}, status=status.HTTP_404_NOT_FOUND)
serializer = ProjectResourceGiftSerializer(gift, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['DELETE'])
@permission_classes([IsAuthenticated])
def delete_gift(request, gift_id):
gift = get_object_or_404(ProjectResourceGift, pk=gift_id)
gift.delete()
return Response(status=status.HTTP_204_NO_CONTENT)
# ProjectResourceMaintenance Views
@api_view(['GET'])
@permission_classes([IsAuthenticated])
def maintenance_list(request, resource_id):
resource = get_object_or_404(ProjectResources, pk=resource_id)
maintenances = ProjectResourceMaintenance.objects.filter(resource=resource).order_by('-project')
paginator = StandardResultsSetPagination()
paginated_maintenances = paginator.paginate_queryset(maintenances, request)
serializer = ProjectResourceMaintenanceSerializer(paginated_maintenances, many=True)
return paginator.get_paginated_response(serializer.data)
@api_view(['POST'])
@permission_classes([IsAuthenticated])
def add_maintenance(request):
serializer = ProjectResourceMaintenanceSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['PUT'])
@permission_classes([IsAuthenticated])
def update_maintenance(request, id):
try:
maintenance = ProjectResourceMaintenance.objects.get(pk=id)
except ProjectResourceMaintenance.DoesNotExist:
return Response({"detail": "记录不存在"}, status=status.HTTP_404_NOT_FOUND)
serializer = ProjectResourceMaintenanceSerializer(maintenance, data=request.data, partial=True)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_200_OK)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
@api_view(['DELETE'])
@permission_classes([IsAuthenticated])
def delete_maintenance(request, maintenance_id):
maintenance = get_object_or_404(ProjectResourceMaintenance, pk=maintenance_id)
maintenance.delete()
return Response(status=status.HTTP_204_NO_CONTENT)