Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with django 1.8, fix deprecation warnings #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions mockups/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
79 changes: 53 additions & 26 deletions mockups/helpers.py
Original file line number Diff line number Diff line change
@@ -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')

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand All @@ -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

23 changes: 19 additions & 4 deletions mockups/management/commands/mockups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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
#
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down