123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- from django.http import HttpResponse, HttpResponseRedirect
- from re import split
- from django.shortcuts import render
- from .models import Item, CheckIn, UserContent
- from .forms import CaptchaForm
- from .functions import *
- import datetime
- from datetime import date
- import re
- def userURL(request, url):
- if url == 'checkIn':
- # 获取签到的所有项目
- items = Item
- checkIn = CheckIn
- if not checkGET(request, 'ID'):
- return render(request, 'checkIn/directPage.html', {'alertMsg': '没有输入ID。', 'dirLink': '/'})
- if request.method == 'POST':
- checkIn.ID = request.GET.get('ID')
- checkIn = CheckIn.searchByID(checkIn)
- if checkIn.captcha: # 是否需要验证码确认
- myCaptcha = CaptchaForm(request.POST)
- if not myCaptcha.is_valid():
- return render(request, 'checkIn/user/error.html', {'title': '参数错误', 'content': '验证码错误。'})
- items.c_ID = request.session.get('Uc_ID')
- items = Item.searchByC_ID(items)
- values = {}
- # 通过遍历所有item从POST里面取值
- for x in items:
- # 从item获取签到key
- key = x['description']
- # 从POST中取值
- value = request.POST[key].strip()
- # 如果item要求唯一性,去user content里面搜索是否有同样的数据
- if x['unique']:
- itemUnique = UserContent
- itemUnique.content = value
- if not UserContent.uniqueCheck(itemUnique):
- return render(request, 'checkIn/user/error.html',
- {'title': '请不要重复签到', 'content': '你已经签到了。%s重复了。' %(x['description'])})
- else:
- del itemUnique
- # 如果要求数据不在预设范围之内的话
- # TODO 这边需要重新写一个reservedcontent 用re.split()
- if x['reservedFlag']:
- if not value in re.split('\r\n|\n|\r|;|;', x['reservedContent']):
- return render(request, 'checkIn/user/error.html', {'title': '你不在活动名单中', 'content': '你不在活动名单中。'})
- # 如果通过验证就把数据Dict保留在Session里面
- values[key] = value
- request.session['UcheckInContent'] = values
- if checkIn.creatorConfirm:
- request.session['Uconfirmation'] = 10 # Session里面先保存一个需要确认,但是未确认的状态
- return HttpResponseRedirect('/checkIn/user/confirm')
- else:
- return HttpResponseRedirect('/checkIn/user/success')
- # GET开启,不是POST
- else:
- # reset签到session
- if checkSession(request, 'Uc_ID'):
- del request.session['Uc_ID']
- if checkSession(request, 'UcheckInContent'):
- del request.session['UcheckInContent']
- if checkSession(request, 'Uconfirmation'):
- del request.session['Uconfirmation']
- # 验证是否GET Uc_ID
- try:
- # 获取签到内容列表
- items.c_ID = request.GET['ID']
- checkIn.ID = request.GET['ID']
- request.session['Uc_ID'] = items.c_ID
- items = Item.searchByC_ID(items)
- checkIn = CheckIn.searchByID(checkIn)
- except Exception as e:
- return render(request, 'checkIn/user/error.html', {'title': '参数错误', 'content': e})
- # 验证签到是否可以被执行。
- # 验证签到内容是否为空。
- if len(items) == 0:
- return render(request, 'checkIn/user/error.html', {'title': '404', 'content': '这个活动内容还没有创建。'})
- # 如果时间未到,就提示签到尚未开始
- currentDate = datetime.date.today()
- timeDelta = currentDate - checkIn.startTime
- if timeDelta.days < 0:
- return render(request, 'checkIn/user/error.html', {'title': '签到活动还没有开始', 'content': '活动还没有开始。'})
- # 如果时间超过了,就提示签到尚未开始
- if isinstance(checkIn.endTime, date):
- timeDelta = currentDate - checkIn.endTime
- if timeDelta.days > 0:
- return render(request, 'checkIn/user/error.html', {'title': '签到活动已经结束了', 'content': '活动已经结束了。'})
- # 如果签到人数达到上限就停止签到
- userContent = UserContent
- userContent.c_ID = checkIn.ID
- checkInQty = UserContent.getContentQty(userContent)
- if checkInQty and isinstance(checkIn.maxNum, int):
- if checkInQty >= checkIn.maxNum:
- return render(request, 'checkIn/user/error.html', {'title': '活动已经到达人数上限了',
- 'content': '活动已经到达人数上限了。'})
- # 如果签到没有激活
- if checkIn.status != 'active':
- return render(request, 'checkIn/user/error.html', {'title': '活动没有开启', 'content': '活动没有开启。'})
- # end of 签到验证
- # 是否需要capthca
- if checkIn.captcha:
- myCaptcha = CaptchaForm
- else:
- myCaptcha = None
- # 遍历item里面的选项
- x = 0
- for i in items:
- if i['type'] == 4:
- option = re.split('\r\n|\n|\r', i['itemSelection'])
- items[x]['option'] = option
- #selection[i['ID']] = option
- x += 1
- # 渲染签到页面
- try:
- return render(request, 'checkIn/user/checkIn.html', {'checkIn':checkIn, 'items': items, 'captcha': myCaptcha})
- except Exception as e:
- return render(request, 'checkIn/user/error.html', {'title': '参数错误', 'content': e})
-
- if url == 'success':
- if not checkSession(request, 'UcheckInContent'):
- return render(request, 'checkIn/directPage.html', {'alertMsg': 'Session错误。', 'dirLink': '/'})
- values = getSession(request, 'UcheckInContent')
- if not checkSession(request, 'Uc_ID'):
- return render(request, 'checkIn/directPage.html', {'alertMsg': 'Session错误。', 'dirLink': '/'})
- checkIn = CheckIn
- checkIn.ID = getSession(request, 'Uc_ID')
- checkIn = CheckIn.searchByID(checkIn)
- # 将数据保存在SQL数据库里面
- userContent = UserContent
- userContent.c_ID = checkIn.ID
- userContent.o_ID = checkIn.o_ID
- userContent.content = values
- # 如果用户签到需要组织者确认的。
- try:
- userContent.confirmation = request.session['Uconfirmation']
- except:
- userContent.confirmation = 0
- # 尝试获取签到者的IP地址
- try:
- userContent.IPAddress = request.META['HTTP_X_FORWARDED_FOR']
- except:
- userContent.IPAddress = request.META['REMOTE_ADDR']
- # 在SQL server里面保存数据
- UserContent.createNew(userContent)
- # 将数据保存在mongoDB里面
- # mongoAddRecord(checkIn.ID, values)
- # 完成checkIn活动,清空Session
- del request.session['Uc_ID']
- del request.session['UcheckInContent']
- try:
- del request.session['Uconfirmation']
- except Exception as e:
- print(e)
- return render(request, 'checkIn/user/success.html', {'checkIn': checkIn})
- # 如果需要确认签到
- if url == 'confirm':
- checkIn = CheckIn
- if request.method == 'POST':
- if not checkSession(request, 'Uconfirmation'):
- return render(request, 'checkIn/directPage.html', {'alertMsg': 'Session错误。', 'dirLink': '/'})
- request.session['Uconfirmation'] = request.POST['confirmValue']
- return HttpResponseRedirect('/checkIn/user/success')
- else: # GET打开页面
- if not checkSession(request, 'Uconfirmation'):
- return render(request, 'checkIn/directPage.html', {'alertMsg': '你还没有输入信息!', 'dirLink': '/'})
- else:
- return render(request, 'checkIn/user/confirm.html')
|