Skip to content
This repository has been archived by the owner on Sep 12, 2018. It is now read-only.

Commit

Permalink
Upgrade to Django 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed May 17, 2018
1 parent c81972e commit 8e211dd
Show file tree
Hide file tree
Showing 15 changed files with 54 additions and 48 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ build/
*.egg-info
datalayer/
.cache/
.pytest_cache/
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
test:
py.test tests
py.test -vx tests
initassets:
git submodule update --init
assets:
Expand Down
9 changes: 5 additions & 4 deletions leaflet_storage/decorators.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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.')
Expand Down
2 changes: 1 addition & 1 deletion leaflet_storage/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
2 changes: 1 addition & 1 deletion leaflet_storage/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions leaflet_storage/managers.py
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
6 changes: 3 additions & 3 deletions leaflet_storage/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',),
Expand Down Expand Up @@ -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),
),
]
14 changes: 7 additions & 7 deletions leaflet_storage/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -174,15 +174,15 @@ 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()
msg = _("Your anonymous map has been attached to your account %s" % user)
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
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 7 additions & 7 deletions leaflet_storage/utils.py
Original file line number Diff line number Diff line change
@@ -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=""):
Expand Down Expand Up @@ -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)
Expand All @@ -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", [])
Expand Down
14 changes: 7 additions & 7 deletions leaflet_storage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 15 additions & 11 deletions tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -30,16 +30,20 @@ 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')

# @classmethod
# def _prepare(cls, create, **kwargs):
# password = kwargs.pop('password', None)
# user = super(UserFactory, cls)._prepare(create, **kwargs)
# if password:
# pouet
# print(password)
# user.set_password(password)
# print(user.password)
# if create:
# user.save()
# return user

class Meta:
model = User
Expand Down
2 changes: 1 addition & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion tests/test_datalayer_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_map.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_map_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 8e211dd

Please sign in to comment.