123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- 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.status != 'active':
- return render(request, 'checkIn/user/error.html',
- {'title': '反馈提交失败', 'content': '反馈已经关闭。'})
- if checkIn.captcha: # 是否需要验证码确认
- myCaptcha = CaptchaForm(request.POST)
- if not myCaptcha.is_valid():
- 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': '活动已经到达人数上限了。'})
- 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()
- value = request.POST.getlist(key)
- # 如果是文件上传类型中的一种
- if x['type'] in [6, 7]:
- filename = request.FILES.get(key)
- new_file_name = hash(str(filename)+str(datetime.datetime.now()))
- upload_file(request, filename=key, prefix=new_file_name)
- filename = str(new_file_name)+'.'+get_ext(filename)
- value = [filename] # 还原value为list
- # 如果item要求唯一性,去user content里面搜索是否有同样的数据
- if x['unique']:
- itemUnique = UserContent
- itemUnique.content = "'{}': '{}'".format(key, value[0])
- # itemUnique.content = value[0]
- itemUnique.c_ID = x['c_ID']
- 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 value[0] not in re.split('\r\n|\n|\r|;|;', x['reservedContent']):
- return render(request, 'checkIn/user/error.html',
- {'title': '输入信息不在预设范围内', 'content': '输入信息不在预设范围内。'})
- # 如果通过验证就把数据Dict保留在Session里面
- values[key] = ','.join(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
- if request.method == 'GET':
- # 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'] in [4, 5]: # 如果是列表需要将文字转换为选项
- option = re.split('\r\n|\n|\r', i['itemSelection'])
- items[x]['option'] = option
- if i['type'] in [6]: # 如果是列表需要将文字转换为选项
- option = re.split('\r\n|\n|\r', i['itemSelection'])
- option_add_dot = []
- if len(option) > 0:
- for o in option:
- o = '.' + o
- option_add_dot.append(o)
- option_to_str = ','.join(option_add_dot)
- if option_to_str == '.': # 如果后缀名格式是空白的情况
- option_to_str = ''
- items[x]['option'] = option_to_str
- 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')
|