mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 21:52:21 +01:00
Song reporting and user stats
This commit is contained in:
committed by
Daan Sprenkels
parent
0c1f9cb08d
commit
f6fcc63450
@ -2,6 +2,8 @@ from datetime import datetime, timedelta
|
||||
|
||||
from django.core.cache import caches
|
||||
from django.shortcuts import render
|
||||
from stats.utils import user_stats
|
||||
from django.http import JsonResponse, HttpResponseForbidden
|
||||
|
||||
|
||||
def stats(request):
|
||||
@ -12,17 +14,33 @@ def stats(request):
|
||||
|
||||
if stats:
|
||||
status = 200
|
||||
if 'last_updated' in stats:
|
||||
current_age = datetime.now() - stats['last_updated']
|
||||
if current_age < timedelta(minutes=1):
|
||||
current_age_text = 'Stats were updated less than a minute ago.'
|
||||
elif current_age < timedelta(minutes=2):
|
||||
current_age_text = 'Stats were updated one minute ago.'
|
||||
elif current_age < timedelta(minutes=60):
|
||||
minutes = current_age.seconds / 60
|
||||
current_age_text = 'Stats were updated {:.0f} minutes ago.'.format(minutes)
|
||||
else:
|
||||
current_age_text = 'Stats were updated more than an hour ago'
|
||||
current_age_text = age_text(stats['last_updated'])
|
||||
|
||||
data = {'stats': stats, 'current_age': current_age, 'current_age_text': current_age_text}
|
||||
return render(request, 'stats/stats.html', data, status=status)
|
||||
|
||||
def user_stats(request):
|
||||
stats = caches['default'].get('userstats_{}'.format(request.user.id))
|
||||
status = 503
|
||||
current_age = None
|
||||
current_age_text = None
|
||||
|
||||
if stats:
|
||||
status = 200
|
||||
current_age_text = age_text(stats['last_updated'])
|
||||
|
||||
data = {'stats': stats, 'current_age': current_age, 'current_age_text': current_age_text}
|
||||
return render(request, 'stats/user.html', data, status=status)
|
||||
|
||||
def age_text(last_updated):
|
||||
current_age = datetime.now() - last_updated
|
||||
minutes = (current_age.seconds % 3600) / 60
|
||||
hours = current_age.seconds / 3600
|
||||
minutestr = "minute" if minutes == 1 else "minutes"
|
||||
hourstr = "hour" if hours == 1 else "hours"
|
||||
if current_age < timedelta(hours=1):
|
||||
return 'Stats were updated {:.0f} {} ago.'.format(minutes, minutestr)
|
||||
else:
|
||||
return 'Stats were updated {:.0f} {} and {:.0f} {} ago.'.format(
|
||||
hours, hourstr, minutes, minutestr)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user