mirror of
https://github.com/RobbieHan/sandboxMP.git
synced 2026-02-13 23:54:58 +08:00
61 lines
2.1 KiB
Python
61 lines
2.1 KiB
Python
# @Time : 2018/10/18 23:04
|
|
# @Author : RobbieHan
|
|
# @File : views_structure.py
|
|
|
|
import json
|
|
|
|
from django.views.generic.base import TemplateView
|
|
from django.views.generic.base import View
|
|
from django.shortcuts import render
|
|
from django.shortcuts import HttpResponse
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from .mixin import LoginRequiredMixin
|
|
from .models import Structure
|
|
from .forms import StructureForm
|
|
|
|
|
|
class StructureView(LoginRequiredMixin, TemplateView):
|
|
|
|
template_name = 'system/structure/structure.html'
|
|
|
|
|
|
class StructureCreateView(LoginRequiredMixin, View):
|
|
|
|
def get(self, request):
|
|
ret = dict(structure_all=Structure.objects.all())
|
|
if 'id' in request.GET and request.GET['id']:
|
|
structure = get_object_or_404(Structure, pk=request.GET['id'])
|
|
ret['structure'] = structure
|
|
return render(request, 'system/structure/structure_create.html', ret)
|
|
|
|
def post(self, request):
|
|
res = dict(result=False)
|
|
if 'id' in request.POST and request.POST['id']:
|
|
structure = get_object_or_404(Structure, pk=request.POST['id'])
|
|
else:
|
|
structure = Structure()
|
|
structure_form = StructureForm(request.POST, instance=structure)
|
|
if structure_form.is_valid():
|
|
structure_form.save()
|
|
res['result'] = True
|
|
return HttpResponse(json.dumps(res), content_type='application/json')
|
|
|
|
|
|
class StructureListView(LoginRequiredMixin, View):
|
|
|
|
def get(self, request):
|
|
fields = ['id', 'name', 'type', 'parent__name']
|
|
ret = dict(data=list(Structure.objects.values(*fields)))
|
|
return HttpResponse(json.dumps(ret), content_type='application/json')
|
|
|
|
|
|
class StructureDeleteView(LoginRequiredMixin, View):
|
|
|
|
def post(self, request):
|
|
ret = dict(result=False)
|
|
if 'id' in request.POST and request.POST['id']:
|
|
id_list = map(int, request.POST['id'].split(','))
|
|
Structure.objects.filter(id__in=id_list).delete()
|
|
ret['result'] = True
|
|
return HttpResponse(json.dumps(ret), content_type='application/json') |