Ejemplo n.º 1
0
def participant_specific(request, participant):
	helper = RelayFunctions()

	info = helper.participant_specific_info(participant)
	totals = helper.participant_specific_totals(participant)
	donations = helper.participant_specific_donations(participant)
	milestones = helper.participant_specific_milestones(participant)
	events = helper.participant_specific_events(participant)
	emails = helper.participant_specific_emails(participant)


	data = { 'info' : info , 'totals' : totals , 'donations' : donations , 'milestones' : milestones, 'events':events, 'emails':emails }

	response = json.dumps(data)
	return HttpResponse(response, mimetype="application/json")
def createFundraisingChallengeStartRecord():
	helper = RelayFunctions()
	
	for participant in Participant.objects.all():
		totals = helper.participant_specific_totals(model_to_dict(participant)['id'])
		total_donations = totals['donations_total']
		total_candles = totals['milestone_total'] + totals['emails_candles'] + totals['event_candles']
		try:
			for challenge in Fundraising_Challenge.objects.all():
				try:
					fundraising_challenge_start_record = Fundraising_Challenge_Start_Record(participant = participant, challenge = challenge)
				except Fundraising_Challenge_Start_Record.DoesNotExist:
					print('Record DNE.  Creating start record...')
					new_fundraising_challenge_start_record = Fundraising_Challenge_Start_Record(participant = participant, challenge = challenge, amount_raised = total_donations, candles_raised = total_candles, datetime_start = challenge.datetime_start)
					new_fundraising_challenge_start_record.save()
		except Exception:
			print('No challenges')

	createFundraisingChallengeRecord()
def createFundraisingChallengeRecord():
	helper = RelayFunctions()
	timezone.now()
	utc=pytz.UTC
	
	
	
	for challenge in Fundraising_Challenge.objects.all():
		if challenge.datetime_start <= datetime.now(utc) <= challenge.datetime_end:
			if not challenge.amount_raised == 0 and challenge.candles_raised == 0:
				for participant in Participant.objects.all():
					save_participant = Participant.objects.get(pk = participant)
					currentRaised = Donation.objects.filter(participant = participant).aggregate(total_donations = Sum('amount'))
					startRecord = Fundraising_Challenge_Start_Record.objects.get(participant = participant, challenge = challenge)
					startRaised = startRecord.amount_raised
					diffRaised = currentRaised - startRaised
					print('Checking to see if record is deserved')
					if diffRaised > challenge.amount_raised:
						try:
							record = Fundraising_Challenge_Record.objects.get(participant = participant, challenge = challenge)
							print('Already created...editing')
							Fundraising_Challenge_Record.objects.get(participant = participant, challenge = challenge).delete()
							donations = Donation.objects.filter(participant = participant).order_by('datetime')
							tot = 0
							stop = 0
							for donation in donations:
								if stop == 0:
									if tot < challenge.amount_raised:
										amoun = donation.amount
										tot = tot + amoun
									else:
										donated_datetime = donation.datetime
										stop = 1
							#donated_datetime = Donation.objects.filter(participant = participant).annotate(most_recent_donation_datetime = Max('datetime'))
							new_fundraising_record = Fundraising_Challenge_Record(participant = save_participant, challenge = challenge, datetime = donated_datetime, candles_rewarded = challenge.candles_rewarded)
							new_fundraising_record.save()
						except Fundraising_Challenge_Record.DoesNotExist:
							print('Deserved...Fundraising_Challenge_Record does not exist...creating one...')
							donations = Donation.objects.filter(participant = participant).order_by('datetime')
							tot = 0
							stop = 0
							for donation in donations:
								if stop == 0:
									if tot < challenge.amount_raised:
										amoun = donation.amount
										tot = tot + amoun
									else:
										donated_datetime = donation.datetime
										stop = 1
							#donated_datetime = Donation.objects.filter(participant = participant).annotate(most_recent_donation_datetime = Max('datetime'))
							new_fundraising_record = Fundraising_Challenge_Record(participant = save_participant, challenge = challenge, datetime = donated_datetime, candles_rewarded = challenge.candles_rewarded)
							new_fundraising_record.save()
					else:
						print('It is not deserved')
			if not challenge.candles_raised == 0 and challenge.amount_raised == 0:
				for participant in Participant.objects.all():
					totals = helper.participant_specific_totals(model_to_dict(participant)['id'])
					currentCandles = totals['milestone_total'] + totals['emails_candles'] + totals['event_candles']
					startRecord = Fundraising_Challenge_Start_Record.objects.get(participant = participant, challenge = challenge)
					startCandles = startRecord.candles_raised
					diffCandles = currentCandles - startCandles
					print('Checking to see if record is deserved')
					if diffCandles > challenge.candles_raised:
						try:
							record = Fundraising_Challenge_Record.objects.filter(participant = participant, challenge = challenge)
							Fundraising_Challenge_Record.objects.get(participant = participant, challenge = challenge).delete()
							candles_record = Candles_Record.objects.filter(participant = participant).order_by('datetime')
							save_participant = Participant.objects.get(pk = participant)
							tot = 0
							stop = 0
							for candles in candles_record:
								if stop == 0:
									if tot < challenge.candles_raised:
										amoun = candles.candles_value
										tot = tot + amoun
									else:
										candles_datetime = candles.datetime
										stop = 1
							new_fundraising_record = Fundraising_Challenge_Record(participant = save_participant, challenge = challenge, datetime = candles_datetime, candles_rewarded = challenge.candles_rewarded)
							new_fundraising_record.save()
						except Fundraising_Challenge_Record.DoesNotExist:
							print('Deserved...Fundraising_Challenge_Record does not exist...creating one...')
							candles_record = Candles_Record.objects.filter(participant = participant).order_by('datetime')
							save_participant = Participant.objects.get(pk = participant)
							tot = 0
							stop = 0
							for candles in candles_record:
								if stop == 0:
									if tot < challenge.candles_raised:
										amoun = candles.candles_value
										tot = tot + amoun
									else:
										candles_datetime = candles.datetime
										stop = 1
							new_fundraising_record = Fundraising_Challenge_Record(participant = save_participant, challenge = challenge, datetime = candles_datetime, candles_rewarded = challenge.candles_rewarded)
							new_fundraising_record.save()
			else:
				print('No amount_raised or candles_raised specified.  Or both specified at the same time.')
		else:
			print('No challenge currently running')

	createFundraisingChallengeTrackerRecord()