Skip to content

Commit

Permalink
Merge pull request django-extensions#1243 from michael-k/drop-1.8
Browse files Browse the repository at this point in the history
Drop compat code for Django < 1.11 and add classifier for Django 2.1
  • Loading branch information
trbs authored Aug 21, 2018
2 parents 11860c7 + 21d2f08 commit b288d4c
Show file tree
Hide file tree
Showing 16 changed files with 32 additions and 353 deletions.
8 changes: 2 additions & 6 deletions django_extensions/admin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,10 @@ def formfield_for_dbfield(self, db_field, **kwargs):
specified in the related_search_fields class attribute.
"""
if isinstance(db_field, models.ForeignKey) and db_field.name in self.related_search_fields:
# TODO: Remove me after Django 1.8 is unsupported
remote_field = db_field.remote_field if hasattr(db_field, 'remote_field') else db_field.rel
remote_field_model = remote_field.model if hasattr(remote_field, 'model') else remote_field.to

help_text = self.get_help_text(db_field.name, remote_field_model._meta.object_name)
help_text = self.get_help_text(db_field.name, db_field.remote_field.model._meta.object_name)
if kwargs.get('help_text'):
help_text = six.u('%s %s' % (kwargs['help_text'], help_text))
kwargs['widget'] = ForeignKeySearchInput(remote_field, self.related_search_fields[db_field.name])
kwargs['widget'] = ForeignKeySearchInput(db_field.remote_field, self.related_search_fields[db_field.name])
kwargs['help_text'] = help_text
return super(ForeignKeyAutocompleteAdminMixin, self).formfield_for_dbfield(db_field, **kwargs)

Expand Down
31 changes: 9 additions & 22 deletions django_extensions/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import six
import codecs
import importlib
import django

from django.conf import settings

Expand All @@ -20,35 +19,23 @@ def load_tag_library(libname):
Returns None if the library isn't loaded.
"""
if django.VERSION < (1, 9):
from django.template.base import get_library, InvalidTemplateLibrary
try:
lib = get_library(libname)
return lib
except InvalidTemplateLibrary:
return None
else:
from django.template.backends.django import get_installed_libraries
from django.template.library import InvalidTemplateLibrary
try:
lib = get_installed_libraries()[libname]
lib = importlib.import_module(lib).register
return lib
except (InvalidTemplateLibrary, KeyError):
return None
from django.template.backends.django import get_installed_libraries
from django.template.library import InvalidTemplateLibrary
try:
lib = get_installed_libraries()[libname]
lib = importlib.import_module(lib).register
return lib
except (InvalidTemplateLibrary, KeyError):
return None


def get_template_setting(template_key, default=None):
""" Read template settings pre and post django 1.8 """
""" Read template settings """
templates_var = getattr(settings, 'TEMPLATES', None)
if templates_var:
for tdict in templates_var:
if template_key in tdict:
return tdict[template_key]
if template_key == 'DIRS':
pre18_template_key = 'TEMPLATES_%s' % template_key
value = getattr(settings, pre18_template_key, default)
return value
return default


Expand Down
8 changes: 2 additions & 6 deletions django_extensions/management/commands/admin_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ def name(self):
def _process_many_to_many(self, meta):
raw_id_threshold = self.raw_id_threshold
for field in meta.local_many_to_many:
if hasattr(field, 'remote_field'): # Django>=1.9
if hasattr(field, 'remote_field'):
related_model = getattr(field.remote_field, 'related_model', field.remote_field.model)
elif hasattr(field, 'related'): # Django<1.9
related_model = getattr(field.related, 'related_model', field.related.model)
else:
raise CommandError("Unable to process ManyToMany relation")
related_objects = related_model.objects.all()
Expand All @@ -186,10 +184,8 @@ def _process_foreign_key(self, field):
raw_id_threshold = self.raw_id_threshold
list_filter_threshold = self.list_filter_threshold
max_count = max(list_filter_threshold, raw_id_threshold)
if hasattr(field, 'remote_field'): # Django>=1.9
if hasattr(field, 'remote_field'):
related_model = getattr(field.remote_field, 'related_model', field.remote_field.model)
elif hasattr(field, 'related'): # Django<1.9
related_model = getattr(field.related, 'related_model', field.related.model)
else:
raise CommandError("Unable to process ForeignKey relation")
related_count = related_model.objects.all()
Expand Down
160 changes: 0 additions & 160 deletions django_extensions/management/commands/create_app.py

This file was deleted.

18 changes: 6 additions & 12 deletions django_extensions/management/commands/dumpscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,9 +668,7 @@ def get_attribute_value(item, field, context, force=False, skip_autofield=True):
# content types in this script, as they can be generated again
# automatically.
# NB: Not sure if "is" will always work
remote_field = field.remote_field if hasattr(field, 'remote_field') else field.rel # Remove me after Django 1.8 is unsupported
remote_field_model = remote_field.model if hasattr(remote_field, 'model') else remote_field.to # Remove me after Django 1.8 is unsupported
if remote_field_model is ContentType:
if field.remote_field.model is ContentType:
return 'ContentType.objects.get(app_label="%s", model="%s")' % (value.app_label, value.model)

