Skip to content

Commit

Permalink
Merge pull request #756 from WhyNotHugo/drop-py2
Browse files Browse the repository at this point in the history
Drop a bunch of python2-specific code
  • Loading branch information
matthiask committed Aug 31, 2020
2 parents 18bba43 + 3b2e9b7 commit 98d85c5
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 37 deletions.
4 changes: 2 additions & 2 deletions docs/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The text used to indicate a tree level can by customised by providing a
``level_indicator`` argument::

category = TreeNodeChoiceField(queryset=Category.objects.all(),
level_indicator=u'+--')
level_indicator='+--')

...which for this example would result in a select with the following
options::
Expand Down Expand Up @@ -186,4 +186,4 @@ A sample view which shows basic usage of the form is provided below::
'category_tree': Category.objects.all(),
})

.. _`move_to method`: models.html#move-to-target-position-first-child
.. _`move_to method`: models.html#move-to-target-position-first-child
4 changes: 2 additions & 2 deletions docs/templates.rst
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ comma-separated list of feature names. The valid feature names are:
on the right::

Books -> []
Sci-fi -> [u'Books']
Dystopian Futures -> [u'Books', u'Sci-fi']
Sci-fi -> ['Books']
Dystopian Futures -> ['Books', 'Sci-fi']

Using this filter with unpacking in a ``{% for %}`` tag, you should have
enough information about the tree structure to create a hierarchical
Expand Down
8 changes: 4 additions & 4 deletions mptt/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def formfield_for_foreignkey(self, db_field, request, **kwargs):
required=False)
defaults.update(kwargs)
kwargs = defaults
return super(MPTTModelAdmin, self).formfield_for_foreignkey(
return super().formfield_for_foreignkey(
db_field, request, **kwargs)

def get_ordering(self, request):
Expand Down Expand Up @@ -91,7 +91,7 @@ def delete_selected_tree(self, modeladmin, request, queryset):
return delete_selected(self, request, queryset)

def get_actions(self, request):
actions = super(MPTTModelAdmin, self).get_actions(request)
actions = super().get_actions(request)
if actions is not None and 'delete_selected' in actions:
actions['delete_selected'] = (
self.delete_selected_tree,
Expand Down Expand Up @@ -145,7 +145,7 @@ def changelist_view(self, request, *args, **kwargs):
if request.is_ajax() and request.POST.get('cmd') == 'move_node':
return self._move_node(request)

response = super(DraggableMPTTAdmin, self).changelist_view(
response = super().changelist_view(
request, *args, **kwargs)

try:
Expand Down Expand Up @@ -298,7 +298,7 @@ def __init__(self, field, request, params, model, model_admin, field_path):
else:
self.rel_name = self.other_model._meta.pk.name
self.changed_lookup_kwarg = '%s__%s__inhierarchy' % (field_path, self.rel_name)
super(TreeRelatedFieldListFilter, self).__init__(field, request, params,
super().__init__(field, request, params,
model, model_admin, field_path)
self.lookup_val = request.GET.get(self.changed_lookup_kwarg)

Expand Down
6 changes: 3 additions & 3 deletions mptt/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ def formfield(self, **kwargs):
Use MPTT's ``TreeNodeChoiceField``
"""
kwargs.setdefault('form_class', TreeNodeChoiceField)
return super(TreeForeignKey, self).formfield(**kwargs)
return super().formfield(**kwargs)


class TreeOneToOneField(models.OneToOneField):
def formfield(self, **kwargs):
kwargs.setdefault('form_class', TreeNodeChoiceField)
return super(TreeOneToOneField, self).formfield(**kwargs)
return super().formfield(**kwargs)


class TreeManyToManyField(models.ManyToManyField):
def formfield(self, **kwargs):
kwargs.setdefault('form_class', TreeNodeMultipleChoiceField)
return super(TreeManyToManyField, self).formfield(**kwargs)
return super().formfield(**kwargs)
10 changes: 5 additions & 5 deletions mptt/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, queryset, *args, **kwargs):
mptt_opts = queryset.model._mptt_meta
queryset = queryset.order_by(mptt_opts.tree_id_attr, mptt_opts.left_attr)

super(TreeNodeChoiceFieldMixin, self).__init__(queryset, *args, **kwargs)
super().__init__(queryset, *args, **kwargs)

def _get_level_indicator(self, obj):
level = getattr(obj, obj._mptt_meta.level_attr)
Expand Down Expand Up @@ -68,7 +68,7 @@ class TreeNodePositionField(forms.ChoiceField):
def __init__(self, *args, **kwargs):
if 'choices' not in kwargs:
kwargs['choices'] = self.DEFAULT_CHOICES
super(TreeNodePositionField, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)


# Forms #######################################################################
Expand Down Expand Up @@ -117,7 +117,7 @@ def __init__(self, node, *args, **kwargs):
target_select_size = kwargs.pop('target_select_size', 10)
position_choices = kwargs.pop('position_choices', None)
level_indicator = kwargs.pop('level_indicator', None)
super(MoveNodeForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
opts = node._mptt_meta
if valid_targets is None:
valid_targets = node._tree_manager.exclude(**{
Expand Down Expand Up @@ -158,7 +158,7 @@ class MPTTAdminForm(forms.ModelForm):
"""

def __init__(self, *args, **kwargs):
super(MPTTAdminForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
if self.instance and self.instance.pk:
instance = self.instance
opts = self._meta.model._mptt_meta
Expand All @@ -173,7 +173,7 @@ def __init__(self, *args, **kwargs):
parent_field.queryset = parent_qs

def clean(self):
cleaned_data = super(MPTTAdminForm, self).clean()
cleaned_data = super().clean()
opts = self._meta.model._mptt_meta
parent = cleaned_data.get(opts.parent_attr)
if self.instance and parent:
Expand Down
4 changes: 2 additions & 2 deletions mptt/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class TreeManager(models.Manager.from_queryset(TreeQuerySet)):
"""

def contribute_to_class(self, model, name):
super(TreeManager, self).contribute_to_class(model, name)
super().contribute_to_class(model, name)

if not model._meta.abstract:
self.tree_model = _get_tree_model(model)
Expand All @@ -57,7 +57,7 @@ def get_queryset(self, *args, **kwargs):
"""
Ensures that this manager always returns nodes in tree order.
"""
return super(TreeManager, self).get_queryset(
return super().get_queryset(
*args, **kwargs
).order_by(
self.tree_id_attr, self.left_attr
Expand Down
20 changes: 10 additions & 10 deletions mptt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def __set__(self, cls, owner, value):

class classpropertytype(property):
def __init__(self, name, bases=(), members={}):
return super(classpropertytype, self).__init__(
return super().__init__(
members.get('__get__'),
members.get('__set__'),
members.get('__delete__'),
Expand Down Expand Up @@ -125,7 +125,7 @@ def update_mptt_cached_fields(self, instance):
so that the MPTT fields need to be updated.
"""
instance._mptt_cached_fields = {}
field_names = set((self.parent_attr,))
field_names = {self.parent_attr}
if self.order_insertion_by:
for f in self.order_insertion_by:
if f[0] == '-':
Expand Down Expand Up @@ -242,7 +242,7 @@ def __new__(meta, class_name, bases, class_dict):
- adds a TreeManager to the model
"""
if class_name == 'NewBase' and class_dict == {}:
return super(MPTTModelBase, meta).__new__(meta, class_name, bases, class_dict)
return super().__new__(meta, class_name, bases, class_dict)
is_MPTTModel = False
try:
MPTTModel
Expand All @@ -266,7 +266,7 @@ class MPTTMeta:
setattr(MPTTMeta, name, value)

class_dict['_mptt_meta'] = MPTTOptions(MPTTMeta)
super_new = super(MPTTModelBase, meta).__new__
super_new = super().__new__
cls = super_new(meta, class_name, bases, class_dict)
cls = meta.register(cls)

Expand Down Expand Up @@ -407,7 +407,7 @@ class Meta:
objects = TreeManager()

def __init__(self, *args, **kwargs):
super(MPTTModel, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
self._mptt_meta.update_mptt_cached_fields(self)

def _mpttfield(self, fieldname):
Expand Down Expand Up @@ -470,8 +470,8 @@ def _mptt_track_tree_insertions(cls, tree_id, num_inserted):
if num_inserted < 0:
deleted = range(tree_id + num_inserted, -num_inserted)
changes.difference_update(deleted)
new_changes = set(
(t + num_inserted if t >= tree_id else t) for t in changes)
new_changes = {
(t + num_inserted if t >= tree_id else t) for t in changes}
cls._threadlocal.mptt_delayed_tree_changes = new_changes

@raise_if_unsaved
Expand Down Expand Up @@ -867,7 +867,7 @@ def save(self, *args, **kwargs):
setattr(self, opts.right_attr, 2)
setattr(self, opts.level_attr, 0)
setattr(self, opts.tree_id_attr, 0)
return super(MPTTModel, self).save(*args, **kwargs)
return super().save(*args, **kwargs)

parent_id = opts.get_raw_field_value(self, opts.parent_attr)

Expand Down Expand Up @@ -1010,7 +1010,7 @@ def save(self, *args, **kwargs):
# Default insertion
self.insert_at(parent, position='last-child', allow_existing_pk=True)
try:
super(MPTTModel, self).save(*args, **kwargs)
super().save(*args, **kwargs)
finally:
if collapse_old_tree is not None:
self._tree_manager._create_tree_space(collapse_old_tree, -1)
Expand Down Expand Up @@ -1049,7 +1049,7 @@ def delete(self, *args, **kwargs):
right_shift = -self.get_descendant_count() - 2
self._tree_manager._post_insert_update_cached_parent_right(parent, right_shift)

return super(MPTTModel, self).delete(*args, **kwargs)
return super().delete(*args, **kwargs)
delete.alters_data = True

def _mptt_refresh(self):
Expand Down
4 changes: 2 additions & 2 deletions mptt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def tree_item_iterator(items, ancestors=False, callback=str):
are given on the right::
Books -> []
Sci-fi -> [u'Books']
Dystopian Futures -> [u'Books', u'Sci-fi']
Sci-fi -> ['Books']
Dystopian Futures -> ['Books', 'Sci-fi']
You can overload the default representation by providing an
optional ``callback`` function which takes a single argument
Expand Down
3 changes: 0 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ files = setup.py mptt/__init__.py
exclude = venv,.tox,docs/conf.py
max-line-length = 99

[bdist_wheel]
universal = 1

[metadata]
license_file = LICENSE

4 changes: 2 additions & 2 deletions tests/myapp/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __str__(self):
return self.name

def delete(self):
super(Category, self).delete()
super().delete()
delete.alters_data = True


Expand Down Expand Up @@ -273,7 +273,7 @@ class SwappedInModel(MPTTModel):
# Default manager
class MultipleManager(TreeManager):
def get_queryset(self):
return super(MultipleManager, self).get_queryset().exclude(published=False)
return super().get_queryset().exclude(published=False)


class MultipleManagerModel(MPTTModel):
Expand Down
4 changes: 2 additions & 2 deletions tests/myapp/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,7 @@ class Meta:
# class does) could cause errors. Kind of... weird, but surprisingly
# effective.

SomeModel = type(str('model_{0}'.format(idx)), (MPTTModel,), {
SomeModel = type(str('model_{}'.format(idx)), (MPTTModel,), {
'Meta': Meta,
'__module__': __name__,
})
Expand All @@ -2491,7 +2491,7 @@ class Meta:
index_together = case
app_label = 'myapp'

SomeModel = type(str('model__different_attr_{0}'.format(idx)), (MPTTModel,), {
SomeModel = type(str('model__different_attr_{}'.format(idx)), (MPTTModel,), {
'MPTTMeta': MPTTMeta,
'Meta': Meta,
'__module__': str(__name__)
Expand Down

0 comments on commit 98d85c5

Please sign in to comment.