Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. from datetime import date
  2.  
  3. from django.contrib.auth import get_user_model
  4. from django.contrib.auth.decorators import user_passes_test
  5. from django.db.models import Count
  6. from django.shortcuts import render
  7. from django.utils import timezone
  8.  
  9. from organisations.models import Organisation
  10.  
  11. User = get_user_model()
  12.  
  13.  
  14. @user_passes_test(lambda u: u.is_staff, login_url='/admin/')
  15. def dashboard(request, year=None, month=None):
  16.  
  17.     if not year or not month:
  18.         now = timezone.now()
  19.         year = now.year
  20.         month = now.month
  21.  
  22.     report_period = date(year=int(year), month=int(month), day=1)
  23.  
  24.     organisations = Organisation.objects.filter(
  25.         date_reached__year=report_period.year,
  26.         date_reached__month=report_period.month,
  27.         reviewed=True,
  28.     ).order_by('name').distinct()
  29.  
  30.     countries = organisations.values('country').annotate(
  31.         organisations=Count('country')
  32.     ).order_by('-organisations')
  33.  
  34.     return render(request, 'analytics/dashboard.html', {
  35.         'organisations': organisations,
  36.         'countries': countries,
  37.         'num_organisations': organisations.count(),
  38.         'report_period': report_period,
  39.     })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement