From 73057042c752391652ef7a5d0a5440693d5430b3 Mon Sep 17 00:00:00 2001 From: Diego Forero Date: Wed, 15 Apr 2015 23:01:44 -0500 Subject: [PATCH] Fix compatibility with django 1.8, fix deprecation warnings --- mockups/generators.py | 7 ++- mockups/helpers.py | 79 +++++++++++++++++--------- mockups/management/commands/mockups.py | 23 ++++++-- setup.py | 2 +- 4 files changed, 78 insertions(+), 33 deletions(-) diff --git a/mockups/generators.py b/mockups/generators.py index 7ce6a2a..bc39273 100644 --- a/mockups/generators.py +++ b/mockups/generators.py @@ -148,8 +148,11 @@ def __init__(self, count=None, method=None, common=None, max_length=None, *args, super(LoremGenerator, self).__init__(*args, **kwargs) def generate(self): - from django.contrib.webdesign.lorem_ipsum import paragraphs, sentence, \ - words + try: + from django.utils.lorem_ipsum import paragraphs, sentence, words + except ImportError: + # Support django < 1.8 + from django.contrib.webdesign.lorem_ipsum import paragraphs, sentence, words if self.method == 'w': lorem = words(self.count, common=self.common) elif self.method == 's': diff --git a/mockups/helpers.py b/mockups/helpers.py index 4b3643c..1b2ac58 100644 --- a/mockups/helpers.py +++ b/mockups/helpers.py @@ -1,9 +1,22 @@ # -*- coding: utf-8 -*- import copy from django.conf import settings -from django.utils.importlib import import_module from django.utils.module_loading import module_has_submodule +# For django 1.8 and earlier +try: + from importlib import import_module +except ImportError: + from django.utils.importlib import import_module + +try: + from django.apps import apps + + get_model = apps.get_model +except ImportError: + # Support django < 1.8 + from django.db.models import get_model + __all__ = ('register', 'unregister', 'create', 'create_one', 'autodiscover') @@ -12,9 +25,9 @@ def get_mockup(model, *args, **kwargs): - ''' + """ Gets an mockup instance for a model - ''' + """ if model not in _registry: from mockups.base import Mockup cls = Mockup @@ -45,7 +58,7 @@ def register(model, mockup_cls, overwrite=False, fail_silently=False): ''' from django.db import models if isinstance(model, basestring): - model = models.get_model(*model.split('.', 1)) + model = get_model(*model.split('.', 1)) if not overwrite and model in _registry: if fail_silently: return @@ -65,9 +78,9 @@ def unregister(model_or_iterable, fail_silently=False): from django.db import models if not isinstance(model_or_iterable, (list, tuple, set)): model_or_iterable = [model_or_iterable] - for model in models: + for model in model_or_iterable: if isinstance(model, basestring): - model = models.get_model(*model.split('.', 1)) + model = get_model(*model.split('.', 1)) try: del _registry[model] except KeyError: @@ -98,9 +111,13 @@ def create(model, count, *args, **kwargs): :func:`create` will return a list of the created objects. ''' - from django.db import models + # try: + # from django.apps import apps + # get_model = apps.get_model + # except ImportError: + # from django.db.models import get_model if isinstance(model, basestring): - model = models.get_model(*model.split('.', 1)) + model = get_model(*model.split('.', 1)) mockup = get_mockup(model, *args, **kwargs) return mockup.create(count) @@ -125,22 +142,32 @@ def autodiscover(): global _registry - for app in settings.INSTALLED_APPS: - mod = import_module(app) - # Attempt to import the app's mockup module. - try: - before_import_registry = copy.copy(_registry) - import_module('%s.mockup' % app) - except: - # Reset the model registry to the state before the last import as - # this import will have to reoccur on the next request and this - # could raise NotRegistered and AlreadyRegistered exceptions - # (see #8245). - _registry = before_import_registry - - # Decide whether to bubble up this error. If the app just - # doesn't have an mockup module, we can ignore the error - # attempting to import it, otherwise we want it to bubble up. - if module_has_submodule(mod, 'mockup'): - raise + + import imp + try: + from django.apps import apps + + for app_config in apps.get_app_configs(): + _registry[app_config.name] = [app_config.path] + + except ImportError: + + for app in settings.INSTALLED_APPS: + mod = import_module(app) + # Attempt to import the app's mockup module. + try: + before_import_registry = copy.copy(_registry) + import_module('%s.mockup' % app) + except: + # Reset the model registry to the state before the last import as + # this import will have to reoccur on the next request and this + # could raise NotRegistered and AlreadyRegistered exceptions + # (see #8245). + _registry = before_import_registry + + # Decide whether to bubble up this error. If the app just + # doesn't have an mockup module, we can ignore the error + # attempting to import it, otherwise we want it to bubble up. + if module_has_submodule(mod, 'mockup'): + raise diff --git a/mockups/management/commands/mockups.py b/mockups/management/commands/mockups.py index 33d25bd..bbbe934 100644 --- a/mockups/management/commands/mockups.py +++ b/mockups/management/commands/mockups.py @@ -27,12 +27,22 @@ ''' from django.core.management.base import BaseCommand, CommandError from django.db import models -from django.db.transaction import commit_on_success -from django.utils.importlib import import_module from ...helpers import create, autodiscover from ... import signals from optparse import make_option +# For django 1.8 and earlier +try: + from django.db.transaction import atomic +except ImportError: + from django.db.transaction import commit_on_success as atomic + +# For django 1.8 and earlier +try: + from importlib import import_module +except ImportError: + from django.utils.importlib import import_module + class Command(BaseCommand): help = ( @@ -114,9 +124,14 @@ def print_instance(self, sender, model, instance, **kwargs): obj.pk, self.format_output(obj)) - @commit_on_success + @atomic def handle(self, *attrs, **options): - from django.db.models import get_model + try: + from django.apps import apps + get_model = apps.get_model + except ImportError: + # Support django < 1.8 + from django.db.models import get_model error_option = None # diff --git a/setup.py b/setup.py index b364ab9..43486c4 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ setup( name = 'django-mockups', - version = '0.4.8', + version = '0.4.9', description = 'Provides tools to auto generate content.', long_description = open('README.rst').read(), author = 'Mikko Hellsing',