Beispiel #1
0
	def get(self, request, *args, **kwargs):
		context = {}
		context['title'] = "Home"
		context['scoreForm'] = self.InputScore() 
		context['homeTeamForm'] = self.HomeTeam(prefix="h") #preifx is used to distinguish to team objects. h for Home. a for Away.
		context['awayTeamForm'] = self.AwayTeam(prefix="a")
		scores = Score.objects.all()
		context['scores'] = scores
		ranks = getRanks()
		context['ranks'] = ranks;
		return self.render_to_response(context)
Beispiel #2
0
	def post(self, request, *args, **kwargs):
		msg = '' # happy message
		error = '' # error message
		save = 0 # do we save score object?
		scoreForm = self.InputScore(request.POST) #getting form data from the post request
		homeTeamForm = self.HomeTeam(request.POST, prefix="h")
		awayTeamForm = self.AwayTeam(request.POST, prefix="a")
		if scoreForm.is_valid() and homeTeamForm.is_valid() and awayTeamForm.is_valid(): # validating data
			save = 1 # data is ok - so probably we can save it
			newScore = scoreForm.save(commit=False) # creating new score object from form data
			newScore.when = date.today() # setting it's date for today
			homeTeamPlayers = homeTeamForm.cleaned_data['players'] # getting lists of home
			awayTeamPlayers = awayTeamForm.cleaned_data['players'] # and away players
			for pl in homeTeamPlayers:
				if pl in awayTeamPlayers:
					error = pl.name + ' is on both teams!' # checking, if someone doesn't try to play for both teams at the same time.
					save = 0 # disabling save in that case.
			if len(homeTeamPlayers) > 3 or len(awayTeamPlayers) > 3 or len(homeTeamPlayers)+len(awayTeamPlayers) == 5:
				save = 0 # checking the possible match formats. 
				error = 'Possible matches: 1 vs. 1, 2 vs. 2, 3 vs. 3, 1 vs. 2';
			elif save <> 0: # if save is allowed
				homeTeam = self.getTeam(homeTeamPlayers, homeTeamForm, self.getHomePoints(newScore)) # getting teams 
				awayTeam = self.getTeam(awayTeamPlayers, awayTeamForm, self.getAwayPoints(newScore)) # and updating them at the same time.
				if homeTeam == '' or awayTeam == '': #when no team comes, 
					msg = 'Team with the same name exists' #team with same name exists.
				else:
					newScore.home = homeTeam #referencing teams from score object.
					newScore.away = awayTeam
					newScore.save() #and finally saving new score.
					msg = 'Saved'
		else:
			error = 'Wrong values' # error message, when input form has errors.
		scores = Score.objects.all() #getting all scores to display them on the page.
		ranks = getRanks(); #same for ranks, but since ranks are not stored, they are generated from team objects to get the most accurate data.
		self.updateChart(ranks) #updating chart
		
		return render_to_response('index.html', {
			'scoreForm': scoreForm, # form to display score input
			'homeTeamForm' : homeTeamForm, # players selection and team name
			'awayTeamForm' : awayTeamForm,
			'msg' : msg, # info message
			'error' : error, # error message
			'scores' : scores, # score history
			'ranks' : ranks, # ranks sorted by points
			'title' : 'Home' # title of the page
		}, context_instance=RequestContext(request))