Handle FAT ZeroDivisionErrors

This commit is contained in:
Adarnof 2017-10-03 21:50:32 -04:00
parent cd0afeca15
commit cca8b26375

View File

@ -37,8 +37,12 @@ class CorpStat(object):
self.n_fats = Fat.objects.filter(character__corporation_id=self.corp.corporation_id).filter( self.n_fats = Fat.objects.filter(character__corporation_id=self.corp.corporation_id).filter(
fatlink__fatdatetime__gte=start_of_month).filter(fatlink__fatdatetime__lte=start_of_next_month).count() fatlink__fatdatetime__gte=start_of_month).filter(fatlink__fatdatetime__lte=start_of_next_month).count()
@property
def avg_fat(self): def avg_fat(self):
return "%.2f" % (float(self.n_fats) / float(self.corp.member_count)) try:
return "%.2f" % (float(self.n_fats) / float(self.corp.member_count))
except ZeroDivisionError:
return "%.2f" % 0
class MemberStat(object): class MemberStat(object):
@ -55,8 +59,12 @@ class MemberStat(object):
self.n_fats = Fat.objects.filter(user_id=member.pk).filter( self.n_fats = Fat.objects.filter(user_id=member.pk).filter(
fatlink__fatdatetime__gte=start_of_month).filter(fatlink__fatdatetime__lte=start_of_next_month).count() fatlink__fatdatetime__gte=start_of_month).filter(fatlink__fatdatetime__lte=start_of_next_month).count()
@property
def avg_fat(self): def avg_fat(self):
return "%.2f" % (float(self.n_fats) / float(self.n_chars)) try:
return "%.2f" % (float(self.n_fats) / float(self.n_chars))
except ZeroDivisionError:
return "%.2f" % 0
def first_day_of_next_month(year, month): def first_day_of_next_month(year, month):
@ -117,7 +125,7 @@ def fatlink_statistics_corp_view(request, corpid, year=None, month=None):
# collect and sort stats # collect and sort stats
stat_list = [fat_stats[x] for x in fat_stats] stat_list = [fat_stats[x] for x in fat_stats]
stat_list.sort(key=lambda stat: stat.mainchar.character_name) stat_list.sort(key=lambda stat: stat.mainchar.character_name)
stat_list.sort(key=lambda stat: (stat.n_fats, stat.n_fats / stat.n_chars), reverse=True) stat_list.sort(key=lambda stat: (stat.n_fats, stat.avg_fat), reverse=True)
context = {'fatStats': stat_list, 'month': start_of_month.strftime("%B"), 'year': year, context = {'fatStats': stat_list, 'month': start_of_month.strftime("%B"), 'year': year,
'previous_month': start_of_previous_month, 'corpid': corpid} 'previous_month': start_of_previous_month, 'corpid': corpid}
@ -153,7 +161,7 @@ def fatlink_statistics_view(request, year=datetime.date.today().year, month=date
# collect and sort stats # collect and sort stats
stat_list = [fat_stats[x] for x in fat_stats] stat_list = [fat_stats[x] for x in fat_stats]
stat_list.sort(key=lambda stat: stat.corp.corporation_name) stat_list.sort(key=lambda stat: stat.corp.corporation_name)
stat_list.sort(key=lambda stat: (stat.n_fats, stat.n_fats / stat.corp.member_count), reverse=True) stat_list.sort(key=lambda stat: (stat.n_fats, stat.avg_fat), reverse=True)
context = {'fatStats': stat_list, 'month': start_of_month.strftime("%B"), 'year': year, context = {'fatStats': stat_list, 'month': start_of_month.strftime("%B"), 'year': year,
'previous_month': start_of_previous_month} 'previous_month': start_of_previous_month}