userViews.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. from django.http import HttpResponse, HttpResponseRedirect
  2. from re import split
  3. from django.shortcuts import render
  4. from .models import Item, CheckIn, UserContent
  5. from .forms import CaptchaForm
  6. from .functions import *
  7. import datetime
  8. from datetime import date
  9. import re
  10. def userURL(request, url):
  11. if url == 'checkIn':
  12. # 获取签到的所有项目
  13. items = Item
  14. checkIn = CheckIn
  15. if not checkGET(request, 'ID'):
  16. return render(request, 'checkIn/directPage.html', {'alertMsg': '没有输入ID。', 'dirLink': '/'})
  17. if request.method == 'POST':
  18. checkIn.ID = request.GET.get('ID')
  19. checkIn = CheckIn.searchByID(checkIn)
  20. if checkIn.captcha: # 是否需要验证码确认
  21. myCaptcha = CaptchaForm(request.POST)
  22. if not myCaptcha.is_valid():
  23. return render(request, 'checkIn/user/error.html', {'title': '参数错误', 'content': '验证码错误。'})
  24. items.c_ID = request.session.get('Uc_ID')
  25. items = Item.searchByC_ID(items)
  26. values = {}
  27. # 通过遍历所有item从POST里面取值
  28. for x in items:
  29. # 从item获取签到key
  30. key = x['description']
  31. # 从POST中取值
  32. value = request.POST[key].strip()
  33. # 如果item要求唯一性,去user content里面搜索是否有同样的数据
  34. if x['unique']:
  35. itemUnique = UserContent
  36. itemUnique.content = value
  37. if not UserContent.uniqueCheck(itemUnique):
  38. return render(request, 'checkIn/user/error.html',
  39. {'title': '请不要重复签到', 'content': '你已经签到了。%s重复了。' %(x['description'])})
  40. else:
  41. del itemUnique
  42. # 如果要求数据不在预设范围之内的话
  43. # TODO 这边需要重新写一个reservedcontent 用re.split()
  44. if x['reservedFlag']:
  45. if not value in re.split('\r\n|\n|\r|;|;', x['reservedContent']):
  46. return render(request, 'checkIn/user/error.html', {'title': '你不在活动名单中', 'content': '你不在活动名单中。'})
  47. # 如果通过验证就把数据Dict保留在Session里面
  48. values[key] = value
  49. request.session['UcheckInContent'] = values
  50. if checkIn.creatorConfirm:
  51. request.session['Uconfirmation'] = 10 # Session里面先保存一个需要确认,但是未确认的状态
  52. return HttpResponseRedirect('/checkIn/user/confirm')
  53. else:
  54. return HttpResponseRedirect('/checkIn/user/success')
  55. # GET开启,不是POST
  56. else:
  57. # reset签到session
  58. if checkSession(request, 'Uc_ID'):
  59. del request.session['Uc_ID']
  60. if checkSession(request, 'UcheckInContent'):
  61. del request.session['UcheckInContent']
  62. if checkSession(request, 'Uconfirmation'):
  63. del request.session['Uconfirmation']
  64. # 验证是否GET Uc_ID
  65. try:
  66. # 获取签到内容列表
  67. items.c_ID = request.GET['ID']
  68. checkIn.ID = request.GET['ID']
  69. request.session['Uc_ID'] = items.c_ID
  70. items = Item.searchByC_ID(items)
  71. checkIn = CheckIn.searchByID(checkIn)
  72. except Exception as e:
  73. return render(request, 'checkIn/user/error.html', {'title': '参数错误', 'content': e})
  74. # 验证签到是否可以被执行。
  75. # 验证签到内容是否为空。
  76. if len(items) == 0:
  77. return render(request, 'checkIn/user/error.html', {'title': '404', 'content': '这个活动内容还没有创建。'})
  78. # 如果时间未到,就提示签到尚未开始
  79. currentDate = datetime.date.today()
  80. timeDelta = currentDate - checkIn.startTime
  81. if timeDelta.days < 0:
  82. return render(request, 'checkIn/user/error.html', {'title': '签到活动还没有开始', 'content': '活动还没有开始。'})
  83. # 如果时间超过了,就提示签到尚未开始
  84. if isinstance(checkIn.endTime, date):
  85. timeDelta = currentDate - checkIn.endTime
  86. if timeDelta.days > 0:
  87. return render(request, 'checkIn/user/error.html', {'title': '签到活动已经结束了', 'content': '活动已经结束了。'})
  88. # 如果签到人数达到上限就停止签到
  89. userContent = UserContent
  90. userContent.c_ID = checkIn.ID
  91. checkInQty = UserContent.getContentQty(userContent)
  92. if checkInQty and isinstance(checkIn.maxNum, int):
  93. if checkInQty >= checkIn.maxNum:
  94. return render(request, 'checkIn/user/error.html', {'title': '活动已经到达人数上限了',
  95. 'content': '活动已经到达人数上限了。'})
  96. # 如果签到没有激活
  97. if checkIn.status != 'active':
  98. return render(request, 'checkIn/user/error.html', {'title': '活动没有开启', 'content': '活动没有开启。'})
  99. # end of 签到验证
  100. # 是否需要capthca
  101. if checkIn.captcha:
  102. myCaptcha = CaptchaForm
  103. else:
  104. myCaptcha = None
  105. # 遍历item里面的选项
  106. x = 0
  107. for i in items:
  108. if i['type'] == 4:
  109. option = re.split('\r\n|\n|\r', i['itemSelection'])
  110. items[x]['option'] = option
  111. #selection[i['ID']] = option
  112. x += 1
  113. # 渲染签到页面
  114. try:
  115. return render(request, 'checkIn/user/checkIn.html', {'checkIn':checkIn, 'items': items, 'captcha': myCaptcha})
  116. except Exception as e:
  117. return render(request, 'checkIn/user/error.html', {'title': '参数错误', 'content': e})
  118. if url == 'success':
  119. if not checkSession(request, 'UcheckInContent'):
  120. return render(request, 'checkIn/directPage.html', {'alertMsg': 'Session错误。', 'dirLink': '/'})
  121. values = getSession(request, 'UcheckInContent')
  122. if not checkSession(request, 'Uc_ID'):
  123. return render(request, 'checkIn/directPage.html', {'alertMsg': 'Session错误。', 'dirLink': '/'})
  124. checkIn = CheckIn
  125. checkIn.ID = getSession(request, 'Uc_ID')
  126. checkIn = CheckIn.searchByID(checkIn)
  127. # 将数据保存在SQL数据库里面
  128. userContent = UserContent
  129. userContent.c_ID = checkIn.ID
  130. userContent.o_ID = checkIn.o_ID
  131. userContent.content = values
  132. # 如果用户签到需要组织者确认的。
  133. try:
  134. userContent.confirmation = request.session['Uconfirmation']
  135. except:
  136. userContent.confirmation = 0
  137. # 尝试获取签到者的IP地址
  138. try:
  139. userContent.IPAddress = request.META['HTTP_X_FORWARDED_FOR']
  140. except:
  141. userContent.IPAddress = request.META['REMOTE_ADDR']
  142. # 在SQL server里面保存数据
  143. UserContent.createNew(userContent)
  144. # 将数据保存在mongoDB里面
  145. # mongoAddRecord(checkIn.ID, values)
  146. # 完成checkIn活动,清空Session
  147. del request.session['Uc_ID']
  148. del request.session['UcheckInContent']
  149. try:
  150. del request.session['Uconfirmation']
  151. except Exception as e:
  152. print(e)
  153. return render(request, 'checkIn/user/success.html', {'checkIn': checkIn})
  154. # 如果需要确认签到
  155. if url == 'confirm':
  156. checkIn = CheckIn
  157. if request.method == 'POST':
  158. if not checkSession(request, 'Uconfirmation'):
  159. return render(request, 'checkIn/directPage.html', {'alertMsg': 'Session错误。', 'dirLink': '/'})
  160. request.session['Uconfirmation'] = request.POST['confirmValue']
  161. return HttpResponseRedirect('/checkIn/user/success')
  162. else: # GET打开页面
  163. if not checkSession(request, 'Uconfirmation'):
  164. return render(request, 'checkIn/directPage.html', {'alertMsg': '你还没有输入信息!', 'dirLink': '/'})
  165. else:
  166. return render(request, 'checkIn/user/confirm.html')