calendar.day_names can't be trusted, if the host's locale changes.

This commit is contained in:
A. Svensson 2015-03-09 16:47:22 +01:00
parent cd3c9d0fd9
commit 6a489bc4f7

View File

@ -1,12 +1,22 @@
from datetime import timedelta from datetime import timedelta
import calendar
from ast import literal_eval from ast import literal_eval
from django.db import models from django.db import models
from django.utils import timezone from django.utils import timezone
DAY_NAMES = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
]
class Server(models.Model): class Server(models.Model):
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
game_url = models.CharField(max_length=255) game_url = models.CharField(max_length=255)
@ -60,21 +70,17 @@ class Server(models.Model):
def measure_weekdays(self, days=7): def measure_weekdays(self, days=7):
weekdays = [] weekdays = []
history = self.get_stats_history(days=days) history = self.get_stats_history(days=days)
for i, day in enumerate(calendar.day_name): for i, day in enumerate(DAY_NAMES):
# HACK: do some number juggling to convert from calendar to django, # HACK: Fuck django for defaulting the start of a week to sunday...
# because SOMEONE didn't bother to follow THE FUCKING STANDARD # Oh, and it's not even zero indexed like the rest of the python
# # community! So much fun with number juggling...
# calendar is zero indexed, first day of week defaults to monday
# (monday = 0, tuesday = 1 etc.)
#
# django isn't zero indexed, first day of week defaults to sunday
# (sunday = 1, monday = 2 etc.)
i += 2 i += 2
if i > 7: i = 1 if i > 7: i = 1
# NOTE: using __week_day is dependant on pytz # NOTE: using __week_day is dependant on pytz
tmp = history.filter(created__week_day=i) tmp = history.filter(created__week_day=i)
avg = tmp.aggregate(models.Avg('players'))['players__avg'] or 0 avg = tmp.aggregate(models.Avg('players'))['players__avg'] or 0
weekdays.append((day, int(avg))) weekdays.append((day, int(avg)))
return weekdays return weekdays
def update_stats(self, player_count=0): def update_stats(self, player_count=0):
@ -88,7 +94,7 @@ class Server(models.Model):
def get_averages_for_weekdays(self): def get_averages_for_weekdays(self):
tmp = literal_eval(self.averages_for_weekdays) tmp = literal_eval(self.averages_for_weekdays)
return zip(calendar.day_name, tmp) return zip(DAY_NAMES, tmp)
class ServerHistory(models.Model): class ServerHistory(models.Model):
server = models.ForeignKey(Server) server = models.ForeignKey(Server)