From 9e8ac9587bdd2991043fb33c8917ea7ea28d04bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20U=CC=88zu=CC=88mcu=CC=88?= Date: Wed, 21 Feb 2018 17:33:01 -0500 Subject: [PATCH 1/8] Add a __name__ function --- easy/admin/field.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/easy/admin/field.py b/easy/admin/field.py index f04a251..08d52cf 100644 --- a/easy/admin/field.py +++ b/easy/admin/field.py @@ -46,6 +46,9 @@ def __init__(self, attr, short_description=None, admin_order_field=None, allow_t super(SimpleAdminField, self).__init__(short_description, admin_order_field, allow_tags) + def __name__(self): + return 'SimpleAdminField' + def render(self, obj): return helper.call_or_get(obj, self.attr, self.default) From 0f1964bd22db1104f259f56d15ff42831b5d0f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20U=CC=88zu=CC=88mcu=CC=88?= Date: Wed, 21 Feb 2018 17:33:12 -0500 Subject: [PATCH 2/8] Add many to many field --- easy/admin/field.py | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/easy/admin/field.py b/easy/admin/field.py index 08d52cf..c0dd957 100644 --- a/easy/admin/field.py +++ b/easy/admin/field.py @@ -88,6 +88,39 @@ def render(self, obj): return self.default +class ManyToManyAdminField(SimpleAdminField): + """Renders a ManyToManyField as links.""" + + def __init__(self, attr, display=None, short_description=None, admin_order_field=None, default=None): + self.display = display + super(ManyToManyAdminField, self).__init__(attr, short_description, admin_order_field, True, default) + + def __name__(self): + return 'ManyToManyAdminField' + + def render(self, obj): + ref = getattr(obj, self.attr, None) + display = None + if self.display: + display = helper.call_or_get(obj, self.display, self.default) + + list_str = display + if hasattr(ref, 'get_queryset'): + list_str = '' + + return list_str + + class LinkChangeListAdminField(BaseAdminField): def __init__(self, app, model, attr, params=None, params_static=None, short_description=None, admin_order_field=None): @@ -206,7 +239,7 @@ def render(self, obj): class CacheAdminField(SimpleAdminField): - def __init__(self, attr, django_filter, load=None, extra=None, short_description=None, + def __init__(self, attr, django_filter, load=None, extra=None, short_description=None, admin_order_field=None, allow_tags=False, default=None): self.filter = django_filter self.load = load From a2d178a44dba62e7280ef9915f45b3fb7f7b8565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20U=CC=88zu=CC=88mcu=CC=88?= Date: Wed, 21 Feb 2018 18:39:26 -0500 Subject: [PATCH 3/8] Add tests class ManyToManyAdminField --- easy/tests.py | 53 +++++++++++++++++++++++++++++++++++++++++++++- test_app/models.py | 7 ++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/easy/tests.py b/easy/tests.py index c64afac..a6d7dbf 100644 --- a/easy/tests.py +++ b/easy/tests.py @@ -18,7 +18,7 @@ import easy from easy.helper import Nothing from test_app.admin import PollAdmin -from test_app.models import Question, Poll +from test_app.models import Question, Poll, PollGroup class TestSimpleAdminField(test.TestCase): @@ -126,6 +126,57 @@ def test_foreignkey_display_sub_property(self): self.assertTrue(custom_field.allow_tags) +class TestManyToManyAdminField(test.TestCase): + def test_foreignkey(self): + poll_group = mommy.make( + PollGroup, + name='Test Poll Group' + ) + + poll_one = mommy.make(Poll, name='Test Poll #1') + poll_two = mommy.make(Poll, name='Test Poll #2') + poll_group.add(poll_one) + poll_group.add(poll_two) + + custom_field = easy.ManyToManyAdminField('polls') + ret = custom_field(poll_group) + if django.VERSION < (1, 9): + expected = u'
  • Poll object
  • ' % (poll_one.id, ) + expected += u'
  • Poll object
  • ' % (poll_two.id, ) + elif django.VERSION < (2, 0): + expected = u'
    • Poll object
    • ' % (poll_one.id, ) + expected += u'
    • Poll object
    • ' % (poll_two.id, ) + else: + expected = u'
      • Poll object (1)
      • ' % (poll_one.id, ) + expected += u'
      • Poll object (1)
      • ' % (poll_two.id, ) + + self.assertEqual(expected, ret) + self.assertTrue(custom_field.allow_tags) + + def test_foreignkey_display(self): + poll_group = mommy.make( + PollGroup, + name='Test Poll Group' + ) + + poll_one = mommy.make(Poll, name='Test Poll #1') + poll_two = mommy.make(Poll, name='Test Poll #2') + poll_group.add(poll_one) + poll_group.add(poll_two) + + custom_field = easy.ManyToManyAdminField('polls', 'name') + ret = custom_field(poll_group) + if django.VERSION < (1, 9): + expected = u'
        • %s
        • ' % (poll_one.id, poll_group.name, ) + expected += u'
        • %s
        ' % (poll_two.id, poll_group.name, ) + else: + expected = u'
        • %s
        • ' % (poll_one.id, poll_group.name, ) + expected += u'
        • %s
        ' % (poll_two.id, poll_group.name, ) + + self.assertEqual(expected, ret) + self.assertTrue(custom_field.allow_tags) + + class TestTemplateAdminField(test.TestCase): def test_template(self): diff --git a/test_app/models.py b/test_app/models.py index 185301f..c24c429 100644 --- a/test_app/models.py +++ b/test_app/models.py @@ -6,6 +6,13 @@ class Poll(models.Model): name = models.CharField(max_length=200) +class PollGroup(models.Model): + """Add this model to test the ManyToManyAdminField.""" + + name = models.CharField(max_length=200) + polls = models.ManyToManyField(Poll, related_name='poll_groups') + + class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') From 125298582978eeb0439c316fb040ed56f2c8ad58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20U=CC=88zu=CC=88mcu=CC=88?= Date: Wed, 21 Feb 2018 19:06:44 -0500 Subject: [PATCH 4/8] Use the correct attribute for the add method --- easy/tests.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/easy/tests.py b/easy/tests.py index a6d7dbf..fca58a7 100644 --- a/easy/tests.py +++ b/easy/tests.py @@ -135,8 +135,8 @@ def test_foreignkey(self): poll_one = mommy.make(Poll, name='Test Poll #1') poll_two = mommy.make(Poll, name='Test Poll #2') - poll_group.add(poll_one) - poll_group.add(poll_two) + poll_group.polls.add(poll_one) + poll_group.polls.add(poll_two) custom_field = easy.ManyToManyAdminField('polls') ret = custom_field(poll_group) @@ -161,8 +161,8 @@ def test_foreignkey_display(self): poll_one = mommy.make(Poll, name='Test Poll #1') poll_two = mommy.make(Poll, name='Test Poll #2') - poll_group.add(poll_one) - poll_group.add(poll_two) + poll_group.polls.add(poll_one) + poll_group.polls.add(poll_two) custom_field = easy.ManyToManyAdminField('polls', 'name') ret = custom_field(poll_group) From 9a9e4489282ecc0714c60cf4d4f9cb633a9979d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20U=CC=88zu=CC=88mcu=CC=88?= Date: Wed, 21 Feb 2018 19:11:20 -0500 Subject: [PATCH 5/8] Add ForeignKeyAdminField to the import --- easy/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easy/__init__.py b/easy/__init__.py index d01c375..49bc271 100644 --- a/easy/__init__.py +++ b/easy/__init__.py @@ -4,7 +4,7 @@ ) from .admin import * # noqa from .admin.field import ( # noqa - BaseAdminField, BooleanAdminField, ExternalLinkAdminField, ForeignKeyAdminField, ImageAdminField, + BaseAdminField, BooleanAdminField, ExternalLinkAdminField, ForeignKeyAdminField, ManyToManyAdminField, ImageAdminField, LinkChangeListAdminField, SimpleAdminField, TemplateAdminField, ModelImageField, FilterAdminField, CacheAdminField, FormatAdminField ) From 6037158d271ef98fc7fbacdc863d207a3c7c8888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20U=CC=88zu=CC=88mcu=CC=88?= Date: Wed, 21 Feb 2018 19:19:19 -0500 Subject: [PATCH 6/8] Add the missing forward slash --- easy/admin/field.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/easy/admin/field.py b/easy/admin/field.py index c0dd957..209dd30 100644 --- a/easy/admin/field.py +++ b/easy/admin/field.py @@ -109,7 +109,7 @@ def render(self, obj): list_str = '
          ' objects = ref.get_queryset() for obj in objects: - list_str += '
        • %s
        • ' % ( + list_str += '
        • %s
        • ' % ( reverse( admin_urlname(obj._meta, 'change'), args=(obj.pk,) From 1f79cbfef6b1734da87903efd2d98b8f58e64954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20U=CC=88zu=CC=88mcu=CC=88?= Date: Wed, 21 Feb 2018 19:19:26 -0500 Subject: [PATCH 7/8] Change test method names --- easy/tests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/easy/tests.py b/easy/tests.py index fca58a7..e50e99d 100644 --- a/easy/tests.py +++ b/easy/tests.py @@ -127,7 +127,7 @@ def test_foreignkey_display_sub_property(self): class TestManyToManyAdminField(test.TestCase): - def test_foreignkey(self): + def test_manytomany(self): poll_group = mommy.make( PollGroup, name='Test Poll Group' @@ -153,7 +153,7 @@ def test_foreignkey(self): self.assertEqual(expected, ret) self.assertTrue(custom_field.allow_tags) - def test_foreignkey_display(self): + def test_manytomany_display(self): poll_group = mommy.make( PollGroup, name='Test Poll Group' From 3185241117968e72fabc066fca1fead52929cc7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Furkan=20U=CC=88zu=CC=88mcu=CC=88?= Date: Wed, 21 Feb 2018 19:27:06 -0500 Subject: [PATCH 8/8] Add the missing end tags for ul --- easy/tests.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/easy/tests.py b/easy/tests.py index e50e99d..9505ccb 100644 --- a/easy/tests.py +++ b/easy/tests.py @@ -142,13 +142,13 @@ def test_manytomany(self): ret = custom_field(poll_group) if django.VERSION < (1, 9): expected = u'' % (poll_two.id, ) elif django.VERSION < (2, 0): expected = u'' % (poll_two.id, ) else: expected = u'' % (poll_two.id, ) self.assertEqual(expected, ret) self.assertTrue(custom_field.allow_tags)