Skip to content

Commit

Permalink
Merge pull request #4454 from jayvdb/2021-01-dj3-phase3
Browse files Browse the repository at this point in the history
Django 2+
  • Loading branch information
gannetson authored Feb 10, 2021
2 parents db4df52 + 30df610 commit c35fe3e
Show file tree
Hide file tree
Showing 32 changed files with 104 additions and 48 deletions.
3 changes: 2 additions & 1 deletion bluebottle/activities/documents.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from builtins import str
from builtins import object
from django_elasticsearch_dsl import DocType, fields

from bluebottle.initiatives.documents import DocType, fields

from bluebottle.utils.documents import MultiTenantIndex
from bluebottle.activities.models import Activity
Expand Down
13 changes: 7 additions & 6 deletions bluebottle/auth/middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from builtins import object
import json
import logging
from calendar import timegm
Expand All @@ -12,6 +11,7 @@
from django.http.request import RawPostDataException
from django.shortcuts import render_to_response
from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin
from lockdown import settings as lockdown_settings
from lockdown.middleware import (LockdownMiddleware as BaseLockdownMiddleware,
compile_url_exceptions, get_lockdown_form)
Expand All @@ -32,7 +32,7 @@ def isAdminRequest(request):
return request.path.startswith('/downloads') or base_path in ['jet', 'admin', 'jet-dashboard']