# Generate an identifier (key) for this foreign object
Expand Down Expand Up @@ -717,21 +715,17 @@ def check_dependencies(model, model_queue, avaliable_models):
# For each ForeignKey or ManyToMany field, check that a link is possible

for field in model._meta.fields:
remote_field = field.remote_field if hasattr(field, 'remote_field') else field.rel # Remove me after Django 1.8 is unsupported
if not remote_field:
if not field.remote_field:
continue
remote_field_model = remote_field.model if hasattr(remote_field, 'model') else remote_field.to # Remove me after Django 1.8 is unsupported
if remote_field_model.__name__ not in allowed_links:
if remote_field_model not in avaliable_models:
if field.remote_field.model.__name__ not in allowed_links:
if field.remote_field.model not in avaliable_models:
continue
return False

for field in model._meta.many_to_many:
remote_field = field.remote_field if hasattr(field, 'remote_field') else field.rel # Remove me after Django 1.8 is unsupported
if not remote_field:
if not field.remote_field:
continue
remote_field_model = remote_field.model if hasattr(remote_field, 'model') else remote_field.to # Remove me after Django 1.8 is unsupported
if remote_field_model.__name__ not in allowed_links:
if field.remote_field.model.__name__ not in allowed_links:
return False

return True
Expand Down
13 changes: 1 addition & 12 deletions django_extensions/management/commands/reset_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""
import logging

import django
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from six.moves import input
Expand Down Expand Up @@ -135,10 +134,7 @@ def handle(self, *args, **options):
connection.query(create_query)

elif engine in ('postgresql', 'postgresql_psycopg2', 'postgis'):
if engine == 'postgresql' and django.VERSION < (1, 9):
import psycopg as Database # NOQA
elif engine in ('postgresql', 'postgresql_psycopg2', 'postgis'):
import psycopg2 as Database # NOQA
import psycopg2 as Database # NOQA

conn_params = {'database': 'template1'}
if user:
Expand Down Expand Up @@ -178,13 +174,6 @@ def handle(self, *args, **options):
create_query += " WITH OWNER = \"%s\" " % owner
create_query += " ENCODING = 'UTF8'"

if engine == 'postgis' and django.VERSION < (1, 9):
# For PostGIS 1.5, fetch template name if it exists
from django.contrib.gis.db.backends.postgis.base import DatabaseWrapper
postgis_template = DatabaseWrapper(dbinfo).template_postgis
if postgis_template is not None:
create_query += ' TEMPLATE = %s' % postgis_template

if settings.DEFAULT_TABLESPACE:
create_query += ' TABLESPACE = %s;' % settings.DEFAULT_TABLESPACE
else:
Expand Down
14 changes: 0 additions & 14 deletions django_extensions/management/commands/runserver_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand, CommandError
from django.core.servers.basehttp import get_internal_wsgi_application
from django.db import DEFAULT_DB_ALIAS, connections
from django.db.backends import utils
from django.db.migrations.executor import MigrationExecutor
from django.utils.autoreload import gen_filenames

from django_extensions.management.technical_response import \
Expand Down Expand Up @@ -416,18 +414,6 @@ def determine_ssl_files_paths(cls, options):
key_file = cls._determine_path_for_file(options['key_file_path'], options['cert_path'], "key")
return cert_file, key_file

if django.VERSION[:2] <= (1, 9):
def check_migrations(self):
"""
Checks to see if the set of migrations on disk matches the
migrations in the database. Prints a warning if they don't match.
"""
executor = MigrationExecutor(connections[DEFAULT_DB_ALIAS])
plan = executor.migration_plan(executor.loader.graph.leaf_nodes())
if plan and self.show_startup_messages:
self.stdout.write(self.style.NOTICE("\nYou have unapplied migrations; your app may not work properly until they are applied."))
self.stdout.write(self.style.NOTICE("Run 'python manage.py migrate' to apply them.\n"))


def set_werkzeug_log_color():
"""Try to set color to the werkzeug log.
Expand Down
7 changes: 2 additions & 5 deletions django_extensions/management/commands/sqldiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,8 @@ def find_field_missing_in_db(self, fieldmap, table_description, table_name):
if field_name not in db_fields:
field_output = []

# TODO: Remove 'field.rel' after dropping Django 1.8 support
remote_field = field.remote_field if hasattr(field, 'remote_field') else field.rel
if remote_field:
remote_field_model = remote_field.model if hasattr(remote_field, 'model') else remote_field.to
field_output.extend([remote_field_model._meta.db_table, remote_field_model._meta.get_field(remote_field.field_name).column])
if field.remote_field:
field_output.extend([field.remote_field.model._meta.db_table, field.remote_field.model._meta.get_field(field.remote_field.field_name).column])
op = 'fkey-missing-in-db'
else:
op = 'field-missing-in-db'
Expand Down
Loading

0 comments on commit b288d4c

Please sign in to comment.