From a02e174334063f0f9c43ec71a643b1189effa046 Mon Sep 17 00:00:00 2001 From: Yohan Boniface Date: Thu, 17 May 2018 22:50:51 +0200 Subject: [PATCH] Upgrade to Django 2.0 --- .gitignore | 1 + Makefile | 2 +- leaflet_storage/decorators.py | 9 +++++---- leaflet_storage/fields.py | 2 +- leaflet_storage/forms.py | 2 +- leaflet_storage/managers.py | 4 ++-- leaflet_storage/migrations/0001_initial.py | 6 +++--- leaflet_storage/models.py | 14 +++++++------- leaflet_storage/utils.py | 14 +++++++------- leaflet_storage/views.py | 14 +++++++------- tests/base.py | 13 ++----------- tests/settings.py | 2 +- tests/test_datalayer_views.py | 2 +- tests/test_map.py | 2 +- tests/test_map_views.py | 2 +- 15 files changed, 41 insertions(+), 48 deletions(-) diff --git a/.gitignore b/.gitignore index 6c79c45..32a31db 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ build/ *.egg-info datalayer/ .cache/ +.pytest_cache/ diff --git a/Makefile b/Makefile index fc8fefc..e6a385e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ test: - py.test tests + py.test -vx tests initassets: git submodule update --init assets: diff --git a/leaflet_storage/decorators.py b/leaflet_storage/decorators.py index a4b17a6..a6f2713 100644 --- a/leaflet_storage/decorators.py +++ b/leaflet_storage/decorators.py @@ -1,6 +1,6 @@ from functools import wraps -from django.core.urlresolvers import reverse_lazy +from django.urls import reverse_lazy from django.shortcuts import get_object_or_404 from django.http import HttpResponseForbidden from django.conf import settings @@ -10,14 +10,15 @@ LOGIN_URL = getattr(settings, "LOGIN_URL", "login") -LOGIN_URL = reverse_lazy(LOGIN_URL) if not LOGIN_URL.startswith("/") else LOGIN_URL +LOGIN_URL = (reverse_lazy(LOGIN_URL) if not LOGIN_URL.startswith("/") + else LOGIN_URL) def login_required_if_not_anonymous_allowed(view_func): @wraps(view_func) def wrapper(request, *args, **kwargs): if (not getattr(settings, "LEAFLET_STORAGE_ALLOW_ANONYMOUS", False) - and not request.user.is_authenticated()): + and not request.user.is_authenticated): return simple_json_response(login_required=str(LOGIN_URL)) return view_func(request, *args, **kwargs) return wrapper @@ -35,7 +36,7 @@ def wrapper(request, *args, **kwargs): if map_inst.edit_status >= map_inst.EDITORS: can_edit = map_inst.can_edit(user=user, request=request) if not can_edit: - if map_inst.owner and not user.is_authenticated(): + if map_inst.owner and not user.is_authenticated: return simple_json_response(login_required=str(LOGIN_URL)) else: return HttpResponseForbidden('Action not allowed for user.') diff --git a/leaflet_storage/fields.py b/leaflet_storage/fields.py index cdef163..f60a62f 100644 --- a/leaflet_storage/fields.py +++ b/leaflet_storage/fields.py @@ -30,4 +30,4 @@ def to_python(self, value): def value_to_string(self, obj): """Return value from object converted to string properly""" - return smart_text(self.get_prep_value(self._get_val_from_obj(obj))) + return smart_text(self.get_prep_value(self.value_from_object(obj))) diff --git a/leaflet_storage/forms.py b/leaflet_storage/forms.py index 2711fe8..136cbba 100644 --- a/leaflet_storage/forms.py +++ b/leaflet_storage/forms.py @@ -48,7 +48,7 @@ def __init__(self, *args, **kwargs): (Map.OWNER, _('Only editable with secret edit link')) ) - edit_status = forms.ChoiceField(STATUS) + edit_status = forms.ChoiceField(choices=STATUS) class Meta: model = Map diff --git a/leaflet_storage/managers.py b/leaflet_storage/managers.py index c4933b4..d172889 100644 --- a/leaflet_storage/managers.py +++ b/leaflet_storage/managers.py @@ -1,7 +1,7 @@ -from django.contrib.gis.db import models +from django.db.models import Manager -class PublicManager(models.GeoManager): +class PublicManager(Manager): def get_queryset(self): return super(PublicManager, self).get_queryset().filter( diff --git a/leaflet_storage/migrations/0001_initial.py b/leaflet_storage/migrations/0001_initial.py index e91b09b..4fcb466 100644 --- a/leaflet_storage/migrations/0001_initial.py +++ b/leaflet_storage/migrations/0001_initial.py @@ -58,7 +58,7 @@ class Migration(migrations.Migration): ('settings', leaflet_storage.fields.DictField(null=True, verbose_name='settings', blank=True)), ('editors', models.ManyToManyField(to=settings.AUTH_USER_MODEL, verbose_name='editors', blank=True)), ('licence', models.ForeignKey(on_delete=django.db.models.deletion.SET_DEFAULT, default=leaflet_storage.models.get_default_licence, verbose_name='licence', to='leaflet_storage.Licence', help_text='Choose the map licence.')), - ('owner', models.ForeignKey(related_name='owned_maps', verbose_name='owner', blank=True, to=settings.AUTH_USER_MODEL, null=True)), + ('owner', models.ForeignKey(related_name='owned_maps', verbose_name='owner', blank=True, to=settings.AUTH_USER_MODEL, null=True, on_delete=django.db.models.deletion.PROTECT)), ], options={ 'ordering': ('name',), @@ -96,11 +96,11 @@ class Migration(migrations.Migration): migrations.AddField( model_name='map', name='tilelayer', - field=models.ForeignKey(related_name='maps', verbose_name='background', blank=True, to='leaflet_storage.TileLayer', null=True), + field=models.ForeignKey(related_name='maps', verbose_name='background', blank=True, to='leaflet_storage.TileLayer', null=True, on_delete=django.db.models.deletion.SET_DEFAULT), ), migrations.AddField( model_name='datalayer', name='map', - field=models.ForeignKey(to='leaflet_storage.Map'), + field=models.ForeignKey(to='leaflet_storage.Map', on_delete=django.db.models.deletion.CASCADE), ), ] diff --git a/leaflet_storage/models.py b/leaflet_storage/models.py index 8e6b19f..b290982 100644 --- a/leaflet_storage/models.py +++ b/leaflet_storage/models.py @@ -5,7 +5,7 @@ from django.contrib.gis.db import models from django.conf import settings -from django.core.urlresolvers import reverse +from django.urls import reverse from django.utils.translation import ugettext_lazy as _ from django.core.signing import Signer from django.contrib import messages @@ -135,14 +135,14 @@ class Map(NamedModel): default=get_default_licence ) modified_at = models.DateTimeField(auto_now=True) - tilelayer = models.ForeignKey(TileLayer, blank=True, null=True, related_name="maps", verbose_name=_("background")) - owner = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name="owned_maps", verbose_name=_("owner")) + tilelayer = models.ForeignKey(TileLayer, blank=True, null=True, related_name="maps", verbose_name=_("background"), on_delete=models.PROTECT) + owner = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True, related_name="owned_maps", verbose_name=_("owner"), on_delete=models.PROTECT) editors = models.ManyToManyField(settings.AUTH_USER_MODEL, blank=True, verbose_name=_("editors")) edit_status = models.SmallIntegerField(choices=EDIT_STATUS, default=OWNER, verbose_name=_("edit status")) share_status = models.SmallIntegerField(choices=SHARE_STATUS, default=PUBLIC, verbose_name=_("share status")) settings = DictField(blank=True, null=True, verbose_name=_("settings")) - objects = models.GeoManager() + objects = models.Manager() public = PublicManager() def get_absolute_url(self): @@ -174,7 +174,7 @@ def can_edit(self, user=None, request=None): if (getattr(settings, "LEAFLET_STORAGE_ALLOW_ANONYMOUS", False) and self.is_anonymous_owner(request)): can = True - if user and user.is_authenticated(): + if user and user.is_authenticated: # if user is authenticated, attach as owner self.owner = user self.save() @@ -182,7 +182,7 @@ def can_edit(self, user=None, request=None): messages.info(request, msg) if self.edit_status == self.ANONYMOUS: can = True - elif not user.is_authenticated(): + elif not user.is_authenticated: pass elif user == self.owner: can = True @@ -253,7 +253,7 @@ class DataLayer(NamedModel): """ Layer to store Features in. """ - map = models.ForeignKey(Map) + map = models.ForeignKey(Map, on_delete=models.CASCADE) description = models.TextField( blank=True, null=True, diff --git a/leaflet_storage/utils.py b/leaflet_storage/utils.py index 60b7878..57d4ce3 100644 --- a/leaflet_storage/utils.py +++ b/leaflet_storage/utils.py @@ -1,7 +1,7 @@ import gzip -from django.core.urlresolvers import get_resolver -from django.core.urlresolvers import RegexURLPattern, RegexURLResolver +from django.urls import get_resolver +from django.urls import URLPattern, URLResolver def get_uri_template(urlname, args=None, prefix=""): @@ -59,10 +59,10 @@ def _convert(template, args=None): return None -class DecoratedURLPattern(RegexURLPattern): +class DecoratedURLPattern(URLPattern): def resolve(self, *args, **kwargs): - result = RegexURLPattern.resolve(self, *args, **kwargs) + result = URLPattern.resolve(self, *args, **kwargs) if result: for func in self._decorate_with: result.func = func(result.func) @@ -84,14 +84,14 @@ def decorated_patterns(func, *urls): def decorate(urls, func): for url in urls: - if isinstance(url, RegexURLPattern): + if isinstance(url, URLPattern): url.__class__ = DecoratedURLPattern if not hasattr(url, "_decorate_with"): setattr(url, "_decorate_with", []) url._decorate_with.append(func) - elif isinstance(url, RegexURLResolver): + elif isinstance(url, URLResolver): for pp in url.url_patterns: - if isinstance(pp, RegexURLPattern): + if isinstance(pp, URLPattern): pp.__class__ = DecoratedURLPattern if not hasattr(pp, "_decorate_with"): setattr(pp, "_decorate_with", []) diff --git a/leaflet_storage/views.py b/leaflet_storage/views.py index 2a21079..6d612c0 100644 --- a/leaflet_storage/views.py +++ b/leaflet_storage/views.py @@ -9,7 +9,7 @@ from django.contrib.auth import logout as do_logout from django.contrib.auth import get_user_model from django.core.signing import Signer, BadSignature -from django.core.urlresolvers import reverse_lazy, reverse +from django.urls import reverse_lazy, reverse from django.http import (HttpResponse, HttpResponseForbidden, HttpResponseRedirect, HttpResponsePermanentRedirect) from django.shortcuts import get_object_or_404 @@ -222,10 +222,10 @@ class MapCreate(FormLessEditMixin, CreateView): form_class = MapSettingsForm def form_valid(self, form): - if self.request.user.is_authenticated(): + if self.request.user.is_authenticated: form.instance.owner = self.request.user self.object = form.save() - if not self.request.user.is_authenticated(): + if not self.request.user.is_authenticated: anonymous_url = "%s%s" % ( settings.SITE_URL, self.object.get_anonymous_edit_url() @@ -242,7 +242,7 @@ def form_valid(self, form): url=self.object.get_absolute_url(), info=msg ) - if not self.request.user.is_authenticated(): + if not self.request.user.is_authenticated: key, value = self.object.signed_cookie_elements response.set_signed_cookie( key=key, @@ -317,12 +317,12 @@ class MapClone(View): def post(self, *args, **kwargs): if not getattr(settings, "LEAFLET_STORAGE_ALLOW_ANONYMOUS", False) \ - and not self.request.user.is_authenticated(): + and not self.request.user.is_authenticated: return HttpResponseForbidden('Forbidden') - owner = self.request.user if self.request.user.is_authenticated() else None + owner = self.request.user if self.request.user.is_authenticated else None self.object = kwargs['map_inst'].clone(owner=owner) response = simple_json_response(redirect=self.object.get_absolute_url()) - if not self.request.user.is_authenticated(): + if not self.request.user.is_authenticated: key, value = self.object.signed_cookie_elements response.set_signed_cookie( key=key, diff --git a/tests/base.py b/tests/base.py index 2d10615..adff517 100644 --- a/tests/base.py +++ b/tests/base.py @@ -2,7 +2,7 @@ import factory from django.contrib.auth import get_user_model -from django.core.urlresolvers import reverse +from django.urls import reverse from leaflet_storage.forms import DEFAULT_CENTER from leaflet_storage.models import DataLayer, Licence, Map, TileLayer @@ -30,16 +30,7 @@ class UserFactory(factory.DjangoModelFactory): username = 'Joe' email = factory.LazyAttribute( lambda a: '{0}@example.com'.format(a.username).lower()) - - @classmethod - def _prepare(cls, create, **kwargs): - password = kwargs.pop('password', None) - user = super(UserFactory, cls)._prepare(create, **kwargs) - if password: - user.set_password(password) - if create: - user.save() - return user + password = factory.PostGenerationMethodCall('set_password', '123123') class Meta: model = User diff --git a/tests/settings.py b/tests/settings.py index 4c59a65..9bea483 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -16,7 +16,7 @@ } } ROOT_URLCONF = 'leaflet_storage.urls' -MIDDLEWARE_CLASSES = ( +MIDDLEWARE = ( 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.middleware.common.CommonMiddleware', diff --git a/tests/test_datalayer_views.py b/tests/test_datalayer_views.py index 15ffaa6..10035a7 100644 --- a/tests/test_datalayer_views.py +++ b/tests/test_datalayer_views.py @@ -2,7 +2,7 @@ import pytest from django.core.files.base import ContentFile -from django.core.urlresolvers import reverse +from django.urls import reverse from leaflet_storage.models import DataLayer, Map diff --git a/tests/test_map.py b/tests/test_map.py index 88023c5..297d2e1 100644 --- a/tests/test_map.py +++ b/tests/test_map.py @@ -1,6 +1,6 @@ import pytest from django.contrib.auth.models import AnonymousUser -from django.core.urlresolvers import reverse +from django.urls import reverse from leaflet_storage.models import Map diff --git a/tests/test_map_views.py b/tests/test_map_views.py index 7670650..d50fc40 100644 --- a/tests/test_map_views.py +++ b/tests/test_map_views.py @@ -4,7 +4,7 @@ import pytest from django.contrib.auth import get_user_model -from django.core.urlresolvers import reverse +from django.urls import reverse from leaflet_storage.models import DataLayer, Map