Skip to content

Commit 7621d70

Browse files
committed
Support for Django 1.10. New meta API for related_manager will be implemented in Django 1.11, in the meantime go with a workaround and silence deprecation warning.
1 parent 9e143b8 commit 7621d70

9 files changed

Lines changed: 64 additions & 29 deletions

File tree

.travis.yml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ python:
77
env:
88
- DJANGO=django==1.7.11 DRF=3.3.1 DATABASE_URL=mysql://root@localhost/test
99
- DJANGO=django==1.7.11 DRF=3.3.1 DATABASE_URL=postgres://postgres@localhost/test
10-
- DJANGO=django==1.8.9 DRF=3.3.1 DATABASE_URL=mysql://root@localhost/test
11-
- DJANGO=django==1.8.9 DRF=3.3.1 DATABASE_URL=postgres://postgres@localhost/test
12-
- DJANGO=django==1.9.2 DRF=3.3.1 DATABASE_URL=mysql://root@localhost/test
13-
- DJANGO=django==1.9.2 DRF=3.3.1 DATABASE_URL=postgres://postgres@localhost/test
10+
- DJANGO=django==1.8.13 DRF=3.3.1 DATABASE_URL=mysql://root@localhost/test
11+
- DJANGO=django==1.8.13 DRF=3.3.1 DATABASE_URL=postgres://postgres@localhost/test
12+
- DJANGO=django==1.9.7 DRF=3.3.1 DATABASE_URL=mysql://root@localhost/test
13+
- DJANGO=django==1.9.7 DRF=3.3.1 DATABASE_URL=postgres://postgres@localhost/test
14+
- DJANGO=https://github.com/django/django/archive/stable/1.10.x.tar.gz DRF=3.3.3 DATABASE_URL=mysql://root@localhost/test
15+
- DJANGO=https://github.com/django/django/archive/stable/1.10.x.tar.gz DRF=3.3.3 DATABASE_URL=postgres://postgres@localhost/test
1416

1517
sudo: false
1618
install:
@@ -28,9 +30,13 @@ after_success:
2830
matrix:
2931
exclude:
3032
- python: 3.3
31-
env: DJANGO=django==1.9.2 DRF=3.3.1 DATABASE_URL=mysql://root@localhost/test
33+
env: DJANGO=django==1.9.7 DRF=3.3.1 DATABASE_URL=mysql://root@localhost/test
3234
- python: 3.3
33-
env: DJANGO=django==1.9.2 DRF=3.3.1 DATABASE_URL=postgres://postgres@localhost/test
35+
env: DJANGO=django==1.9.7 DRF=3.3.1 DATABASE_URL=postgres://postgres@localhost/test
36+
- python: 3.3
37+
env: DJANGO=https://github.com/django/django/archive/stable/1.10.x.tar.gz DRF=3.3.3 DATABASE_URL=mysql://root@localhost/test
38+
- python: 3.3
39+
env: DJANGO=https://github.com/django/django/archive/stable/1.10.x.tar.gz DRF=3.3.3 DATABASE_URL=postgres://postgres@localhost/test
3440
- python: 3.5
3541
env: DJANGO=django==1.7.11 DRF=3.3.1 DATABASE_URL=mysql://root@localhost/test
3642
- python: 3.5

hvad/admin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def get_language_name(language_code):
3636
class InlineModelForm(TranslatableModelForm):
3737
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
3838
initial=None, error_class=ErrorList, label_suffix=':',
39-
empty_permitted=False, instance=None):
39+
empty_permitted=False, instance=None, **kwargs):
4040
"""
4141
4242
"""
@@ -56,7 +56,7 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
5656
super(TranslatableModelForm, self).__init__(data, files, auto_id,
5757
prefix, object_data,
5858
error_class, label_suffix,
59-
empty_permitted, instance)
59+
empty_permitted, instance, **kwargs)
6060

6161

6262
class TranslatableModelAdminMixin(object):

