mirror of
https://gitlab.science.ru.nl/technicie/MarietjeDjango.git
synced 2025-12-09 21:52:21 +01:00
Marietje 4.1: Addition of Django REST framework, Swagger, Dark mode and updates to Django and Bootstrap
This commit is contained in:
@ -1,44 +1,40 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.views.generic import TemplateView
|
||||
from django.core.cache import caches
|
||||
from django.shortcuts import render
|
||||
|
||||
from stats.services import age_text
|
||||
|
||||
def stats(request):
|
||||
stats_data = caches['default'].get('stats')
|
||||
status = 503
|
||||
current_age = None
|
||||
current_age_text = None
|
||||
|
||||
if stats_data:
|
||||
status = 200
|
||||
current_age_text = age_text(stats_data['last_updated'])
|
||||
class StatsView(LoginRequiredMixin, TemplateView):
|
||||
template_name = "stats/stats.html"
|
||||
|
||||
data = {'stats': stats_data, 'current_age': current_age, 'current_age_text': current_age_text}
|
||||
return render(request, 'stats/stats.html', data, status=status)
|
||||
def get(self, request, **kwargs):
|
||||
stats_data = caches["default"].get("stats")
|
||||
status = 503
|
||||
current_age = None
|
||||
current_age_text = None
|
||||
|
||||
def user_stats(request):
|
||||
if stats_data:
|
||||
status = 200
|
||||
current_age_text = age_text(stats_data["last_updated"])
|
||||
|
||||
stats_data = caches['default'].get('userstats_{}'.format(request.user.id))
|
||||
status = 503
|
||||
current_age = None
|
||||
current_age_text = None
|
||||
data = {"stats": stats_data, "current_age": current_age, "current_age_text": current_age_text}
|
||||
return render(request, self.template_name, data, status=status)
|
||||
|
||||
if stats_data:
|
||||
status = 200
|
||||
current_age_text = age_text(stats_data['last_updated'])
|
||||
|
||||
data = {'stats': stats_data, 'current_age': current_age, 'current_age_text': current_age_text}
|
||||
return render(request, 'stats/user.html', data, status=status)
|
||||
class UserStatsView(LoginRequiredMixin, TemplateView):
|
||||
template_name = "stats/user.html"
|
||||
|
||||
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)
|
||||
def get(self, request, **kwargs):
|
||||
stats_data = caches["default"].get("userstats_{}".format(request.user.id))
|
||||
status = 503
|
||||
current_age = None
|
||||
current_age_text = None
|
||||
|
||||
return 'Stats were updated {:.0f} {} and {:.0f} {} ago.'.format(
|
||||
hours, hourstr, minutes, minutestr)
|
||||
if stats_data:
|
||||
status = 200
|
||||
current_age_text = age_text(stats_data["last_updated"])
|
||||
|
||||
data = {"stats": stats_data, "current_age": current_age, "current_age_text": current_age_text}
|
||||
return render(request, self.template_name, data, status=status)
|
||||
|
||||
Reference in New Issue
Block a user