import datetime
from django.shortcuts import render, redirect
from django.views import View
from web.config import upload, get_excel_c, read_excel
from web.models import *
class Index(View):
def get(self, request):
return render(request, 'login.html')
def post(self, request):
type_uuid = int(request.POST.get('type'))
account = request.POST.get('username')
password = request.POST.get('password')
users = User.objects.filter(types=type_uuid, account=account, password=password)
if users:
user = users[0]
request.session['user'] = {
"role": type_uuid,
'account': account,
'id': user.id,
}
if type_uuid == '1':
admin = True
return redirect("/home/")
if type_uuid == '2':
manage = True
return redirect("/home/")
if type_uuid == '3':
czy = True
return redirect("/kk/")
return redirect('/home/')
else:
error = '不存在该账户'
return render(request, 'login.html', locals())
import os
from django.db.models import Q
from django.core.paginator import Paginator
class Home(View):
def get(self, request):
delete_id = request.GET.get('delete_id')
page = request.GET.get('page', 1)
if delete_id:
u = User.objects.get(id=int(delete_id))
ms = Manage.objects.filter(user=u)
for i in ms:
i.delete()
u.delete()
return redirect('/home/?page=' + page)
users_list = User.objects.filter(Q(types=None))
paginator = Paginator(users_list, 5)
users = paginator.page(int(page))
return render(request, 'UserManagement.html', locals())
def post(self, request):
obj = request.FILES.get('file')
type = request.POST.get('type')
if type == 'upload':
if 'xlsx' != obj.name.split('.')[1]:
error = '文件类型错误'
return render(request, 'UserManagement.html', locals())
else:
f = open(os.path.join('media/3.xlsx'), 'wb')
for chunk in obj.chunks():
f.write(chunk)
f.close()
data = upload()
if not data:
error = "数据不完成,缺少用户图像"
else:
ne = 1
for i in data:
h = User.objects.filter(number=i[1])
if not h:
if os.path.isfile('static/media/image{0}.png'.format(str(ne))):
user = User(number=i[1], display_name=i[2], sex=i[3], class_name=i[4],
img='image{0}.png'.format(str(ne)))
user.save()
else:
user = User(number=i[1], display_name=i[2], sex=i[3], class_name=i[4],
img='image{0}.jpeg'.format(str(ne)))
user.save()
ne += 1
return redirect('/home/')
from django.http import FileResponse
class Cd(View):
def get(self, request):
delete_id = request.GET.get('delete_id')
page = request.GET.get('page', 1)
time = request.GET.get('date')
if time:
users_lists = Manage.objects.filter(types='cd').filter(time__contains=time)
sql = "select a.display_name,a.sex,a.class_name,b.time,b.total_hours FROM web_user a RIGHT JOIN web_manage b ON a.id=b.user_id WHERE b.time like '{0}' and b.types='cd'".format(
'%%' + time + '%%')
key = ()
lists = ["姓名", '性别', '班级', '迟到时间', '迟到时长']
get_excel_c(sql, key, lists)
file = open('media/ok.xls', 'rb')
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{0}.xls"'.format(time)
return response
users_list = Manage.objects.filter(types='cd')
if delete_id:
u = User.objects.get(id=int(delete_id))
u.delete()
return redirect('/cd/?page=' + page)
paginator = Paginator(users_list, 5)
users = paginator.page(int(page))
return render(request, 'OrderManagement.html', locals())
def post(self, request):
search = request.POST.get('search')
time = request.POST.get('time')
if search:
sh_users = User.objects.filter(Q(display_name=search) | Q(number=search))
if sh_users:
sh_user = sh_users[0]
else:
error = "不存在此人信息"
users_list = Manage.objects.filter(types='cd')
paginator = Paginator(users_list, 5)
page = 1
users = paginator.page(int(page))
return render(request, 'OrderManagement.html', locals())
if time:
type = request.POST.get("type")
user = request.POST.get("user")
t = datetime.datetime.now().strftime('%Y-%m-%d %H:%M-%S')
m = Manage(total_hours=time, user=User.objects.get(id=int(user)), time=t, types='cd')
m.save()
sh_user = None
error = '成功'
return redirect('/cd/')
class Wg(View):
def get(self, request):
delete_id = request.GET.get('delete_id')
page = request.GET.get('page', 1)
time = request.GET.get('date')
if time:
users_lists = Manage.objects.filter(types='wg').filter(time__contains=time)
sql = "select a.number,a.display_name,a.sex,a.class_name,b.time,b.total_hours FROM web_user a RIGHT JOIN web_manage b ON a.id=b.user_id WHERE b.time like '{0}' and b.types='wg'".format(
'%%' + time + '%%')
key = ()
lists = ["学号", "姓名", '性别', '班级', '晚归时间', '晚归时长']
get_excel_c(sql, key, lists)
file = open('media/ok.xls', 'rb')
response = FileResponse(file)
response['Content-Type'] = 'application/octet-stream'
response['Content-Disposition'] = 'attachment;filename="{0}.xls"'.format(time)
return response
users_list = Manage.objects.filter(types='wg')
if delete_id:
u = User.objects.get(id=int(delete_id))
u.delete()
return redirect('/wg/?page=' + page)
paginator = Paginator(users_list, 5)
users = paginator.page(int(page))
return render(request, 'classificationManagement.html', locals())
def post(self, request):
search = request.POST.get('search')
time = request.POST.get('time')
obj = request.FILES.get('file')
type = request.POST.get('type')
if type == 'upload':
if 'xlsx' != obj.name.split('.')[1] and 'xls' != obj.name.split('.')[1]:
error = '文件类型错误'
return render(request, 'classificationManagement.html', locals())
else:
f = open(os.path.join('media/3.xls'), 'wb')
for chunk in obj.chunks():
f.write(chunk)
f.close()
data, lens = read_excel('media/3.xls')
for i in data:
try:
m = Manage(user=User.objects.get(number=i[0]), total_hours=i[5], time=i[4], types='wg')
m.save()
except Exception as e:
print(e)
pass
os.remove('media/3.xls')
return redirect('/wg/')