XH_Digital_Management/application/asset_mgnt/views.py

297 lines
13 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, 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 common.auth import group_required, custom_permission_required
from common.utils.page_helper import paginate_query_and_assign_numbers
from .forms import FixedAssetsInventoryForm, IntangibleAssetsInventoryForm
from .models import *
# Create your views.txt here.
@login_required
@custom_permission_required('asset_mgnt.view_fixedassetsinventory')
def fixed_assets_list_view(request):
# 声明查询集
query_set = FixedAssetsInventory.objects.filter().order_by('-asset_id')
# 获取查询参数
asset_id = request.GET.get('asset_id', '')
brand = request.GET.get('brand', '')
# 根据提供的参数进行筛选
if asset_id:
query_set = query_set.filter(asset_id__icontains=asset_id)
if brand:
query_set = query_set.filter(brand__icontains=brand)
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
query_params = '&asset_id={}&brand={}'.format(asset_id, brand)
# Excel上传模板
template_name = "资产管理-固定资产清单-Excel上传模板.xlsx"
# 构建上下文
context = {
"model_config": 'asset_mgnt.FixedAssetsInventory',
"items": items,
"breadcrumb_list": [
{"title": "首页", "name": "index"},
{"title": "资产管理", "name": "index"},
{"title": "固定资产清单表", "name": "fixed_assets_list"}
],
"filters": [
{"type": "text", "id": "asset_id", "name": "asset_id", "label": "资产编号",
"placeholder": "请输入资产编号"},
{"type": "text", "id": "brand", "name": "brand", "label": "品牌", "placeholder": "请输入品牌"}
],
"excel_upload_config": {
"template_name": template_name,
"template_url": reverse("dl_excel_tpl", kwargs={'template_name': template_name}),
"parse_url": reverse("ep_common_parse"),
"save_url": reverse("save_excel_table_data_fix"),
"fields_preview_config": {
"asset_category": {"type": "text", "width": "180px"},
"brand": {"type": "text", "width": "180px"},
"model": {"type": "text", "width": "180px"},
"unit": {"type": "text", "width": "180px"},
"quantity": {"type": "number", "width": "100px"},
"affiliated_entity": {"type": "text", "width": "180px"},
"location": {"type": "text", "width": "180px"},
"department": {"type": "text", "width": "180px"},
"user": {"type": "text", "width": "180px"},
"purchase_date": {"type": "date", "width": "110px"},
"recorded_date": {"type": "date", "width": "110px"},
"original_value": {"type": "number", "width": "100px"},
"residual_value": {"type": "number", "width": "100px"},
"depreciation_method": {"type": "text", "width": "180px"},
"depreciation_years": {"type": "number", "width": "100px"},
"depreciation_months": {"type": "number", "width": "100px"},
"book_value": {"type": "number", "width": "100px"},
"status": {"type": "text", "width": "100px"},
}
},
"query_params": query_params,
"form_action_url": 'fixed_assets_list',
"modify_url": reverse("fixed_assets_list_modify"),
"add_url": reverse("fixed_assets_list_add"),
"delete_url": reverse("fixed_assets_list_delete"),
"table_exclude_field_name": ['asset_id'],
"add_button": True,
"import_excel_button": True
}
return render(request, 'items_list.html', context)
@login_required
@permission_required('asset_mgnt.add_fixedassetsinventory', raise_exception=True)
def fixed_assets_list_add(request):
if request.method == 'POST':
form = FixedAssetsInventoryForm(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 = FixedAssetsInventoryForm()
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('asset_mgnt.change_fixedassetsinventory', raise_exception=True)
def fixed_assets_list_modify(request):
if request.method == 'POST':
if 'asset_id' in request.POST:
instance = FixedAssetsInventory.objects.get(asset_id=request.POST['asset_id'])
form = FixedAssetsInventoryForm(request.POST, instance=instance)
else:
form = FixedAssetsInventoryForm(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 'asset_id' in request.GET:
try:
instance = FixedAssetsInventory.objects.get(asset_id=request.GET['asset_id'])
form = FixedAssetsInventoryForm(instance=instance)
except FixedAssetsInventory.DoesNotExist:
raise Http404("对象不存在")
else:
form = FixedAssetsInventoryForm()
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('asset_mgnt.delete_fixedassetsinventory', raise_exception=True)
def fixed_assets_list_delete(request):
if request.method == 'GET':
asset_id = request.GET.get('asset_id')
FixedAssetsInventory.objects.filter(asset_id=asset_id).delete()
return JsonResponse({"message": "删除成功"})
return JsonResponse({"message": "无效的请求方法"}, status=405)
@login_required
@permission_required('asset_mgnt.view_intangibleassetsinventory', raise_exception=True)
def intangible_assets_list_view(request):
# 声明查询集
query_set = IntangibleAssetsInventory.objects.filter().order_by('-asset_id')
# 获取查询参数
asset_id = request.GET.get('asset_id', '')
brand = request.GET.get('brand', '')
# 根据提供的参数进行筛选
if asset_id:
query_set = query_set.filter(asset_id__icontains=asset_id)
if brand:
query_set = query_set.filter(brand__icontains=brand)
# 对查询结果进行分页每页10条记录
items = paginate_query_and_assign_numbers(
request=request,
queryset=query_set,
per_page=10
)
# 构建上下文查询参数字符串
query_params = '&asset_id={}&brand={}'.format(asset_id, brand)
# Excel上传模板
template_name = "资产管理-无形资产清单-Excel上传模板.xlsx"
# 构建上下文
context = {
"model_config": 'asset_mgnt.IntangibleAssetsInventory',
"items": items,
"breadcrumb_list": [
{"title": "首页", "name": "index"},
{"title": "资产管理", "name": "index"},
{"title": "无形资产清单表", "name": "intangible_assets_list"}
],
"filters": [
{"type": "text", "id": "asset_id", "name": "asset_id", "label": "资产编号",
"placeholder": "请输入资产编号"},
{"type": "text", "id": "brand", "name": "brand", "label": "品牌", "placeholder": "请输入品牌"}
],
"excel_upload_config": {
"template_name": template_name,
"template_url": reverse("dl_excel_tpl", kwargs={'template_name': template_name}),
"parse_url": reverse("ep_common_parse"),
"save_url": reverse("save_excel_table_data_fix"),
"fields_preview_config": {
"asset_category": {"type": "text", "width": "180px"},
"brand": {"type": "text", "width": "180px"},
"model": {"type": "text", "width": "180px"},
"unit": {"type": "text", "width": "180px"},
"quantity": {"type": "number", "width": "100px"},
"affiliated_entity": {"type": "text", "width": "180px"},
"department": {"type": "text", "width": "180px"},
"user": {"type": "text", "width": "180px"},
"purchase_date": {"type": "date", "width": "110px"},
"recorded_date": {"type": "date", "width": "110px"},
"original_value": {"type": "number", "width": "100px"},
"residual_value": {"type": "number", "width": "100px"},
"amortization_method": {"type": "text", "width": "180px"},
"amortization_years": {"type": "number", "width": "100px"},
"amortization_months": {"type": "number", "width": "100px"},
"book_value": {"type": "number", "width": "100px"},
"status": {"type": "text", "width": "100px"},
}
},
"query_params": query_params,
"form_action_url": 'intangible_assets_list',
"modify_url": reverse("intangible_assets_list_modify"),
"add_url": reverse("intangible_assets_list_add"),
"delete_url": reverse("intangible_assets_list_delete"),
"table_exclude_field_name": ['asset_id'], # Exclude primary key
"add_button": True,
"import_excel_button": True
}
return render(request, 'items_list.html', context)
@login_required
@permission_required('asset_mgnt.add_intangibleassetsinventory', raise_exception=True)
def intangible_assets_list_add(request):
if request.method == 'POST':
form = IntangibleAssetsInventoryForm(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 = IntangibleAssetsInventoryForm()
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('asset_mgnt.change_intangibleassetsinventory', raise_exception=True)
def intangible_assets_list_modify(request):
if request.method == 'POST':
if 'asset_id' in request.POST:
instance = IntangibleAssetsInventory.objects.get(asset_id=request.POST['asset_id'])
form = IntangibleAssetsInventoryForm(request.POST, instance=instance)
else:
form = IntangibleAssetsInventoryForm(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 'asset_id' in request.GET:
try:
instance = IntangibleAssetsInventory.objects.get(asset_id=request.GET['asset_id'])
form = IntangibleAssetsInventoryForm(instance=instance)
except IntangibleAssetsInventory.DoesNotExist:
raise Http404("对象不存在")
else:
form = IntangibleAssetsInventoryForm()
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('asset_mgnt.delete_intangibleassetsinventory', raise_exception=True)
def intangible_assets_list_delete(request):
if request.method == 'GET':
asset_id = request.GET.get('asset_id')
IntangibleAssetsInventory.objects.filter(asset_id=asset_id).delete()
return JsonResponse({"message": "删除成功"})
return JsonResponse({"message": "无效的请求方法"}, status=405)