# coding:utf-8
import json
from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from django.contrib.auth.models import Permission
from django.contrib.auth.models import User as authUser
from django.contrib import auth
from django.core.paginator import Paginator
from main.models import Book,BookType,BookNum,AddItem,BorrowItem,UserInfo,StaffInfo
# Create your views here.
def loginView(request):
if request.user.is_authenticated():
auth.logout(request)
return render(request,"login.html",{'msgFail':'已有用户登陆中,自动登出请重新登陆'})
else:
return render(request,"login.html")
def logoutView(request):
auth.logout(request)
return render(request,"logout.html")
def registView(request):
return render(request,"regist.html")
def main(request):
if request.user.is_authenticated():
currentUser = request.user
if currentUser.is_staff:
staffInfo = StaffInfo.objects.get(staffId=currentUser)
typeInfo = []
for typeItem in BookType.objects.all():
typeInfo.append({"typeNum":typeItem.TypeNum,"typeInfo":typeItem.TypeName})
currentUserInfo = {"userId":currentUser.username,
"userName":staffInfo.staffName}
return render(request,'indexStaff.html',{"currentUser":currentUserInfo,"is_staff":True,"typeList":typeInfo})#To staffMain
else:
return HttpResponseRedirect("/staffViewUserDetail")
else:
return HttpResponseRedirect("/loginView")
def logout(request):
if request.user.is_authenticated():
auth.logout(request)
return HttpResponseRedirect("/login")
else:
return HttpResponseRedirect("/login")
def regist(request):
try:
userid = request.POST['id']
userpw = request.POST['pw']
useremail = request.POST['email']
username = request.POST['name']
usersex = request.POST['sex']
userage = request.POST['age']
userpn = request.POST['phonenum']
except:
HttpResponseRedirect("/registView")
try:
user = authUser.objects.create_user(username = userid,
password = userpw,
email = useremail)
user.is_staff = False
user.save()
try:
UserInfo.objects.create(userId = user,
userName = username,
userSex = usersex,
userAge = userage,
userPhoneNum = userpn)
except:
return HttpResponse("regist fail")
return HttpResponseRedirect("/loginView")
except:
return HttpResponseRedirect("/registView")
def login(request):
try:
userId = request.POST['id']
userPw = request.POST['pw']
except:
return HttpResponseRedirect("/login")
user = auth.authenticate(username=userId,password=userPw)
if user is not None and user.is_active:
auth.login(request,user)
return HttpResponseRedirect("/main")
else:
return HttpResponseRedirect("/login")
def userBorrowedBook(request):#得到当前用户已借阅图书数组
if request.user.is_authenticated():
currentUser = request.user
if currentUser.is_staff:
try:
userid = request.GET['userid']
userInfoFK = UserInfo.objects.get(userId = authUser.objects.get(username=userid))
borrowItems = BorrowItem.objects.filter(userId = userInfoFK)
ansBookList = []
for item in borrowItems:
if not item.hasReturned:
ansBookList.append(item.bookId)
ans = []
for bookItem in ansBookList:
currentBook = {"name":bookItem.bookName,
"type":bookItem.bookType.TypeName,
"author":bookItem.bookAuthor,
"price":bookItem.bookPrice}
ans.append(currentBook)
return HttpResponse(json.dumps({"end":True,"msg":ans}),content_type="application/json")
except:
return HttpResponse(json.dumps({"end":False}),content_type="application/json")
else:
try:
userInfoFK = UserInfo.objects.get(userId=currentUser)
borrowItems = BorrowItem.objects.filter(userId = userInfoFK)
ansBookList = []
for item in borrowItems:
if not item.hasReturned:
ansBookList.append(item.bookId)
ans = []
for bookItem in ansBookList:
currentBook = {"name":bookItem.bookName,
"type":bookItem.bookType.TypeName,
"author":bookItem.bookAuthor,
"price":bookItem.bookPrice}
ans.append(currentBook)
return HttpResponse(json.dumps({"end":True,"msg":ans}),content_type="application/json")
except:
return HttpResponse(json.dumps({"end":False}),content_type="application/json")
return HttpResponse(json.dumps({"end":False}),content_type="application/json")
else:
return HttpResponse(json.dumps({"end":False}),content_type="application/json")
def viewBook(request):
if request.user.is_authenticated():
currentUser = request.user
if currentUser.is_staff:
staffOrNot = True
userFK = StaffInfo.objects.get(staffId=currentUser).staffName
else:
staffOrNot = False
userFK = UserInfo.objects.get(userId=currentUser).userName
currentUserTrans = {"userId":currentUser.username,"userName":userFK}
try:
mode = request.GET['mode']
startPage = request.GET['startPage']
offset = request.GET['offset']
bookLikeName = request.GET['bookLikeName']
except:
return HttpResponse("get error")
ans = []
if mode == 'Detail':
BookAns = Book.objects.get(bookId=bookLikeName)#得到书的详细信息
BookNumFK = BookNum.objects.get(bookId=BookAns)
BookDetail = {"bookId":BookAns.bookId,
"bookName":BookAns.bookName,
"bookType":BookAns.bookType.TypeName,
"bookPublisher":BookAns.bookPublisher,
"bookAuthor":BookAns.bookAuthor,
"bookIntroduction":BookAns.bookIntroduction,
"bookPrice":BookAns.bookPrice,
"bookNum":BookNumFK.bookNum}
return HttpResponse(json.dumps(BookDetail),content_type="application/json")
elif mode == "simple":
currentPage = int(startPage)
if bookLikeName == '':#一连串书的信息
BookAll = Book.objects.all().order_by("bookName")
else:
BookAll = Book.objects.filter(bookName__contains=bookLikeName).order_by("bookName")
bookPagi = Paginator(BookAll,int(offset))
pageN = bookPagi.page(currentPage)
pageNum = bookPagi.page_range[-1]
after_range_num = 5
befor_range_num = 4
bookAnsList = pageN.object_list
if currentPage-1>after_range_num:
pageList = bookPagi.page_range[currentPage-1-after_range_num:currentPage-1+befor_range_num]
else:
pageList = bookPagi.page_range[0:currentPage-1+befor_range_num]
for BookAns in bookAnsList:
BookNumFK = BookNum.objects.get(bookId=BookAns)