File python-django-waliki-django2.patch of Package python-django-waliki

From bdcae9c1d6022f8179760a8c1366f732e551c599 Mon Sep 17 00:00:00 2001
From: Oleg Girko <ol@infoserver.lv>
Date: Wed, 2 May 2018 04:06:18 +0100
Subject: [PATCH] Make Waliki compatible with Django 2.0.

The following compatibility issues have been fixed:
* new stype middleware configuration using "MIDDLEWARE" settings key;
* using django.urls instead of django.core.urlresolvers package;
* is_authenticated is now property of User, not method;
* on_delete parameter is mandatory for ForeignKey model.

Project is marked as Django 2.0 compatible in setup.py file.

Signed-off-by: Oleg Girko <ol@infoserver.lv>
---
 runtests.py                                   |  8 ++++++++
 setup.py                                      |  2 ++
 tests/test_git.py                             |  6 +++++-
 tests/test_git_webhook.py                     |  6 +++++-
 tests/test_redirects.py                       |  6 +++++-
 tests/test_slides.py                          |  6 +++++-
 tests/test_views.py                           |  6 +++++-
 waliki/acl.py                                 |  7 ++++---
 waliki/attachments/migrations/0001_initial.py |  2 +-
 waliki/attachments/models.py                  | 10 ++++++++--
 waliki/git/models.py                          |  3 ++-
 waliki/git/views.py                           |  6 +++++-
 waliki/models.py                              |  6 +++++-
 waliki/search/views.py                        |  4 +++-
 waliki/templatetags/waliki_tags.py            |  6 +++++-
 waliki/utils.py                               | 13 ++++++++++++-
 waliki/views.py                               |  6 +++++-
 17 files changed, 85 insertions(+), 18 deletions(-)

diff --git a/runtests.py b/runtests.py
index b1c194b..367bc9b 100755
--- a/runtests.py
+++ b/runtests.py
@@ -42,6 +42,14 @@ try:
                 },
             },
         ],