class UserJwtTokenMiddleware(object):
class UserJwtTokenMiddleware(MiddlewareMixin):
"""
Custom middleware to set the User on the request when using
Jwt Token authentication.
Expand All @@ -41,7 +41,8 @@ class UserJwtTokenMiddleware(object):
def process_request(self, request):
""" Override only the request to add the user """
try:
return request.user
request.user
return
except AttributeError:
pass

Expand All @@ -64,7 +65,7 @@ def process_request(self, request):
return


class SlidingJwtTokenMiddleware(object):
class SlidingJwtTokenMiddleware(MiddlewareMixin):
"""
Custom middleware to set a sliding window for the jwt auth token expiration.
"""
Expand Down Expand Up @@ -177,7 +178,7 @@ def process_request(self, request):
super(AdminOnlyAuthenticationMiddleware, self).process_request(request)


class AdminOnlyCsrf(object):
class AdminOnlyCsrf(MiddlewareMixin):
"""
Disable csrf for non-Admin requests, eg API
"""
Expand Down Expand Up @@ -280,7 +281,7 @@ def process_request(self, request):
authorization_logger = logging.getLogger(__name__)


class LogAuthFailureMiddleWare(object):
class LogAuthFailureMiddleWare(MiddlewareMixin):
def process_request(self, request):
# TODO: Handle this more cleanly. The exception is raised when using IE11.
# Possibly related to the following issue:
Expand Down
5 changes: 4 additions & 1 deletion bluebottle/bb_accounts/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
from rest_framework.permissions import IsAuthenticated
from rest_framework.exceptions import PermissionDenied, NotAuthenticated, ValidationError

from rest_framework_jwt.views import ObtainJSONWebToken
try:
from rest_framework_jwt.views import ObtainJSONWebToken
except ImportError:
from rest_framework_jwt.views import ObtainJSONWebTokenView as ObtainJSONWebToken

from rest_framework_json_api.views import AutoPrefetchMixin

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "admin:admin/change_list.html" %}
{% load i18n admin_static admin_list %}
{% load i18n static admin_list %}
{% load admin_urls %}
{% load humanize %}
{% load admin_extra %}
Expand Down
5 changes: 3 additions & 2 deletions bluebottle/bluebottle_drf2/middleware.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from builtins import object
from django.utils.deprecation import MiddlewareMixin

METHOD_OVERRIDE_HEADER = 'HTTP_X_HTTP_METHOD_OVERRIDE'


class MethodOverrideMiddleware(object):
class MethodOverrideMiddleware(MiddlewareMixin):
def process_view(self, request, callback, callback_args, callback_kwargs):
if request.method != 'POST':
return
Expand Down
6 changes: 5 additions & 1 deletion bluebottle/bluebottle_drf2/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
)
from rest_framework.settings import api_settings

from rest_framework_json_api.compat import collections_abc
try:
import collections.abc as collections_abc
except ImportError:
import collections as collections_abc

from rest_framework_json_api.relations import HyperlinkedMixin, ResourceRelatedField, SkipDataMixin
from rest_framework_json_api.renderers import JSONRenderer
from rest_framework_json_api import utils
Expand Down
4 changes: 2 additions & 2 deletions bluebottle/clients/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
from django.conf import settings
from django.utils._os import safe_join

from tenant_schemas.postgresql_backend.base import FakeTenant


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -35,6 +33,8 @@ def set_tenant(self, tenant):
self.tenant_properties)

except (ImportError, AttributeError, IOError):
from tenant_schemas.postgresql_backend.base import FakeTenant

if not isinstance(tenant, FakeTenant):
logger.debug('No tenant properties found for: {0}'.format(tenant.client_name))

Expand Down
4 changes: 2 additions & 2 deletions bluebottle/clients/middleware.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from future import standard_library
standard_library.install_aliases()
from builtins import object
from urllib.parse import urljoin

from django.db import connection
from django.conf import settings
from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin


class MediaMiddleware(object):
class MediaMiddleware(MiddlewareMixin):
def process_response(self, request, response):
if (
response.status_code == 404 and
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% load i18n admin_urls admin_static django_template_additions %}
{% load i18n admin_urls static django_template_additions %}
<div class="js-inline-admin-formset sortable sortable-ui inline-group{% if recursive_formset %} {{ recursive_formset.formset.prefix|default:"Root" }}-nested-inline {% if prev_prefix %} {{ prev_prefix }}-{{ loopCounter }}-nested-inline{% endif %} nested-inline{% endif %}" id="{{ inline_admin_formset.formset.prefix }}-group">
{% with recursive_formset=inline_admin_formset stacked_template='admin/edit_inline/stacked-nested.html' tabular_template='admin/edit_inline/tabular-nested.html'%}
<h2>{{ recursive_formset.opts.verbose_name_plural|title }}</h2>
Expand Down
5 changes: 3 additions & 2 deletions bluebottle/common/admin_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ def label_for_value(self, value):


class ImprovedModelForm(admin.ModelAdmin):
def formfield_for_dbfield(self, db_field, **kwargs):
def formfield_for_dbfield(self, db_field, request=None, **kwargs):
if db_field.name in self.raw_id_fields:
kwargs.pop("request", None)
type = db_field.rel.__class__.__name__
if type == "ManyToOneRel":
kwargs['widget'] = VerboseForeignKeyRawIdWidget(db_field.rel,
Expand All @@ -69,5 +68,7 @@ def formfield_for_dbfield(self, db_field, **kwargs):
kwargs['widget'] = VerboseManyToManyRawIdWidget(db_field.rel,
site)
return db_field.formfield(**kwargs)
if request:
kwargs["request"] = request
return super(ImprovedModelForm, self).formfield_for_dbfield(db_field,
**kwargs)
2 changes: 1 addition & 1 deletion bluebottle/exports/templates/exportdb/base.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "admin/base_site.html" %}{% load i18n admin_static %}
{% extends "admin/base_site.html" %}{% load i18n static %}

{% block extrahead %}{{ block.super }}
{% if jquery_in_vendor %}
Expand Down
2 changes: 1 addition & 1 deletion bluebottle/exports/templates/exportdb/in_progress.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% extends "exportdb/base.html" %}{% load i18n admin_static %}
{% extends "exportdb/base.html" %}{% load i18n static %}

{% block extrahead %}{{ block.super }}
<style type="text/css">
Expand Down
5 changes: 4 additions & 1 deletion bluebottle/files/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
from bluebottle.files.serializers import FileSerializer, ImageSerializer, PrivateFileSerializer
from bluebottle.utils.views import CreateAPIView, RetrieveAPIView

mime = magic.Magic(mime=True)
try:
mime = magic.Magic(mime=True)
except TypeError:
mime = magic.Magic()


class FileList(AutoPrefetchMixin, CreateAPIView):
Expand Down
5 changes: 3 additions & 2 deletions bluebottle/fsm/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ class StateWidget(forms.TextInput):

class StateMachineModelFormMetaClass(ModelFormMetaclass):
def __new__(cls, name, bases, attrs):
if 'Meta' in attrs:
for field, machine in list(attrs['Meta'].model._state_machines.items()):
model = getattr(attrs.get('Meta', None), 'model', None)
if hasattr(model, '_state_machines'):
for field, machine in list(model._state_machines.items()):
attrs[field] = forms.ChoiceField(
required=False,
widget=TransitionSelectWidget()
Expand Down
4 changes: 2 additions & 2 deletions bluebottle/funding/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class PayoutInline(StateMachineAdminMixin, admin.TabularInline):
extra = 0
can_delete = False

def has_add_permission(self, request):
def has_add_permission(self, request, obj=None):
return False

def payout_link(self, obj):
Expand Down Expand Up @@ -642,7 +642,7 @@ class DonorInline(PaymentLinkMixin, admin.TabularInline):
extra = 0
can_delete = False

def has_add_permission(self, request):
def has_add_permission(self, request, obj=None):
return False


Expand Down
2 changes: 2 additions & 0 deletions bluebottle/funding/documents.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Meta(object):
model = Funding
related_models = (Initiative, Member, Donor)

Django = Meta

def get_instances_from_related(self, related_instance):
if isinstance(related_instance, Initiative):
return Funding.objects.filter(initiative=related_instance)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "admin/change_form.html" %}
{% load i18n admin_static admin_list admin_urls %}
{% load i18n static admin_list admin_urls %}

{% block object-tools-items %}
{% if original.can_update %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "admin/change_form.html" %}
{% load i18n admin_static admin_list admin_urls %}
{% load i18n static admin_list admin_urls %}

{% block object-tools-items %}
<li>
Expand Down
9 changes: 8 additions & 1 deletion bluebottle/initiatives/documents.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
from builtins import object
from django_elasticsearch_dsl import DocType, fields

try:
from django_elasticsearch_dsl.documents import DocType
except ImportError:
from django_elasticsearch_dsl import DocType
from django_elasticsearch_dsl import fields

from bluebottle.time_based.models import PeriodActivity, DateActivity
from bluebottle.utils.documents import MultiTenantIndex
Expand Down Expand Up @@ -95,6 +100,8 @@ class Meta(object):
DateActivity
)

Django = Meta

def get_queryset(self):
return super(InitiativeDocument, self).get_queryset().select_related(
'theme', 'place', 'owner', 'promoter',
Expand Down
8 changes: 6 additions & 2 deletions bluebottle/members/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from django import forms
from django.conf.urls import url
from django.contrib import admin
from django.contrib.admin.sites import NotRegistered
from django.contrib.auth.admin import UserAdmin, GroupAdmin
from django.contrib.auth.models import Group, Permission
from django.contrib.auth.tokens import default_token_generator
Expand Down Expand Up @@ -203,7 +204,7 @@ class UserActivityInline(admin.TabularInline):

formset = LimitModelFormset

def has_add_permission(self, request):
def has_add_permission(self, request, obj=None):
return False


Expand Down Expand Up @@ -577,5 +578,8 @@ class TokenAdmin(admin.ModelAdmin):
fields = ('user', 'key')


admin.site.unregister(Token)
try:
admin.site.unregister(Token)
except NotRegistered:
pass
admin.site.register(Token, TokenAdmin)
6 changes: 5 additions & 1 deletion bluebottle/members/serializers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from builtins import object
from axes.attempts import is_already_locked
from django import forms
from django.conf import settings
from django.contrib.auth import get_user_model, password_validation, authenticate
Expand All @@ -23,6 +22,11 @@
from bluebottle.tasks.models import Skill
from bluebottle.utils.serializers import PermissionField, TruncatedCharField, CaptchaField

try:
from axes.attempts import is_already_locked
except ImportError:
is_already_locked = lambda request: request.axes_locked_out

BB_USER_MODEL = get_user_model()


Expand Down
2 changes: 1 addition & 1 deletion bluebottle/notifications/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MessageAdminInline(GenericTabularInline):
readonly_fields = ['sent', 'subject', 'recipient']
fields = readonly_fields

def has_add_permission(self, request):
def has_add_permission(self, request, obj=None):
return False

extra = 0
Expand Down
4 changes: 2 additions & 2 deletions bluebottle/redirects/middleware.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
from __future__ import unicode_literals

from builtins import object
import regex

from django import http
from django.conf import settings
from django.db import connection
from django.utils.deprecation import MiddlewareMixin

from bluebottle.redirects.models import Redirect
from bluebottle.clients import properties


class RedirectFallbackMiddleware(object):
class RedirectFallbackMiddleware(MiddlewareMixin):
"""
A modified version of django.contrib.redirects, this app allows
us to optionally redirect users using regular expressions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% extends "admin/change_form.html" %}
{% load i18n admin_static admin_list admin_urls %}
{% load i18n static admin_list admin_urls %}

{% block object-tools-items %}
<li>
Expand Down
4 changes: 2 additions & 2 deletions bluebottle/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
'tenant_extras.template_loaders.FilesystemLoader',
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
'django.template.loaders.eggs.Loader',
# 'django.template.loaders.eggs.Loader',
'admin_tools.template_loaders.Loader',
],
'context_processors': [
Expand Down Expand Up @@ -149,7 +149,7 @@
'bluebottle.clients.middleware.MediaMiddleware',
'tenant_extras.middleware.TenantLocaleMiddleware',
'bluebottle.redirects.middleware.RedirectFallbackMiddleware',
'bluebottle.auth.middleware.UserJwtTokenMiddleware',
'bluebottle.auth.middleware.UserJwtTokenMiddleware', # 'Member' object has no attribute 'has_header'
'bluebottle.utils.middleware.SubDomainSessionMiddleware',
'bluebottle.utils.middleware.APILanguageMiddleware',
'bluebottle.auth.middleware.AdminOnlySessionMiddleware',
Expand Down
Loading

0 comments on commit c35fe3e

Please sign in to comment.