hvad/forms.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ class BaseTranslatableModelForm(BaseModelForm):
104104
'''
105105
def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
106106
initial=None, error_class=ErrorList, label_suffix=':',
107-
empty_permitted=False, instance=None):
107+
empty_permitted=False, instance=None, **kwargs):
108108

109109
# Insert values of instance's translated fields into 'initial' dict
110110
object_data = {}
@@ -123,7 +123,7 @@ def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
123123

124124
super(BaseTranslatableModelForm, self).__init__(
125125
data, files, auto_id, prefix, object_data,
126-
error_class, label_suffix, empty_permitted, instance
126+
error_class, label_suffix, empty_permitted, instance, **kwargs
127127
)
128128

129129
def clean(self):

hvad/manager.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ def __init__(self, sql, aliases):
138138
self.sql = sql
139139
self.aliases = aliases
140140

141-
def as_sql(self, qn, connection):
142-
aliases = tuple(qn(alias) for alias in self.aliases)
141+
def as_sql(self, compiler, connection):
142+
aliases = tuple(compiler.quote_name_unless_alias(alias) for alias in self.aliases)
143143
return (self.sql % aliases, [])
144144

145145
class BetterTranslationsField(object):
@@ -1026,6 +1026,7 @@ class TranslationManager(models.Manager):
10261026
# API
10271027
#===========================================================================
10281028
use_for_related_fields = True
1029+
silence_use_for_related_fields_deprecation = True # Django 1.10
10291030

10301031
queryset_class = TranslationQueryset
10311032
fallback_class = FallbackQueryset

hvad/models.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,12 @@ class TranslatableModel(models.Model):
231231
"""
232232
# change the default manager to the translation manager
233233
objects = TranslationManager()
234+
_plain_manager = models.Manager()
234235

235236
class Meta:
236237
abstract = True
238+
if django.VERSION >= (1, 10):
239+
base_manager_name = '_plain_manager'
237240

238241
def __init__(self, *args, **kwargs):
239242
# Split arguments into shared/translatd
@@ -474,6 +477,7 @@ def prepare_translatable_model(sender, **kwargs):
474477
# translation aware.
475478
base_mgr = getattr(model, '_base_manager', None)
476479
if base_mgr is None or isinstance(base_mgr, TranslationManager):
480+
assert django.VERSION < (1, 10)
477481
model.add_to_class('_base_manager', Manager())
478482

479483
# Replace get_field_by_name with one that warns for common mistakes

hvad/test_utils/cli.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,22 @@ def configure(**extra):
3030
ADMIN_MEDIA_PREFIX = '/static/admin/',
3131
EMAIL_BACKEND = 'django.core.mail.backends.locmem.EmailBackend',
3232
SECRET_KEY = 'key',
33-
TEMPLATE_LOADERS = (
33+
TEMPLATE_LOADERS = ( # Remove when dropping support for Django 1.7
3434
'django.template.loaders.filesystem.Loader',
3535
'django.template.loaders.app_directories.Loader',
36-
'django.template.loaders.eggs.Loader',
3736
),
38-
TEMPLATE_DIRS = [
37+
TEMPLATE_DIRS = [ # Remove when dropping support for Django 1.7
3938
os.path.abspath(os.path.join(os.path.dirname(__file__), 'project', 'templates'))
4039
],
40+
TEMPLATES = [
41+
{
42+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
43+
'DIRS': [
44+
os.path.abspath(os.path.join(os.path.dirname(__file__), 'project', 'templates'))
45+
],
46+
'APP_DIRS': True,
47+
},
48+
],
4149
MIDDLEWARE_CLASSES = [
4250
'django.contrib.sessions.middleware.SessionMiddleware',
4351
'django.contrib.auth.middleware.AuthenticationMiddleware',

hvad/tests/basic.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -639,12 +639,15 @@ def test_translated_foreignkey_set(self):
639639
self.assertEqual(getattr(related, cache).__dict__['translated_id'], 4242)
640640

641641
def test_translated_attribute_delete(self):
642-
# Its not possible to delete the charfield, which should result in an AttributeError
642+
# Accessing a deleted translated attribute reloads the translation
643643
obj = Normal.objects.language("en").create(shared_field="test", translated_field="en")
644644
obj.save()
645645
self.assertEqual(obj.translated_field, "en")
646646
delattr(obj, 'translated_field')
647-
self.assertRaises(AttributeError, getattr, obj, 'translated_field')
647+
if django.VERSION >= (1, 10): # on version 1.10 and newer, this refreshes from db
648+
self.assertEqual(obj.translated_field, "en")
649+
else:
650+
self.assertRaises(AttributeError, getattr, obj, 'translated_field')
648651

649652
def test_languagecodeattribute(self):
650653
# Its not possible to set/delete a language code

hvad/tests/related.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# -*- coding: utf-8 -*-
22
import django
33
from django.core.exceptions import FieldError
4-
from django.db import connection, models, IntegrityError
4+
from django.db import connection, models, IntegrityError, transaction
55
from django.db.models.query_utils import Q
6+
from django.test.testcases import TransactionTestCase
67
from django.utils import translation
78
from hvad.exceptions import WrongManager
89
from hvad.models import (TranslatedFields, TranslatableModel)
@@ -32,22 +33,33 @@ def test_relation(self):
3233
self.assertEqual(related.normal.translated_field, normal.translated_field)
3334
self.assertTrue(related in normal.rel1.all())
3435

35-
def test_failed_relation(self):
36-
related = Related.objects.create()
37-
related.normal_id = 999
38-
if connection.features.supports_forward_references:
39-
related.save()
40-
self.assertRaises(Normal.DoesNotExist, getattr, related, 'normal')
41-
else:
42-
self.assertRaises(IntegrityError, related.save)
43-
4436
def test_reverse_relation(self):
4537
normal = Normal.objects.language('en').get(pk=self.normal_id[1])
4638
related = Related.objects.language('en').create(normal=normal)
4739

4840
self.assertEqual(normal.rel1.language('en').get().pk, related.pk)
4941

5042

43+
class NormalToNormalFKTest2(TransactionTestCase, NormalFixture):
44+
normal_count = 1
45+
46+
def test_failed_relation(self):
47+
related = Related.objects.create()
48+
related.normal_id = 999
49+
if connection.features.supports_foreign_keys:
50+
if connection.features.supports_forward_references:
51+
try:
52+
transaction.set_autocommit(False)
53+
related.save()
54+
self.assertRaises(Normal.DoesNotExist, getattr, related, 'normal')
55+
self.assertRaises(IntegrityError, transaction.commit)
56+
finally:
57+
transaction.rollback()
58+
transaction.set_autocommit(True)
59+
else:
60+
self.assertRaises(IntegrityError, related.save)
61+
62+
5163
class StandardToTransFKTest(HvadTestCase, StandardFixture, NormalFixture):
5264
normal_count = 2
5365
standard_count = 2

hvad/tests/views.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# -*- coding: utf-8 -*-
2+
import django
23
from django import forms
34
from django.http import Http404
45
from django.utils import translation
@@ -15,12 +16,12 @@
1516

1617
class TestCreateView(TranslatableCreateView):
1718
model = Normal
18-
success_url = '%(id)s'
19+
success_url = '{id}' if django.VERSION >= (1, 8) else '%(id)s'
1920

2021
class TestUpdateView(TranslatableUpdateView):
2122
model = Normal
2223
slug_field = 'shared_field'
23-
success_url = '%(id)s'
24+
success_url = '{id}' if django.VERSION >= (1, 8) else '%(id)s'
2425

2526
class TestDeleteView(TranslatableDeleteView):
2627
model = Normal

0 commit comments

Comments
 (0)