Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
[models] Updated sortedm2m __str__ definition #29
Browse files Browse the repository at this point in the history
Closes #29
  • Loading branch information
nemesifier committed Aug 8, 2017
1 parent 3dc8683 commit 54a68bd
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
23 changes: 13 additions & 10 deletions django_netjsonconfig/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ def key(self):
AbstractConfig._meta.get_field('config').blank = True


class TemplatesThrough(object):
"""
Improves string representation of m2m relationship objects
"""
def __str__(self):
return _('Relationship with {0}').format(self.template.name)


class TemplatesVpnMixin(models.Model):
"""
Provides a mixin that adds two m2m relationships:
Expand All @@ -122,6 +130,7 @@ class TemplatesVpnMixin(models.Model):
templates = SortedManyToManyField('django_netjsonconfig.Template',
related_name='config_relations',
verbose_name=_('templates'),
base_class=TemplatesThrough,
blank=True,
help_text=_('configuration templates, applied from '
'first to last'))
Expand Down Expand Up @@ -277,13 +286,7 @@ class Meta:
abstract = True


def sortedm2m__str__(self):
"""
Improves string representation of m2m relationship objects
TODO
----
this method can be removed if the following pull request
gets merged: https://github.com/gregmuellegger/django-sortedm2m/pull/101
"""
return _('Relationship with {0}').format(self.template.name)
# kept for backward compatibility to avoid
# breaking openwisp-controller 0.2.x
# TODO: remove in 0.7.x
sortedm2m__str__ = TemplatesThrough.__str__
5 changes: 1 addition & 4 deletions django_netjsonconfig/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .base.config import AbstractConfig, TemplatesVpnMixin, sortedm2m__str__
from .base.config import AbstractConfig, TemplatesVpnMixin
from .base.device import AbstractDevice
from .base.tag import AbstractTaggedTemplate, AbstractTemplateTag
from .base.template import AbstractTemplate
Expand All @@ -13,9 +13,6 @@ class Meta(AbstractConfig.Meta):
abstract = False


Config.templates.through.__str__ = sortedm2m__str__


class Device(AbstractDevice):
"""
Concrete device model
Expand Down
4 changes: 3 additions & 1 deletion django_netjsonconfig/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ def test_m2m_str_conversion(self):
c = self._create_config(device=self._create_device(name='test-m2m-str-repr'))
c.templates.add(t)
c.save()
self.assertIn('Relationship with', str(c.templates.through.objects.first()))
through = str(c.templates.through.objects.first())
self.assertIn('Relationship with', through)
self.assertIn(t.name, through)

def test_get_template_model_static(self):
self.assertIs(Config.get_template_model(), Template)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ django-model-utils
jsonfield
six
netjsonconfig>=0.5.4,<0.7.0
django-sortedm2m>=1.4.0,<1.5
django-sortedm2m>=1.5.0,<1.6
django-reversion>=2.0.6,<2.1
django-x509>=0.2.0,<0.3.0
django-taggit>=0.22.1,<0.23.0
Expand Down

0 comments on commit 54a68bd

Please sign in to comment.