fix all pylint complaints

This commit is contained in:
Daan Sprenkels
2019-01-21 22:26:43 +01:00
parent 2c41e85753
commit 19c1c70cd3
28 changed files with 85 additions and 119 deletions

View File

@ -1,3 +0,0 @@
from django.contrib import admin
# Register your models here.

View File

@ -1,3 +0,0 @@
from django.db import models
# Create your models here.

View File

@ -1,3 +0,0 @@
from django.test import TestCase
# Create your tests here.

View File

@ -1,9 +1,8 @@
from datetime import datetime, timedelta, time
from datetime import datetime, timedelta
from django.core.cache import caches
from django.conf import settings
from django.db.models import Count, Q, Sum
from django.shortcuts import render
from django.utils import timezone
from queues.models import PlaylistSong
@ -16,10 +15,10 @@ def recache_stats():
caches['default'].delete('stats')
caches['default'].set('stats', new_stats, 2 * 3600)
return new_stats
def recache_user_stats():
users = User.objects.exclude(Q(id=None)
| Q(id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values('id')
| Q(id__in=settings.STATS_REQUEST_IGNORE_USER_IDS)).values('id')
for user in users:
new_stats = user_stats(user['id'])
cacheloc = 'userstats_{}'.format(user['id'])
@ -27,11 +26,6 @@ def recache_user_stats():
caches['default'].set(cacheloc, new_stats, 48 * 3600)
return new_stats
def to_days(time):
for tr in time:
tr['duration'] = str(round(tr['total'] / 86400, 2)) + ' days'
return time
def compute_stats():
# We want to grab the time now, because otherwise we would be reporting a minute too late
last_updated = datetime.now()
@ -79,7 +73,7 @@ def compute_stats():
timedelta(days=14)).exclude(user_id=None).values(
'song__artist',
'song__title').annotate(total=Count('id')).order_by(
'-total', 'song__artist')[:settings.STATS_TOP_COUNT]
'-total', 'song__artist')[:settings.STATS_TOP_COUNT]
time_requested = PlaylistSong.objects.filter(state=2).exclude(
Q(user_id=None)
@ -98,6 +92,11 @@ def compute_stats():
'song__user__id', 'song__user__name').annotate(total=Count(
'song__user__id')).order_by('-total')[:settings.STATS_TOP_COUNT]
# Convert requested time to days
time_requested = list(time_requested)
for tr in time_requested:
tr['duration'] = str(round(tr['total'] / 86400, 2)) + ' days'
return {
'last_updated': last_updated,
'total_uploads': total_uploads,
@ -108,7 +107,7 @@ def compute_stats():
'total_unique_requests': total_unique_requests,
'most_played_songs': list(most_played_songs),
'most_played_songs_14_days': list(most_played_songs_14_days),
'time_requested': to_days(list(time_requested)),
'time_requested': time_requested,
'total_time_requested': str(round(float(total_time_requested['total']) / 86400, 2)) + ' days',
'stats_top_count': settings.STATS_TOP_COUNT,
'most_requested_uploaders': list(most_requested_uploaders),
@ -167,4 +166,3 @@ def user_stats(request):
'stats_top_count': settings.STATS_TOP_COUNT,
'total_played_uploads': total_played_uploads['newtotal'],
}

View File

@ -2,34 +2,32 @@ 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):
stats = caches['default'].get('stats')
stats_data = caches['default'].get('stats')
status = 503
current_age = None
current_age_text = None
if stats:
if stats_data:
status = 200
current_age_text = age_text(stats['last_updated'])
current_age_text = age_text(stats_data['last_updated'])
data = {'stats': stats, 'current_age': current_age, 'current_age_text': current_age_text}
data = {'stats': stats_data, '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))
stats_data = caches['default'].get('userstats_{}'.format(request.user.id))
status = 503
current_age = None
current_age_text = None
if stats:
if stats_data:
status = 200
current_age_text = age_text(stats['last_updated'])
current_age_text = age_text(stats_data['last_updated'])
data = {'stats': stats, 'current_age': current_age, 'current_age_text': current_age_text}
data = {'stats': stats_data, 'current_age': current_age, 'current_age_text': current_age_text}
return render(request, 'stats/user.html', data, status=status)
def age_text(last_updated):
@ -40,7 +38,6 @@ def age_text(last_updated):
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)
return 'Stats were updated {:.0f} {} and {:.0f} {} ago.'.format(
hours, hourstr, minutes, minutestr)