newmediamonitoring/management/views.py

116 lines
4.5 KiB
Python

import json
from django.contrib import messages
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from dashboard.models import Userprofile, Group, Organization, Level
def user_management(request):
user = User.objects.all()
return render(request,'management/user-management.html',{'userallinfo':user})
def user_detail(request,pk):
user = User.objects.get(id=pk)
userprofile = Userprofile.objects.get(user_id=pk)
return HttpResponse(json.dumps({
"status":"1",
"id":str(user.id),
"username":user.username,
"first_name":user.first_name,
"last_login":str(user.last_login).split('.')[0],
"date_joined":str(user.date_joined).split('.')[0],
"name":userprofile.name,
"organization":userprofile.organization.name,
}))
def user_delete(request,pk):
user = User.objects.get(id=pk)
user.delete()
return HttpResponseRedirect('/management/user/management/')
def group_management(request):
group = Group.objects.all()
return render(request,'management/group-management.html',{'group':group})
def group_detail(request,pk):
group = Group.objects.get(id=pk)
print(group)
return HttpResponse(json.dumps({
"status":"1",
"id": str(group.id),
"name":group.name,
"created":str(group.created)
}))
def group_update(request,pk):
group = Group.objects.get(id=pk)
if request.method == 'POST':
name = request.POST.get('name')
Group.objects.filter(id=pk).update(name=name)
messages.success(request,'修改成功')
return HttpResponseRedirect('/management/group/management/')
return render(request,'management/group-management-update.html',{'group':group})
def group_create(request):
if request.method == 'POST':
name = request.POST.get('name')
if name is not None:
group = Group(name=name)
group.save()
messages.success(request,'添加成功')
else:
messages.error(request,'群组名不能为空')
return HttpResponseRedirect('/management/group/management/')
return render(request,'management/group-management-create.html')
def group_delete(request,pk):
group = Group.objects.get(id=pk)
group.delete()
messages.success(request,'删除成功')
return HttpResponseRedirect('/management/group/management/')
def organization_management(request):
organization = Organization.objects.all()
return render(request,'management/organization-management.html',{"organization":organization})
def organization_detail(request,pk):
organization = Organization.objects.get(id=pk)
return HttpResponse(json.dumps({
"status":"1",
"id":str(organization.id),
"name":organization.name,
"group":organization.group.name,
"level":organization.level.name,
"created":str(organization.created).split('.')[0],
}))
def organization_delete(request,pk):
organization = Organization.objects.get(id=pk)
organization.delete()
messages.success(request, '删除成功')
return HttpResponseRedirect('/management/organization/management/')
def organization_update(request,pk):
organization = Organization.objects.get(id=pk)
group = Group.objects.all()
level = Level.objects.all()
if request.method == 'POST':
name = request.POST.get('name')
group = request.POST.get('group')
level = request.POST.get('level')
Organization.objects.filter(id=pk).update(name=name,group=group,level=level)
messages.success(request, '修改成功')
return HttpResponseRedirect('/management/organization/management/')
return render(request,'management/organization-management-update.html',{'organization':organization,'level':level,'group':group})
def organization_create(request):
group = Group.objects.all()
level = Level.objects.all()
if request.method == 'POST':
name = request.POST.get('name')
group = request.POST.get('group')
level = request.POST.get('level')
if name is not None:
organization = Organization(name=name,group_id=group,level_id=level)
organization.save()
messages.success(request,'添加成功')
else:
messages.error(request,'单位名不能为空')
return HttpResponseRedirect('/management/organization/management/')
return render(request,'management/organization-management-create.html',{'level':level,'group':group})