22from django .db .models import Count , Sum
33
44from portal .constants import (
5+ CACHE_KEY_DONATION_BREAKDOWN ,
6+ CACHE_KEY_DONATION_TOWARDS_GOAL_PERCENT ,
7+ CACHE_KEY_DONATIONS_TOTAL_AMOUNT ,
8+ CACHE_KEY_DONORS_COUNT ,
59 CACHE_KEY_SPONSORSHIP_BREAKDOWN ,
610 CACHE_KEY_SPONSORSHIP_COMMITTED ,
711 CACHE_KEY_SPONSORSHIP_COMMITTED_COUNT ,
1216 CACHE_KEY_SPONSORSHIP_PENDING_COUNT ,
1317 CACHE_KEY_SPONSORSHIP_TOWARDS_GOAL_PERCENT ,
1418 CACHE_KEY_TEAMS_COUNT ,
19+ CACHE_KEY_TOTAL_FUNDS_RAISED ,
1520 CACHE_KEY_TOTAL_SPONSORSHIPS ,
1621 CACHE_KEY_VOLUNTEER_BREAKDOWN ,
1722 CACHE_KEY_VOLUNTEER_LANGUAGES ,
1823 CACHE_KEY_VOLUNTEER_ONBOARDED_COUNT ,
1924 CACHE_KEY_VOLUNTEER_PYLADIES_CHAPTERS ,
2025 CACHE_KEY_VOLUNTEER_SIGNUPS_COUNT ,
26+ DONATION_GOAL_AMOUNT ,
27+ DONATIONS_GOAL ,
28+ SPONSORSHIP_GOAL ,
2129 SPONSORSHIP_GOAL_AMOUNT ,
2230 STATS_CACHE_TIMEOUT ,
2331)
24- from sponsorship .models import SponsorshipProfile , SponsorshipProgressStatus
32+ from sponsorship .models import (
33+ IndividualDonation ,
34+ SponsorshipProfile ,
35+ SponsorshipProgressStatus ,
36+ )
2537from volunteer .constants import ApplicationStatus
2638from volunteer .models import Team , VolunteerProfile
2739
@@ -33,6 +45,7 @@ def get_stats_cached_values():
3345 stats_dict .update (get_volunteer_stats_dict ())
3446
3547 stats_dict .update (get_sponsorships_stats_dict ())
48+ stats_dict .update (get_donations_stats_dict ())
3649 return stats_dict
3750
3851
@@ -53,6 +66,7 @@ def get_volunteer_stats_dict():
5366
5467def get_sponsorships_stats_dict ():
5568 stats_dict = {}
69+ stats_dict [SPONSORSHIP_GOAL ] = SPONSORSHIP_GOAL_AMOUNT
5670 stats_dict [CACHE_KEY_TOTAL_SPONSORSHIPS ] = get_sponsorship_total_count_stats_cache ()
5771 stats_dict [CACHE_KEY_SPONSORSHIP_PAID ] = get_sponsorship_paid_amount_stats_cache ()
5872 stats_dict [CACHE_KEY_SPONSORSHIP_PAID_PERCENT ] = (
@@ -78,7 +92,10 @@ def get_sponsorships_stats_dict():
7892 get_sponsorship_committed_count_stats_cache ()
7993 )
8094 stats_dict [CACHE_KEY_SPONSORSHIP_BREAKDOWN ] = get_sponsorship_breakdown ()
81-
95+ stats_dict [CACHE_KEY_TOTAL_FUNDS_RAISED ] = (
96+ get_total_donations_amount_cache ()
97+ + get_sponsorship_committed_amount_stats_cache ()
98+ )
8299 return stats_dict
83100
84101
@@ -498,3 +515,67 @@ def get_volunteer_breakdown():
498515 STATS_CACHE_TIMEOUT ,
499516 )
500517 return volunteer_breakdown
518+
519+
520+ def get_total_donations_amount_cache ():
521+ """Returns the donations amount"""
522+ total_donations = cache .get (CACHE_KEY_DONATIONS_TOTAL_AMOUNT )
523+ if not total_donations :
524+
525+ total_donations = (
526+ IndividualDonation .objects .aggregate (Sum ("donation_amount" ))[
527+ "donation_amount__sum"
528+ ]
529+ or 0
530+ )
531+ cache .set (
532+ CACHE_KEY_DONATIONS_TOTAL_AMOUNT ,
533+ total_donations ,
534+ STATS_CACHE_TIMEOUT ,
535+ )
536+ return total_donations
537+
538+
539+ def get_donors_count_cache ():
540+ """Returns the number of unique donors"""
541+ donors_count = cache .get (CACHE_KEY_DONORS_COUNT )
542+ if not donors_count :
543+
544+ donors_count = (
545+ IndividualDonation .objects .values ("donor_email" ).distinct ().count ()
546+ )
547+ cache .set (
548+ CACHE_KEY_DONORS_COUNT ,
549+ donors_count ,
550+ STATS_CACHE_TIMEOUT ,
551+ )
552+ return donors_count
553+
554+
555+ def get_donation_to_goal_percent_cache ():
556+ """Returns donation towards goal percent"""
557+ donation_towards_goal_percent = cache .get (CACHE_KEY_DONATION_TOWARDS_GOAL_PERCENT )
558+ if not donation_towards_goal_percent :
559+ total_donations = get_total_donations_amount_cache ()
560+ donation_towards_goal_percent = (
561+ (total_donations / DONATION_GOAL_AMOUNT ) * 100
562+ if DONATION_GOAL_AMOUNT > 0
563+ else 0
564+ )
565+ cache .set (
566+ CACHE_KEY_DONATION_TOWARDS_GOAL_PERCENT ,
567+ donation_towards_goal_percent ,
568+ STATS_CACHE_TIMEOUT ,
569+ )
570+ return donation_towards_goal_percent
571+
572+
573+ def get_donations_stats_dict ():
574+ stats_dict = {}
575+ stats_dict [DONATIONS_GOAL ] = DONATION_GOAL_AMOUNT
576+ stats_dict [CACHE_KEY_DONATION_BREAKDOWN ] = {
577+ "total_donations_amount" : get_total_donations_amount_cache (),
578+ "donors_count" : get_donors_count_cache (),
579+ "donation_towards_goal_percent" : get_donation_to_goal_percent_cache (),
580+ }
581+ return stats_dict
0 commit comments