Skip to content

Commit

Permalink
Merge pull request #218 from lzaoral/remove-old-django-support
Browse files Browse the repository at this point in the history
django: remove support for Django 1.10 and older
  • Loading branch information
rohanpm authored Aug 23, 2023
2 parents ac60424 + c7626a3 commit ab2d638
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 93 deletions.
4 changes: 1 addition & 3 deletions kobo/django/auth/admin.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from django.contrib.auth.admin import *
import django.contrib.admin as admin
from kobo.django.django_version import django_version_ge

from kobo.django.auth.models import *

# users are not displayed on admin page since migrations were introduced
if django_version_ge("1.9.0"):
admin.site.register(User, UserAdmin)
admin.site.register(User, UserAdmin)
6 changes: 2 additions & 4 deletions kobo/django/auth/middleware.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from django.contrib.auth.middleware import RemoteUserMiddleware
from kobo.django.django_version import django_version_ge
if django_version_ge('1.10.0'):
from django.utils.deprecation import MiddlewareMixin
from django.utils.deprecation import MiddlewareMixin

from kobo.django.helpers import call_if_callable

class LimitedRemoteUserMiddleware(RemoteUserMiddleware, MiddlewareMixin if django_version_ge('1.10.0') else object):
class LimitedRemoteUserMiddleware(RemoteUserMiddleware, MiddlewareMixin):
'''
Same behaviour as RemoteUserMiddleware except that it doesn't logout user
if is already logged in.
Expand Down
6 changes: 1 addition & 5 deletions kobo/django/menu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,7 @@

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from kobo.django.django_version import django_version_ge
if django_version_ge('1.10.0'):
from django.urls import reverse
else:
from django.core.urlresolvers import reverse
from django.urls import reverse
from django.utils.html import format_html

try:
Expand Down
6 changes: 2 additions & 4 deletions kobo/django/menu/middleware.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# -*- coding: utf-8 -*-

from kobo.django.django_version import django_version_ge
if django_version_ge('1.10.0'):
from django.utils.deprecation import MiddlewareMixin
from django.utils.deprecation import MiddlewareMixin

from kobo.django.menu import menu

Expand All @@ -22,7 +20,7 @@ def __get__(self, request, obj_type=None):
return request._cached_menu


class MenuMiddleware(MiddlewareMixin if django_version_ge('1.10.0') else object):
class MenuMiddleware(MiddlewareMixin):
"""
@summary: Middleware for menu object.
"""
Expand Down
11 changes: 2 additions & 9 deletions kobo/django/xmlrpc/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from django.contrib.sessions.models import Session

from kobo.django.auth.krb5 import Krb5RemoteUserBackend
from kobo.django.django_version import django_version_ge
from kobo.django.helpers import call_if_callable


Expand All @@ -37,10 +36,7 @@ def renew_session(request):
def login_password(request, username, password):
"""login_password(username, password): session_id"""
backend = ModelBackend()
if django_version_ge('1.11.0'):
user = backend.authenticate(None, username, password)
else:
user = backend.authenticate(username, password)
user = backend.authenticate(None, username, password)
if user is None:
raise PermissionDenied("Invalid username or password.")
user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
Expand Down Expand Up @@ -70,10 +66,7 @@ def login_krbv(request, krb_request, proxy_user=None):
# remove @REALM
username = cprinc.name.split("@")[0]
backend = Krb5RemoteUserBackend()
if django_version_ge('1.11.0'):
user = backend.authenticate(None, username)
else:
user = backend.authenticate(username)
user = backend.authenticate(None, username)
if user is None:
raise PermissionDenied()
user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
Expand Down
1 change: 0 additions & 1 deletion kobo/hub/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from kobo.exceptions import ImproperlyConfigured
from django.conf import settings
from kobo.django.django_version import django_version_ge


for var in ["XMLRPC_METHODS", "TASK_DIR", "UPLOAD_DIR"]:
Expand Down
6 changes: 2 additions & 4 deletions kobo/hub/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

from __future__ import absolute_import

from kobo.django.django_version import django_version_ge
if django_version_ge('1.10.0'):
from django.utils.deprecation import MiddlewareMixin
from django.utils.deprecation import MiddlewareMixin

from .models import Worker

Expand All @@ -28,7 +26,7 @@ def __get__(self, request, obj_type=None):
return request._cached_worker


class WorkerMiddleware(MiddlewareMixin if django_version_ge('1.10.0') else object):
class WorkerMiddleware(MiddlewareMixin):
"""Sets a request.worker.
- Worker instance if username exists in database
Expand Down
24 changes: 7 additions & 17 deletions kobo/hub/urls/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,10 @@
from django.conf.urls import url
import kobo.hub.views

if django_version_ge('1.11.0'):
urlpatterns = [
url(r"^login/$", kobo.hub.views.LoginView.as_view(), name="auth/login"),
url(r"^krb5login/$", kobo.hub.views.krb5login, name="auth/krb5login"),
url(r"^oidclogin/$", kobo.hub.views.oidclogin, name="auth/oidclogin"),
url(r"^tokenoidclogin/$", kobo.hub.views.oidclogin, name="auth/tokenoidclogin"),
url(r'^logout/$', kobo.hub.views.LogoutView.as_view(), name="auth/logout"),
]

else:
urlpatterns = [
url(r"^login/$", kobo.hub.views.login, name="auth/login"),
url(r"^krb5login/$", kobo.hub.views.krb5login, name="auth/krb5login"),
url(r"^oidclogin/$", kobo.hub.views.oidclogin, name="auth/oidclogin"),
url(r"^tokenoidclogin/$", kobo.hub.views.oidclogin, name="auth/tokenoidclogin"),
url(r'^logout/$', kobo.hub.views.logout, name="auth/logout"),
]
urlpatterns = [
url(r"^login/$", kobo.hub.views.LoginView.as_view(), name="auth/login"),
url(r"^krb5login/$", kobo.hub.views.krb5login, name="auth/krb5login"),
url(r"^oidclogin/$", kobo.hub.views.oidclogin, name="auth/oidclogin"),
url(r"^tokenoidclogin/$", kobo.hub.views.oidclogin, name="auth/tokenoidclogin"),
url(r'^logout/$', kobo.hub.views.LogoutView.as_view(), name="auth/logout"),
]
34 changes: 7 additions & 27 deletions kobo/hub/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import mimetypes
import os
import six
from kobo.django.django_version import django_version_ge

try:
import json
Expand All @@ -14,14 +13,10 @@
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME, get_user_model
from django.core.exceptions import ImproperlyConfigured
from kobo.django.django_version import django_version_ge
if django_version_ge('1.10.0'):
from django.urls import reverse
else:
from django.core.urlresolvers import reverse
from django.http import HttpResponse, StreamingHttpResponse, HttpResponseForbidden
from django.shortcuts import render, get_object_or_404
from django.template import RequestContext
from django.urls import reverse
from django.views.generic import RedirectView

from kobo.hub.models import Arch, Channel, Task
Expand Down Expand Up @@ -242,29 +237,18 @@ def task_log_json(request, id, log_name):

return HttpResponse(json.dumps(result), content_type="application/json")

if django_version_ge('1.11.0'):
class LoginView(django.contrib.auth.views.LoginView):
template_name = 'auth/login.html'

class LogoutView(django.contrib.auth.views.LogoutView):
pass
class LoginView(django.contrib.auth.views.LoginView):
template_name = 'auth/login.html'

else:
def login(request, redirect_field_name=REDIRECT_FIELD_NAME):
return django.contrib.auth.views.login(request, template_name="auth/login.html", redirect_field_name=redirect_field_name)

def logout(request, redirect_field_name=REDIRECT_FIELD_NAME):
return django.contrib.auth.views.logout(request, redirect_field_name=redirect_field_name)
class LogoutView(django.contrib.auth.views.LogoutView):
pass


def krb5login(request, redirect_field_name=REDIRECT_FIELD_NAME):
#middleware = 'django.contrib.auth.middleware.RemoteUserMiddleware'
middleware = 'kobo.django.auth.middleware.LimitedRemoteUserMiddleware'
if django_version_ge('1.10.0'):
middleware_setting = settings.MIDDLEWARE
else:
middleware_setting = settings.MIDDLEWARE_CLASSES
if middleware not in middleware_setting:
if middleware not in settings.MIDDLEWARE:
raise ImproperlyConfigured("krb5login view requires '%s' middleware installed" % middleware)
redirect_to = request.POST.get(redirect_field_name, "")
if not redirect_to:
Expand All @@ -275,10 +259,6 @@ def krb5login(request, redirect_field_name=REDIRECT_FIELD_NAME):

def oidclogin(request):
middleware = 'kobo.django.auth.middleware.LimitedRemoteUserMiddleware'
if django_version_ge('1.10.0'):
middleware_setting = settings.MIDDLEWARE
else:
middleware_setting = settings.MIDDLEWARE_CLASSES
if middleware not in middleware_setting:
if middleware not in settings.MIDDLEWARE:
raise ImproperlyConfigured("oidclogin view requires '%s' middleware installed" % middleware)
return RedirectView.as_view(url=reverse("home/index"), permanent=False)(request)
6 changes: 1 addition & 5 deletions kobo/hub/xmlrpc/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from kobo.hub.models import Worker
from kobo.django.auth.krb5 import Krb5RemoteUserBackend
from kobo.django.xmlrpc.auth import *
from kobo.django.django_version import django_version_ge


__all__ = (
Expand All @@ -36,10 +35,7 @@ def login_worker_key(request, worker_key):

username = "worker/%s" % worker.name
backend = Krb5RemoteUserBackend()
if django_version_ge('1.11.0'):
user = backend.authenticate(None, username)
else:
user = backend.authenticate(username)
user = backend.authenticate(None, username)
if user is None:
raise PermissionDenied()
user.backend = "%s.%s" % (backend.__module__, backend.__class__.__name__)
Expand Down
6 changes: 1 addition & 5 deletions kobo/hub/xmlrpc/client.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
# -*- coding: utf-8 -*-


from kobo.django.django_version import django_version_ge
if django_version_ge('1.10.0'):
from django.urls import reverse
else:
from django.core.urlresolvers import reverse
from django.core.exceptions import ObjectDoesNotExist
from django.urls import reverse

from kobo.hub import models
from kobo.django.xmlrpc.decorators import admin_required, login_required
Expand Down
11 changes: 2 additions & 9 deletions tests/test_xmlrpc_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

from kobo.hub.models import Worker
from kobo.hub.xmlrpc import auth
from kobo.django.django_version import django_version_ge

from .utils import DjangoRunner

Expand All @@ -34,10 +33,7 @@ def login(request, user):
session_key = auth.login_worker_key(req, 'key')

login_mock.assert_called_once_with(req, user)
if django_version_ge('1.11.0'):
krb_mock.authenticate.assert_called_once_with(None, 'worker/name')
else:
krb_mock.authenticate.assert_called_once_with('worker/name')
krb_mock.authenticate.assert_called_once_with(None, 'worker/name')
self.assertEqual(session_key, '1234567890')

def test_login_worker_key_valid_worker_invalid_user(self):
Expand All @@ -49,10 +45,7 @@ def test_login_worker_key_valid_worker_invalid_user(self):
with self.assertRaises(PermissionDenied):
auth.login_worker_key(req, 'key')

if django_version_ge('1.11.0'):
krb_mock.authenticate.assert_called_once_with(None, 'worker/name')
else:
krb_mock.authenticate.assert_called_once_with('worker/name')
krb_mock.authenticate.assert_called_once_with(None, 'worker/name')

def test_login_worker_key_invalid_worker(self):
req = Mock()
Expand Down

0 comments on commit ab2d638

Please sign in to comment.