views.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from django.shortcuts import render
  2. from django.http import HttpResponse, HttpResponseRedirect
  3. from .models import Book, User
  4. from .forms import NewBookForm, BookManageForm
  5. # Create your views here.
  6. def index(request):
  7. book = Book
  8. filter_rule = ''
  9. try:
  10. filter_rule = 'category=' + request.GET['category']
  11. except:
  12. None
  13. try:
  14. filter_rule = filter_rule + ','+ 'status=' + request.GET['status']
  15. except:
  16. None
  17. if filter_rule == '':
  18. book_list = Book.getList(book)
  19. else:
  20. book_list = Book.search_filter(book, filter_rule)
  21. return render(request, 'library/index.html', {'book_list': book_list})
  22. def newbook(request):
  23. if request.method == 'POST':
  24. newBookForm = NewBookForm(request.POST)
  25. if newBookForm.is_valid():
  26. book = Book
  27. book.name = newBookForm.cleaned_data['name'].strip()
  28. book.category = newBookForm.cleaned_data['category'].strip()
  29. book.description = newBookForm.cleaned_data['description'].strip()
  30. book.create_new(book)
  31. return render(request, 'library/directPage.html', {'alertMsg': '新书添加成功。', 'dirLink': '/cq/library'})
  32. else:
  33. return render(request, 'library/newbook.html', {'new_book_form':NewBookForm})
  34. else:
  35. return render(request, 'library/newbook.html', {'new_book_form': NewBookForm})
  36. def newuser(request):
  37. return render(request, 'library/newuser.html')
  38. def bookmanage(request):
  39. if request.method == 'POST':
  40. bookManageForm = BookManageForm(request.POST)
  41. if bookManageForm.is_valid():
  42. book = Book
  43. book.ID = bookManageForm.cleaned_data['ID']
  44. book.name = bookManageForm.cleaned_data['name'].strip()
  45. book.category = bookManageForm.cleaned_data['category'].strip()
  46. book.description = bookManageForm.cleaned_data['description'].strip()
  47. book.writer = bookManageForm.cleaned_data['writer'].strip()
  48. book.serial_number = 'YYSCDJWXYS' + str(bookManageForm.cleaned_data['serial_number']).zfill(4)
  49. if bookManageForm.cleaned_data['borrower'].strip() == '':
  50. book.status = '在库'
  51. book.borrower = ''
  52. book.borrower_phone = None
  53. else:
  54. book.status = '外借'
  55. book.borrower = bookManageForm.cleaned_data['borrower'].strip()
  56. book.borrower_phone = bookManageForm.cleaned_data['borrower_phone']
  57. import datetime
  58. book.borrow_time = datetime.date.today()
  59. book.update(book)
  60. return render(request, 'library/directPage.html', {'alertMsg': '书籍管理成功。', 'dirLink': '/cq/library'})
  61. else:
  62. return render(request, 'library/bookmanage.html', {'book_manage_form': BookManageForm})
  63. else:
  64. book = Book
  65. book.ID = request.GET['ID']
  66. book = Book.search_by_ID(book)
  67. bookManageForm = BookManageForm(initial={'ID':book[0].ID, 'name':book[0].name, 'description':book[0].description, 'category':book[0].category,
  68. 'borrower':book[0].borrower, 'borrower_phone':book[0].borrower_phone, 'writer':book[0].writer, 'serial_number':int(book[0].serial_number[-4:])})
  69. return render(request, 'library/bookmanage.html', {'book_manage_form': bookManageForm})
  70. def intro(request):
  71. return render(request, 'library/intro.html')