def get_ranklist(request):
	"""
	get rank list
	:param request:
	:return:
	"""
	ranklistVolume = eval(request.GET.get('volume'))
	ranklist = User.get_ranklist(ranklistVolume=ranklistVolume)
	ranklistVolumes = User.get_ranklist_volumes()
	submissions = []
	if 'checkSignin' in request.session and request.session['checkSignin']:
		email = request.session['email']
		user = User.objects.all().filter(email=email)[0]
		request.session['score'] = str(user.score)
		for item in User.get_my_submissions(email=email, volume=1):
			item['score'] = str("%.2f" % item['score'])
			submissions.append(item)
	return render(request, 'index.html', {'ranklist': ranklist,
	                                      'ranklistVolume': ranklistVolume,
	                                      'ranklistVolumes': ranklistVolumes,
	                                      'problemTabClass': '',
	                                      'statusTabClass': '',
	                                      'rankTabClass': 'active',
	                                      'problemContentClass': 'hidden',
	                                      'statusContentClass': 'hidden',
	                                      'rankContentClass': '',
	                                      'submissions': submissions, })
def user(request):
	"""
	user signup, signin, checkSignin/
	:param request:
	:return:
	"""
	if request.method == 'POST':
		if request.GET.get('action') == 'signin':
			email = request.POST.get('email')
			password = request.POST.get('password')
			exist = User.objects.filter(email=email).exists()
			nextURL = request.GET.get('next')
			if exist:
				user = User.objects.all().filter(email=email)[0]
				if check_password(password, user.password):
					request.session['checkSignin'] = True
					request.session['email'] = user.email
					request.session['nickname'] = user.nickname
					request.session['score'] = str(user.score)
					submissions = []
					for item in User.get_my_submissions(email=email, volume=1):
						item['score'] = str("%.2f" % item['score'])
						submissions.append(item)
					request.session['submissions'] = json.dumps(submissions)
					return redirect(nextURL)
				else:
					# The password is wrong.
					return redirect(nextURL)
			else:
				# The account does not exist.
				return redirect(nextURL)
		elif request.GET.get('action') == 'signup':
			email, password, nickname \
				= request.POST.get('email'), request.POST.get('password'), request.POST.get('nickname')
			try:
				user = User(email=email,
				            password=make_password(password=password, salt=None),
				            nickname=nickname)
				user.save()
				next = request.GET.get('next')
				return redirect(next)
			except ValueError:
				print("Invalid parameters => (%s, %s, %s) while saving a user!" % (email, password, nickname))
				logger.error("Invalid parameters => (%s, %s, %s) while saving a user!" % (email, password, nickname))
		else:
			return render(request, 'register.html')
	elif request.method == 'GET':
		if request.GET.get('action') == 'logout':
			nextURL = request.GET.get('next')
			request.session['checkSignin'] = False
			request.session['email'] = ''
			request.session['nickname'] = ''
			request.session['score'] = ''
			return redirect(nextURL)
def get_problem_by_theme(request):
	"""
	get a collection of questions having the common tag or theme
	:param request:
	:return:
	"""
	from Model.themes import themes
	theme = request.GET.get('theme')
	problemVolume = eval(request.GET.get('volume'))
	problemList = Problem.get_problem_list_by_theme(theme=theme, problemVolume=problemVolume)
	problemVolumes = Problem.get_problem_volumes_by_theme(theme=theme)
	submissions = []
	if 'checkSignin' in request.session and request.session['checkSignin']:
		email = request.session['email']
		user = User.objects.all().filter(email=email)[0]
		request.session['score'] = str(user.score)
		for item in User.get_my_submissions(email=email, volume=1):
			item['score'] = str("%.2f" % item['score'])
			submissions.append(item)
	return render(request, 'theme.html', {
		'problemList': problemList,
		'themeId': theme,
		'theme': themes[theme],
		'problemVolume': problemVolume,
		'problemVolumes': problemVolumes,
		'problemTabClass': 'active',
		'statusTabClass': '',
		'rankTabClass': '',
		'problemContentClass': '',
		'statusContentClass': 'hidden',
		'rankContentClass': 'hidden',
		'submissions': submissions, })
def get_problem_by_keyword(request):
	keyword = request.GET.get('keyword')
	problemVolume = eval(request.GET.get('volume'))
	problemList = Problem.get_problem_list_by_keyword(keyword=keyword, problemVolume=problemVolume)
	problemVolumes = Problem.get_problem_volumes_by_keyword(keyword=keyword)
	submissions = []
	if 'checkSignin' in request.session and request.session['checkSignin']:
		email = request.session['email']
		user = User.objects.all().filter(email=email)[0]
		request.session['score'] = str(user.score)
		for item in User.get_my_submissions(email=email, volume=1):
			item['score'] = str("%.2f" % item['score'])
			submissions.append(item)
	return render(request, 'keyword.html', {
		'problemList': problemList,
		'keyword': keyword,
		'problemVolume': problemVolume,
		'problemVolumes': problemVolumes,
		'problemTabClass': 'active',
		'statusTabClass': '',
		'rankTabClass': '',
		'problemContentClass': '',
		'statusContentClass': 'hidden',
		'rankContentClass': 'hidden',
		'submissions': submissions, })
def index(request):
	"""
	firstly loads problem list and render the problem archive panel
	:param request:
	:return:
	"""
	problemList = Problem.get_problem_list(volume=1)
	problemVolumes = Problem.get_problem_volumes()
	submissions = []
	if 'checkSignin' in request.session and request.session['checkSignin']:
		email = request.session['email']
		user = User.objects.all().filter(email=email)[0]
		request.session['score'] = str(user.score)
		for item in User.get_my_submissions(email=email, volume=1):
			item['score'] = str("%.2f" % item['score'])
			submissions.append(item)
	return render(request, 'index.html', {
		'problemList': problemList,
		'problemVolume': 1,
		'problemVolumes': problemVolumes,
		'problemTabClass': 'active',
		'statusTabClass': '',
		'rankTabClass': '',
		'problemContentClass': '',
		'statusContentClass': 'hidden',
		'rankContentClass': 'hidden',
		'submissions': submissions, })
def get_problem_by_id(request):
	"""
	get specified problem targeted by problem id
	:param request:
	:return:
	"""
	try:
		id = eval(request.GET.get('id'))
	except Exception as e:
		print(e, "\nIncorrect Way")
		return redirect('/getProblemList?volume=1')
	problem = Problem.get_problem_by_id(id=id)
	problemFile = read_out('data/problem/' + str(id) + '.cpp')
	sampleIn = read_out('data/test_cases/' + str(id) + '/1.in')
	# sampleIn = sampleIn.replace('\n', '<br/>')
	sampleOut = read_out('data/test_cases/' + str(id) + '/1.out')
	# sampleOut = sampleOut.replace('\n', '<br/>')
	submissions = []
	if 'checkSignin' in request.session and request.session['checkSignin']:
		email = request.session['email']
		user = User.objects.all().filter(email=email)[0]
		request.session['score'] = str(user.score)
		for item in User.get_my_submissions(email=email, volume=1):
			item['score'] = str("%.2f" % item['score'])
			submissions.append(item)
	return render(request, 'problem.html', {'problem': problem,
	                                        'problemFile': problemFile,
	                                        'sampleIn': sampleIn,
	                                        'sampleOut': sampleOut,
	                                        'problemTabClass': 'active',
	                                        'statusTabClass': '',
	                                        'rankTabClass': '',
	                                        'problemContentClass': '',
	                                        'statusContentClass': 'hidden',
	                                        'rankContentClass': 'hidden',
	                                        'submissions': submissions, })