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

[admin] Conflicting approach in extending DeviceAdmin #202

Closed
nemesifier opened this issue Aug 20, 2020 · 0 comments · Fixed by #237
Closed

[admin] Conflicting approach in extending DeviceAdmin #202

nemesifier opened this issue Aug 20, 2020 · 0 comments · Fixed by #237
Labels
bug Something isn't working Hacktoberfest Easy issues for attracting Hacktoberfest participants.
Milestone

Comments

@nemesifier
Copy link
Member

I noticed we use 2 different approaches in extending DeviceAdmin at the same time:

we create a new class, unregister the admin from openwisp-controller and register the extended class in openwisp-monitoring, but then we also monkeypatch the class we just defined.

def device_admin_get_inlines(self, request, obj):
# copy the list to avoid modifying the original data structure
inlines = list(super(DeviceAdmin, self).get_inlines(request, obj))
if not obj or obj._state.adding:
inlines.remove(MetricInline)
return inlines
return inlines
DeviceAdmin.inlines += [CheckInline, MetricInline]
# This attribute needs to be set for nested inline
for inline in DeviceAdmin.inlines:
if not hasattr(inline, 'sortable_options'):
inline.sortable_options = {'disabled': True}
DeviceAdmin.get_inlines = device_admin_get_inlines
DeviceAdmin.Media.js += MetricAdmin.Media.js + (
'monitoring/js/percircle.js',
'monitoring/js/alert-settings.js',
)
DeviceAdmin.Media.css['all'] += (
'monitoring/css/percircle.css',
) + MetricAdmin.Media.css['all']
DeviceAdmin.list_display.insert(
DeviceAdmin.list_display.index('config_status'), 'health_status'
)
DeviceAdmin.list_select_related += ('monitoring',)
DeviceAdmin.list_filter.insert(
0, 'monitoring__status',
)
DeviceAdmin.fields.insert(DeviceAdmin.fields.index('last_ip'), 'health_status')
DeviceAdmin.readonly_fields.append('health_status')

I didn't notice this before, @nepython can you help me to fix this? We should be able to just rewrite all the monkey patching in the class definition above those lines linked.

@nemesifier nemesifier added the bug Something isn't working label Aug 20, 2020
@nemesifier nemesifier mentioned this issue Aug 20, 2020
11 tasks
@nemesifier nemesifier added this to the 0.1 release milestone Aug 26, 2020
@nemesifier nemesifier added the Hacktoberfest Easy issues for attracting Hacktoberfest participants. label Sep 30, 2020
nepython added a commit that referenced this issue Oct 1, 2020
Closes #202

DeviceAdmin was being extended by two different approaches. Registering a new admin and monkey patching.
This could lead to inconsistent behaviour. After this commit we stick to extending DeviceAdmin by (the first approach) registering a new admin and unregistering the previous one.

Also ``get_inlines`` helper method has been introduced in Django starting from version 3.0. So to support custom inlines in previous versions we have added ``get_inline_instances`` method.
This method can be safely removed when we drop support for Django 2.
nepython added a commit that referenced this issue Oct 1, 2020
Closes #202

DeviceAdmin was being extended by two different approaches. Registering a new admin and monkey patching.
This could lead to inconsistent behaviour. After this commit we stick to extending DeviceAdmin by (the first approach) registering a new admin and unregistering the previous one.

Also ``get_inlines`` helper method has been introduced in Django starting from version 3.0. So to support custom inlines in previous versions we have added ``get_inline_instances`` method.
This method can be safely removed when we drop support for Django 2.
nepython added a commit that referenced this issue Oct 1, 2020
Closes #202

DeviceAdmin was being extended by two different approaches. Registering a new admin and monkey patching.
This could lead to inconsistent behaviour. After this commit we stick to extending DeviceAdmin by (the first approach) registering a new admin and unregistering the previous one.

Also ``get_inlines`` helper method has been introduced in Django starting from version 3.0. So to support custom inlines in previous versions we have added ``get_inline_instances`` method.
This method can be safely removed when we drop support for Django 2.
nepython added a commit that referenced this issue Oct 3, 2020
Closes #202

DeviceAdmin was being extended by two different approaches. Registering a new admin and monkey patching.
This could lead to inconsistent behaviour. After this commit we stick to extending DeviceAdmin by (the first approach) registering a new admin and unregistering the previous one.

Also ``get_inlines`` helper method has been introduced in Django starting from version 3.0. So to support custom inlines in previous versions we have added ``get_inline_instances`` method.
This method can be safely removed when we drop support for Django 2.
nepython added a commit that referenced this issue Oct 3, 2020
Closes #202

DeviceAdmin was being extended by two different approaches. Registering a new admin and monkey patching.
This could lead to inconsistent behaviour. After this commit we stick to extending DeviceAdmin by (the first approach) registering a new admin and unregistering the previous one.

Also ``get_inlines`` helper method has been introduced in Django starting from version 3.0. So to support custom inlines in previous versions we have added ``get_inline_instances`` method.
This method can be safely removed when we drop support for Django 2.
nepython added a commit that referenced this issue Oct 3, 2020
Closes #202

DeviceAdmin was being extended by two different approaches. Registering a new admin and monkey patching.
This could lead to inconsistent behaviour. After this commit we stick to extending DeviceAdmin by (the first approach) registering a new admin and unregistering the previous one.

Also ``get_inlines`` helper method has been introduced in Django starting from version 3.0. So to support custom inlines in previous versions we have added ``get_inline_instances`` method.
This method can be safely removed when we drop support for Django 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working Hacktoberfest Easy issues for attracting Hacktoberfest participants.
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant