From 8450f42a200e16b038649dc91e3cb1651a67f771 Mon Sep 17 00:00:00 2001 From: Oliver Falk Date: Wed, 30 Dec 2020 13:41:04 +0100 Subject: [PATCH] Implement simple stats feature --- ivatar/urls.py | 3 ++- ivatar/views.py | 27 ++++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/ivatar/urls.py b/ivatar/urls.py index df3b450..96b4d22 100644 --- a/ivatar/urls.py +++ b/ivatar/urls.py @@ -7,7 +7,7 @@ from django.conf.urls import url from django.conf.urls.static import static from django.views.generic import TemplateView, RedirectView from ivatar import settings -from . views import AvatarImageView, GravatarProxyView +from . views import AvatarImageView, GravatarProxyView, StatsView urlpatterns = [ # pylint: disable=invalid-name path('admin/', admin.site.urls), @@ -36,6 +36,7 @@ urlpatterns = [ # pylint: disable=invalid-name url('privacy/', TemplateView.as_view(template_name='privacy.html'), name='privacy'), url('contact/', TemplateView.as_view(template_name='contact.html'), name='contact'), path('talk_to_us/', RedirectView.as_view(url='/contact'), name='talk_to_us'), + url('stats/', StatsView.as_view(), name='stats'), ] MAINTENANCE = False diff --git a/ivatar/views.py b/ivatar/views.py index fb79958..d3de012 100644 --- a/ivatar/views.py +++ b/ivatar/views.py @@ -8,12 +8,14 @@ from urllib.request import urlopen from urllib.error import HTTPError, URLError from ssl import SSLError from django.views.generic.base import TemplateView, View -from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound +from django.http import HttpResponse, HttpResponseRedirect +from django.http import HttpResponseNotFound, JsonResponse from django.core.exceptions import ObjectDoesNotExist from django.core.cache import cache, caches from django.utils.translation import ugettext_lazy as _ from django.urls import reverse_lazy from django.db.models import Q +from django.contrib.auth.models import User from PIL import Image @@ -23,7 +25,8 @@ from pydenticon5 import Pydenticon5 import pagan from robohash import Robohash -from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE, CACHE_RESPONSE +from ivatar.settings import AVATAR_MAX_SIZE, JPEG_QUALITY, DEFAULT_AVATAR_SIZE +from ivatar.settings import CACHE_RESPONSE from ivatar.settings import CACHE_IMAGES_MAX_AGE from . ivataraccount.models import ConfirmedEmail, ConfirmedOpenId from . ivataraccount.models import pil_format, file_format @@ -60,7 +63,8 @@ class CachingHttpResponse(HttpResponse): ''' Handle caching of response ''' - def __init__(self, uri, content=b'', content_type=None, status=200, reason=None, charset=None): # pylint: disable=too-many-arguments + def __init__(self, uri, content=b'', content_type=None, status=200, # pylint: disable=too-many-arguments + reason=None, charset=None): if CACHE_RESPONSE: caches['filesystem'].set(uri, { 'content': content, @@ -394,3 +398,20 @@ class GravatarProxyView(View): # We shouldn't reach this point... But make sure we do something return redir_default(default) + + +class StatsView(TemplateView, JsonResponse): + ''' + Return stats + ''' + def get(self, request, *args, **kwargs): # pylint: disable=too-many-branches,too-many-statements,too-many-locals,no-self-use,unused-argument,too-many-return-statements + retval = { + 'users': User.objects.all().count(), + 'mails': ConfirmedEmail.objects.all().count(), + 'openids': ConfirmedOpenId.objects.all().count(), # pylint: disable=no-member + } + + if request.content_type == 'application/json': + return JsonResponse(retval) + + return HttpResponseRedirect(reverse_lazy('home'))