+        MIDDLEWARE=(
+            'django.contrib.sessions.middleware.SessionMiddleware',
+            'django.middleware.common.CommonMiddleware',
+            'django.middleware.csrf.CsrfViewMiddleware',
+            'django.contrib.auth.middleware.AuthenticationMiddleware',
+            'django.contrib.messages.middleware.MessageMiddleware',
+            'django.middleware.clickjacking.XFrameOptionsMiddleware',
+        ),
         MIDDLEWARE_CLASSES=(
             'django.contrib.sessions.middleware.SessionMiddleware',
             'django.middleware.common.CommonMiddleware',
diff --git a/setup.py b/setup.py
index 150f541..a8aff1b 100755
--- a/setup.py
+++ b/setup.py
@@ -72,6 +72,8 @@ setup(
         'Framework :: Django :: 1.8',
         'Framework :: Django :: 1.9',
         'Framework :: Django :: 1.10',
+        'Framework :: Django :: 1.11',
+        'Framework :: Django :: 2.0',
         'Programming Language :: Python :: 2',
         'Programming Language :: Python :: 2.7',
         'Programming Language :: Python :: 3',
diff --git a/tests/test_git.py b/tests/test_git.py
index 21534c5..cb24fad 100644
--- a/tests/test_git.py
+++ b/tests/test_git.py
@@ -3,8 +3,12 @@ import os
 import shutil
 from sh import git
 from mock import patch, PropertyMock
+from django import VERSION
 from django.test import TestCase
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from waliki.models import Page
 from waliki.git.models import Git
 from waliki.settings import WALIKI_DATA_DIR, WALIKI_COMMITTER_EMAIL, WALIKI_COMMITTER_NAME
diff --git a/tests/test_git_webhook.py b/tests/test_git_webhook.py
index 8c10eb3..ddba324 100644
--- a/tests/test_git_webhook.py
+++ b/tests/test_git_webhook.py
@@ -1,5 +1,9 @@
+from django import VERSION
 from django.test import TestCase
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from .factories import PageFactory
 from waliki.settings import WALIKI_DATA_DIR
 from waliki.models import Page
diff --git a/tests/test_redirects.py b/tests/test_redirects.py
index 5aca975..dd28c75 100644
--- a/tests/test_redirects.py
+++ b/tests/test_redirects.py
@@ -1,5 +1,9 @@
+from django import VERSION
 from django.test import TestCase
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from .factories import PageFactory, RedirectFactory
 
 
diff --git a/tests/test_slides.py b/tests/test_slides.py
index 1e598f3..17b1bcb 100644
--- a/tests/test_slides.py
+++ b/tests/test_slides.py
@@ -2,9 +2,13 @@ import json
 import os
 import mock
 import unittest
+from django import VERSION
 from django.test import TestCase
 from waliki.models import Page
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from django.core.exceptions import PermissionDenied
 from django.template.loader import render_to_string
 from waliki import settings
diff --git a/tests/test_views.py b/tests/test_views.py
index adbf131..2aebbab 100644
--- a/tests/test_views.py
+++ b/tests/test_views.py
@@ -1,9 +1,13 @@
 import json
 import os
 import mock
+from django import VERSION
 from django.test import TestCase
 from waliki.models import Page
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from django.template.loader import render_to_string
 from django import forms
 from waliki.models import Redirect
diff --git a/waliki/acl.py b/waliki/acl.py
index dfa0712..2f67175 100644
--- a/waliki/acl.py
+++ b/waliki/acl.py
@@ -9,6 +9,7 @@ from django.utils.six.moves.urllib.parse import urlparse
 from django.utils.six import string_types
 from django.contrib.auth import REDIRECT_FIELD_NAME
 from django.shortcuts import resolve_url
+from waliki.utils import is_authenticated
 from .models import ACLRule
 from .settings import (WALIKI_ANONYMOUS_USER_PERMISSIONS,
                        WALIKI_LOGGED_USER_PERMISSIONS,
@@ -31,7 +32,7 @@ def check_perms(perms, user, slug, raise_exception=False):
     if perms.issubset(set(WALIKI_ANONYMOUS_USER_PERMISSIONS)):
         return True
 
-    if user.is_authenticated() and perms.issubset(set(WALIKI_LOGGED_USER_PERMISSIONS)):
+    if is_authenticated(user) and perms.issubset(set(WALIKI_LOGGED_USER_PERMISSIONS)):
         return True
 
     # First check if the user has the permission (even anon users)
@@ -61,7 +62,7 @@ def permission_required(perms, login_url=None, raise_exception=False, redirect_f
 
             if check_perms(perms, request.user, kwargs['slug'], raise_exception=raise_exception):
                 return view_func(request, *args, **kwargs)
-            if request.user.is_authenticated():
+            if is_authenticated(request.user):
                 if WALIKI_RENDER_403:
                     return render(request, 'waliki/403.html', kwargs, status=403)
                 else:
@@ -83,4 +84,4 @@ def permission_required(perms, login_url=None, raise_exception=False, redirect_f
                 path, resolved_login_url, redirect_field_name)
         return _wrapped_view
 
-    return decorator
\ No newline at end of file
+    return decorator
diff --git a/waliki/attachments/migrations/0001_initial.py b/waliki/attachments/migrations/0001_initial.py
index db40ade..43e248c 100644
--- a/waliki/attachments/migrations/0001_initial.py
+++ b/waliki/attachments/migrations/0001_initial.py
@@ -17,7 +17,7 @@ class Migration(migrations.Migration):
             fields=[
                 ('id', models.AutoField(serialize=False, verbose_name='ID', primary_key=True, auto_created=True)),
                 ('file', models.FileField(upload_to=waliki.settings.WALIKI_UPLOAD_TO)),
-                ('page', models.ForeignKey(related_name='attachments', to='waliki.Page')),
+                ('page', models.ForeignKey(related_name='attachments', to='waliki.Page', on_delete=models.CASCADE)),
             ],
             options={
                 'verbose_name': 'Attachment',
diff --git a/waliki/attachments/models.py b/waliki/attachments/models.py
index 04b42fe..52ebfec 100644
--- a/waliki/attachments/models.py
+++ b/waliki/attachments/models.py
@@ -1,19 +1,25 @@
 # -*- coding: utf-8 -*-
 import os.path
+from django import VERSION
 from django.db import models
 from django.utils.translation import ugettext_lazy as _
 from django.db.models.signals import pre_delete
 from django.dispatch.dispatcher import receiver
 from django.utils.encoding import python_2_unicode_compatible
 from django.utils.six import text_type
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from waliki.models import Page
 from waliki.settings import WALIKI_UPLOAD_TO
 
 
 @python_2_unicode_compatible
 class Attachment(models.Model):
-    page = models.ForeignKey(Page, related_name='attachments')
+    page = models.ForeignKey(Page,
+                             on_delete=models.CASCADE,
+                             related_name='attachments')
     file = models.FileField(upload_to=WALIKI_UPLOAD_TO, max_length=300)
 
     class Meta:
diff --git a/waliki/git/models.py b/waliki/git/models.py
index d11f38c..5161de8 100644
--- a/waliki/git/models.py
+++ b/waliki/git/models.py
@@ -12,6 +12,7 @@ from collections import namedtuple
 from waliki.signals import page_saved, page_preedit, page_moved
 from waliki import settings
 from waliki.models import Page
+from waliki.utils import is_authenticated
 
 
 git = git.bake("--no-pager", _tty_out=False)
@@ -39,7 +40,7 @@ class Git(object):
             paths_to_commit.append(extra_path)
         kwargs = {}
         User = get_user_model()
-        if isinstance(author, User) and author.is_authenticated():
+        if isinstance(author, User) and is_authenticated(author):
             kwargs['author'] = u"%s <%s>" % (author.get_full_name() or author.username, author.email)
         elif isinstance(author, six.string_types):
             kwargs['author'] = author
diff --git a/waliki/git/views.py b/waliki/git/views.py
index 2e5bbec..e1237a6 100644
--- a/waliki/git/views.py
+++ b/waliki/git/views.py
@@ -2,13 +2,17 @@ import json
 from django.utils import timezone
 
 from datetime import datetime
+from django import VERSION
 from django.templatetags.tz import localtime
 from django.shortcuts import render, get_object_or_404, redirect
 from django.utils.translation import ugettext_lazy as _
 from django.http import Http404
 from django.core.management import call_command
 from django.http import HttpResponse
-from django.core.urlresolvers import reverse, reverse_lazy
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse, reverse_lazy
+else:
+    from django.core.urlresolvers import reverse, reverse_lazy
 from django.utils.encoding import smart_text
 from django.utils.six import StringIO, text_type
 from django.views.decorators.csrf import csrf_exempt
diff --git a/waliki/models.py b/waliki/models.py
index a3afa76..198d868 100644
--- a/waliki/models.py
+++ b/waliki/models.py
@@ -3,11 +3,15 @@ import os
 import codecs
 import shutil
 import os.path
+from django import VERSION
 from django.db import models
 from django.db.models import Q
 from django.db.utils import IntegrityError
 from django.conf import settings
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from django.dispatch import receiver
 from django.utils.six import string_types
 from django.utils.translation import ugettext_lazy as _
diff --git a/waliki/search/views.py b/waliki/search/views.py
index 09bd6bc..abac925 100644
--- a/waliki/search/views.py
+++ b/waliki/search/views.py
@@ -6,11 +6,13 @@ from django.contrib.auth.decorators import user_passes_test
 
 from django.conf import settings
 
+from waliki.utils import is_authenticated
+
 def user_has_permission(user):
     if 'view_page' in settings.WALIKI_ANONYMOUS_USER_PERMISSIONS:
         return True
 
-    if user.is_authenticated() and user.is_active:
+    if is_authenticated(user) and user.is_active:
         return True
 
     return False
diff --git a/waliki/templatetags/waliki_tags.py b/waliki/templatetags/waliki_tags.py
index 73fb1da..5d2bb9f 100644
--- a/waliki/templatetags/waliki_tags.py
+++ b/waliki/templatetags/waliki_tags.py
@@ -1,5 +1,9 @@
+from django import VERSION
 from django import template
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from waliki.acl import check_perms as check_perms_helper
 from waliki.models import Page
 from waliki.forms import PageForm
diff --git a/waliki/utils.py b/waliki/utils.py
index 4fdc977..27dadc3 100644
--- a/waliki/utils.py
+++ b/waliki/utils.py
@@ -2,8 +2,12 @@ import os
 import re
 import mimetypes
 import unicodedata
+from django import VERSION
 from django.http import HttpResponse
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from django.utils.six import PY2
 from django.utils.encoding import force_text
 from django.utils.safestring import mark_safe
@@ -49,3 +53,10 @@ def send_file(path, filename=None, content_type=None):
     response['Content-Disposition'] = 'attachment; filename=%s' % filename
     response.write(open(path, "rb").read())
     return response
+
+if VERSION[:2] >= (1, 10):
+    def is_authenticated(user):
+        return user.is_authenticated
+else:
+    def is_authenticated(user):
+        return user.is_authenticated()
diff --git a/waliki/views.py b/waliki/views.py
index 45a9d6c..2cb8cbc 100644
--- a/waliki/views.py
+++ b/waliki/views.py
@@ -1,11 +1,15 @@
 import json
+from django import VERSION
 from django.http import HttpResponse, Http404, HttpResponseRedirect, HttpResponsePermanentRedirect
 from django.shortcuts import render, redirect, get_object_or_404
 from django.template import RequestContext
 from django.template.loader import render_to_string
 from django.utils.translation import ugettext_lazy as _
 from django.contrib import messages
-from django.core.urlresolvers import reverse
+if VERSION[:2] >= (1, 10):
+    from django.urls import reverse
+else:
+    from django.core.urlresolvers import reverse
 from .models import Page, Redirect
 from .forms import PageForm, MovePageForm, DeleteForm, NewPageForm
 from .signals import page_saved, page_preedit, page_moved
-- 
2.14.3