Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ManyToManyAdminField for use with ManyToManyField #43

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion easy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
38 changes: 37 additions & 1 deletion easy/admin/field.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you send me the warning message from django?

Copy link
Author

@Furkanzmc Furkanzmc Feb 22, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'ForeignKeyAdminField' object has no attribute '__name__'

When used like this

price_model_link = ForeignKeyAdminField('price_model')

readonly_fields = (
    price_model_link,
)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about put this way:

    def __name__(self):
        return self.__class__.__name__

It will work for all child classes with right name.


def render(self, obj):
return helper.call_or_get(obj, self.attr, self.default)

Expand Down Expand Up @@ -85,6 +88,39 @@ def render(self, obj):
return self.default


class ManyToManyAdminField(SimpleAdminField):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will improve the coverage by the way. I put it together after using it myself, but I haven't had time to improve the testing.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great

"""Renders a ManyToManyField as links."""

def __init__(self, attr, display=None, short_description=None, admin_order_field=None, default=None):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think will be great to add a template path. to help on customization from user.

then you check

if template_path:
    # use template
else:
    # use default way

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 = '<ul>'
objects = ref.get_queryset()
for obj in objects:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do think about using string template to replace this code?

list_str += '<li><a href="%s">%s</a></li>' % (
reverse(
admin_urlname(obj._meta, 'change'),
args=(obj.pk,)
),
conditional_escape(display or obj)
)
list_str += '</ul>'

return list_str


class LinkChangeListAdminField(BaseAdminField):

def __init__(self, app, model, attr, params=None, params_static=None, short_description=None, admin_order_field=None):
Expand Down Expand Up @@ -203,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
Expand Down
53 changes: 52 additions & 1 deletion easy/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -126,6 +126,57 @@ def test_foreignkey_display_sub_property(self):
self.assertTrue(custom_field.allow_tags)


class TestManyToManyAdminField(test.TestCase):
def test_manytomany(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.polls.add(poll_one)
poll_group.polls.add(poll_two)

custom_field = easy.ManyToManyAdminField('polls')
ret = custom_field(poll_group)
if django.VERSION < (1, 9):
expected = u'<ul><li><a href="/admin/test_app/poll/%s/">Poll object</a></li>' % (poll_one.id, )
expected += u'<li><a href="/admin/test_app/poll/%s/">Poll object</a></li></ul>' % (poll_two.id, )
elif django.VERSION < (2, 0):
expected = u'<ul><li><a href="/admin/test_app/poll/%s/change/">Poll object</a></li>' % (poll_one.id, )
expected += u'<li><a href="/admin/test_app/poll/%s/change/">Poll object</a></li></ul>' % (poll_two.id, )
else:
expected = u'<ul><li><a href="/admin/test_app/poll/%s/change/">Poll object (1)</a></li>' % (poll_one.id, )
expected += u'<li><a href="/admin/test_app/poll/%s/change/">Poll object (1)</a></li></ul>' % (poll_two.id, )

self.assertEqual(expected, ret)
self.assertTrue(custom_field.allow_tags)

def test_manytomany_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.polls.add(poll_one)
poll_group.polls.add(poll_two)

custom_field = easy.ManyToManyAdminField('polls', 'name')
ret = custom_field(poll_group)
if django.VERSION < (1, 9):
expected = u'<ul><li><a href="/admin/test_app/poll/%s/">%s</a></li>' % (poll_one.id, poll_group.name, )
expected += u'<li><a href="/admin/test_app/poll/%s/">%s</a></li></ul>' % (poll_two.id, poll_group.name, )
else:
expected = u'<ul><li><a href="/admin/test_app/poll/%s/change/">%s</a></li>' % (poll_one.id, poll_group.name, )
expected += u'<li><a href="/admin/test_app/poll/%s/change/">%s</a></li></ul>' % (poll_two.id, poll_group.name, )

self.assertEqual(expected, ret)
self.assertTrue(custom_field.allow_tags)


class TestTemplateAdminField(test.TestCase):

def test_template(self):
Expand Down
7 changes: 7 additions & 0 deletions test_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down