XH_Digital_Management/application/mkt_mgnt/views.py

384 lines
15 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from django.contrib.auth.decorators import login_required
from django.http import JsonResponse, Http404
from django.shortcuts import render, get_object_or_404
from django.template.loader import render_to_string
from django.urls import reverse
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
from application.mkt_mgnt.forms import CustomerForm, ProjectResourcesForm
from application.mkt_mgnt.models import *
from application.mkt_mgnt.serializers import ProjectResourceGiftSerializer, ProjectResourceMaintenanceSerializer, \
ProjectResourceGiftListSerializer
from common.auth import custom_permission_required
from common.utils.page_helper import paginate_query_and_assign_numbers
@login_required
@custom_permission_required('mkt_mgnt.view_projectresources')
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": {
"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"},
"is_enter": {"type": "text", "width": "180px"},
"project_name": {"type": "text", "width": "180px"},
}
},
"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
"add_button": True,
"import_excel_button": True
}
return render(request, 'pj_list.html', context)
@login_required
@custom_permission_required('mkt_mgnt.add_projectresources')
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
@custom_permission_required('mkt_mgnt.change_projectresources')
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
@custom_permission_required('mkt_mgnt.delete_projectresources')
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
@custom_permission_required('mkt_mgnt.view_customer')
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": {
"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"},
}
},
"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
"add_button": True,
"import_excel_button": True
}
return render(request, 'items_list.html', context)
@login_required
@custom_permission_required('mkt_mgnt.add_customer')
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
@custom_permission_required('mkt_mgnt.change_customer')
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
@custom_permission_required('mkt_mgnt.delete_customer')
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)
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)