Allow running without CAS.
[redakcja.git] / src / catalogue / views.py
index f620805..dcb68d8 100644 (file)
@@ -1,17 +1,17 @@
-# -*- coding: utf-8 -*-
+# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
+# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
+#
 from collections import defaultdict
 from datetime import datetime, date, timedelta
 import logging
 import os
-from StringIO import StringIO
-from urllib import unquote
-from urlparse import urlsplit, urlunsplit
+from urllib.parse import unquote, urlsplit, urlunsplit
 
 from django.conf import settings
 from django.contrib import auth
 from django.contrib.auth.models import User
 from django.contrib.auth.decorators import login_required, permission_required
-from django.core.urlresolvers import reverse
+from django.urls import reverse
 from django.db.models import Count, Q
 from django.db import transaction
 from django import http
@@ -22,7 +22,7 @@ from django.utils.encoding import iri_to_uri
 from django.utils.http import urlquote_plus
 from django.utils.translation import ugettext_lazy as _
 from django.views.decorators.http import require_POST
-from django_cas.decorators import user_passes_test
+from django_cas_ng.decorators import user_passes_test
 
 from apiclient import NotAuthorizedError
 from catalogue import forms
@@ -114,7 +114,7 @@ def create_missing(request, slug=None):
         form = forms.DocumentCreateForm(request.POST, request.FILES)
         if form.is_valid():
             
-            if request.user.is_authenticated():
+            if request.user.is_authenticated:
                 creator = request.user
             else:
                 creator = None
@@ -150,7 +150,7 @@ def upload(request):
         if form.is_valid():
             from slugify import slugify
 
-            if request.user.is_authenticated():
+            if request.user.is_authenticated:
                 creator = request.user
             else:
                 creator = None
@@ -397,7 +397,7 @@ def chunk_add(request, slug, chunk):
     if request.method == "POST":
         form = forms.ChunkAddForm(request.POST, instance=doc)
         if form.is_valid():
-            if request.user.is_authenticated():
+            if request.user.is_authenticated:
                 creator = request.user
             else:
                 creator = None
@@ -463,14 +463,14 @@ def chunk_edit(request, slug, chunk):
 @login_required
 @require_POST
 def chunk_mass_edit(request):
-    ids = map(int, filter(lambda i: i.strip()!='', request.POST.get('ids').split(',')))
-    chunks = map(lambda i: Chunk.objects.get(id=i), ids)
+    ids = [int(i) for i in request.POST.get('ids').split(',') if i.strip()]
+    chunks = list(Chunk.objects.filter(id__in=ids))
     
     stage = request.POST.get('stage')
     if stage:
         try:
             stage = Chunk.tag_model.objects.get(slug=stage)
-        except Chunk.DoesNotExist, e:
+        except Chunk.DoesNotExist as e:
             stage = None
        
         for c in chunks: c.stage = stage
@@ -481,7 +481,7 @@ def chunk_mass_edit(request):
     if username:
         try:
             user = User.objects.get(username=username)
-        except User.DoesNotExist, e:
+        except User.DoesNotExist as e:
             user = None
             
         for c in chunks: c.user = user
@@ -490,7 +490,7 @@ def chunk_mass_edit(request):
     if project_id:
         try:
             project = Project.objects.get(pk=int(project_id))
-        except (Project.DoesNotExist, ValueError), e:
+        except (Project.DoesNotExist, ValueError) as e:
             project = None
         for c in chunks:
             book = c.book
@@ -513,7 +513,7 @@ def image_mass_edit(request):
     if stage:
         try:
             stage = Image.tag_model.objects.get(slug=stage)
-        except Image.DoesNotExist, e:
+        except Image.DoesNotExist as e:
             stage = None
        
         for c in images: c.stage = stage
@@ -524,7 +524,7 @@ def image_mass_edit(request):
     if username:
         try:
             user = User.objects.get(username=username)
-        except User.DoesNotExist, e:
+        except User.DoesNotExist as e:
             user = None
             
         for c in images: c.user = user
@@ -533,7 +533,7 @@ def image_mass_edit(request):
     if project_id:
         try:
             project = Project.objects.get(pk=int(project_id))
-        except (Project.DoesNotExist, ValueError), e:
+        except (Project.DoesNotExist, ValueError) as e:
             project = None
         for c in images:
             c.project = project
@@ -584,7 +584,7 @@ def publish(request, slug):
         book.publish(request.user, host=protocol + request.get_host(), days=days, beta=beta)
     except NotAuthorizedError:
         return http.HttpResponseRedirect(reverse('apiclient_oauth' if not beta else 'apiclient_beta_oauth'))
-    except BaseException, e:
+    except BaseException as e:
         return http.HttpResponse(repr(e))
     else:
         return http.HttpResponseRedirect(book.get_absolute_url())
@@ -601,7 +601,7 @@ def publish_image(request, slug):
         image.publish(request.user)
     except NotAuthorizedError:
         return http.HttpResponseRedirect(reverse('apiclient_oauth'))
-    except BaseException, e:
+    except BaseException as e:
         return http.HttpResponse(e)
     else:
         return http.HttpResponseRedirect(image.get_absolute_url())
@@ -626,13 +626,13 @@ class GalleryView(UploadView):
 
 
 def active_users_list(request):
-    since = date(date.today().year, 1, 1)
+    year = int(request.GET.get('y', date.today().year))
     by_user = defaultdict(lambda: 0)
     by_email = defaultdict(lambda: 0)
     names_by_email = defaultdict(set)
     for change_model in (Chunk.change_model, Image.change_model):
         for c in change_model.objects.filter(
-                created_at__gte=since).order_by(
+                created_at__year=year).order_by(
                 'author', 'author_email', 'author_name').values(
                 'author', 'author_name', 'author_email').annotate(
                 c=Count('author'), ce=Count('author_email')).distinct():
@@ -640,7 +640,7 @@ def active_users_list(request):
                 by_user[c['author']] += c['c']
             else:
                 by_email[c['author_email']] += c['ce']
-                if c['author_name'].strip():
+                if (c['author_name'] or '').strip():
                     names_by_email[c['author_email']].add(c['author_name'])
     for user in User.objects.filter(pk__in=by_user):
         by_email[user.email] += by_user[user.pk]
@@ -652,7 +652,7 @@ def active_users_list(request):
     active_users.sort(key=lambda x: -x[2])
     return render(request, 'catalogue/active_users_list.html', {
         'users': active_users,
-        'since': since,
+        'year': year,
     })