from django.contrib.auth import authenticate,logout
from django.contrib.auth.views import login
from django.shortcuts import render,redirect
from django.urls import reverse
from django.views.generic.base import View
from django.contrib.auth.hashers import make_password
from .models import UserProfile,UserInfo,schooleModel,bulidingModel,roomModel
from django.http import JsonResponse
# Create your views here.
def Error(code,massage):
data = {}
data['status'] = 'ERROR'
data['code'] = code
data['massage'] = massage
return JsonResponse(data)
#Ajax返回成功信息
def Success():
data = {}
data['status'] = 'SUCCESS'
data['code'] = 200
return JsonResponse(data)
# 加载首页
def homeView(request):
# 登录判断
if request.user.is_authenticated:
return render(request,"index.html",{
"step":request.user.step
})
return render(request, "login.html")
# 登录
def loginView(request):
if request.method == 'POST':
username = request.POST.get("username")
password = request.POST.get("password")
user = authenticate(username=username, password=password)
print(user)
if user is not None:
if user.is_active:
login(request, user)
return redirect(request.POST.get('next', reverse('home')))
else:
return render(request, 'login.html', {"msg": "用户未激活"})
else:
return render(request, 'login.html', {"msg": "密码或用户错误"})
return render(request, "login.html")
else:
if request.user.is_authenticated:
return redirect('home')
return render(request, "login.html")
#注销
class LoginOut(View):
def get(self,request):
logout(request)
return redirect('login')
# 重设密码
def setPwdView(request):
if request.method == 'POST':
oldPwd = request.POST.get("oldPwd")
newPwd = request.POST.get("newPwd")
newPwd1 = request.POST.get("newPwd1")
# 判断旧密码是否正确
userAuth = authenticate(username=request.user.username, password=oldPwd)
# 正确
if userAuth is not None:
if newPwd == newPwd1:
userAuth.password = make_password(newPwd)
userAuth.save()
logout(request)
return render(request, 'login.html', {"msg": "重置成功"})
else:
return render(request, 'setPwd.html', {"msg": "新密码不一致"})
#错误
else:
return render(request, 'setPwd.html', {"msg": "旧密码错误"})
else:
return render(request, "setPwd.html")
# 编辑用户信息
def setUserInfo(request):
if request.method == 'POST':
habit = request.POST.get("habit")
hobby = request.POST.get("hobby")
season = request.POST.get("season")
user = UserProfile.objects.get(id=request.user.id)
print(habit)
# 看是否存在信息
userInfoArr = UserInfo.objects.filter(user=user)
print(userInfoArr)
if len(userInfoArr)== 0: # 不存在创建数据
# 保存个人信息
userInfo = UserInfo.objects.create(
hobby = hobby,
habit = habit,
season = season,
user = user
)
userInfo.save()
# 步骤加1
user.step = user.step + 1
user.save()
return Success()
else:
userInfo = userInfoArr[0]
userInfo.habit = habit
userInfo.hobby = hobby
userInfo.season = season
userInfo.save()
return Success()
else:
# 看是否存在信息
user = UserProfile.objects.get(id=request.user.id)
userInfoArr = UserInfo.objects.filter(user=user)
if len(userInfoArr) == 0: # 不存在创建数据
return render(request, "userInfo.html")
else:
return render(request, "userInfo.html",{
"userInfo":userInfoArr[0]
})
# 步骤1
def selectView(request):
if request.method == 'POST':
pass
else:
user = UserProfile.objects.get(id=request.user.id)
userInfoArr = UserInfo.objects.filter(user=user)
return render(request,"select.html",{
"userInfo": userInfoArr[0]
})
# 步骤2
def selectScoolView(request):
if request.method == 'POST':
pass
else:
school = schooleModel.objects.all()
return render(request,"select2.html",{
"schoolAll":school
})
# 步骤3 id 是楼层的id
def select3View(request,id):
if request.method == 'POST':
pass
else:
try:
school = schooleModel.objects.get(id=id)
bulidingAll = bulidingModel.objects.filter(school=school)
return render(request, "select3.html", {
"bulidingAll": bulidingAll,
"school": school,
"number":range(1,school.number + 1)
})
except:
return redirect('home')
# 步骤4 id是宿舍的id
def selectRoom(request,bulidingId):
if request.method == 'POST':
print(bulidingId)
roomNumber = request.POST.get("roomNumber")
bulid = bulidingModel.objects.get(id=int(bulidingId))
uesrInfo = UserInfo.objects.get(user=request.user)
print(bulid,uesrInfo)
room = roomModel.objects.create(
buliding=bulid,
roomNumber=roomNumber,
userInfo=uesrInfo
)
user = UserProfile.objects.get(id=request.user.id)
# 步骤加1
user.step = user.step + 1
user.save()
return Success()
else:
try:
buliding = bulidingModel.objects.get(id=bulidingId)
roomAll = roomModel.objects.filter(buliding=buliding)
print(roomAll)
# 寻找所属用户床位信息
roomArr = []
for item in range(1, buliding.number + 1):
roomArr.append({
'number': item,
'isSelect': False
})
if len(roomAll) > 0:
for room in roomAll:
roomArr[room.roomNumber - 1]["isSelect"] = True
roomArr[room.roomNumber - 1]["userInfo"] = room.userInfo
return render(request, "select4.html", {
"buliding": buliding,
"number": range(1, buliding.number + 1),
"roomArr":roomArr
})
except:
return redirect('home')
# 步骤5 完成
def complete(request):
if request.method == 'GET':
room = roomModel.objects.get(userInfo__user=request.user)
return render(request,'select5.html',{
"room":room
})
# 打印
def dayinView(request):
if request.method == 'GET':
room = roomModel.objects.get(userInfo__user=request.user)
user = UserProfile.objects.get(id=request.user.id)
# 步骤加1
if user.step < 3:
user.step = user.step + 1
user.save()
return render(request,'dayin.html',{
"room":room
})