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

Jlc/extende usersupport admin #230

Merged
merged 9 commits into from
Nov 6, 2024
7 changes: 7 additions & 0 deletions eox_nelp/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
"""General admin module file.
Register all the nelp admin models.
"""
from importlib.util import find_spec

from django.conf import settings

from eox_nelp.admin.certificates import * # noqa: F401
from eox_nelp.admin.course_creators import * # noqa: F401
from eox_nelp.admin.student import * # noqa: F401
from eox_nelp.course_experience.admin import * # noqa: F401
from eox_nelp.notifications.admin import * # noqa: F401
from eox_nelp.payment_notifications.admin import * # noqa: F401
from eox_nelp.pearson_vue.admin import * # noqa: F401

if find_spec('eox_support') and 'eox_support.apps.EoxSupportConfig' in settings.INSTALLED_APPS:
from eox_nelp.admin.user import * # noqa: F401
38 changes: 38 additions & 0 deletions eox_nelp/admin/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from unittest.mock import MagicMock, patch

from ddt import data, ddt, unpack
from django.contrib import admin
from django.contrib.auth import get_user_model
from django.test import RequestFactory, TestCase, override_settings

from eox_nelp.admin import (
Expand All @@ -19,6 +21,8 @@
pearson_update_ead_action,
)

User = get_user_model()


@ddt
class TestPearsonAction(TestCase):
Expand Down Expand Up @@ -149,3 +153,37 @@ def test_actions(self, admin_action):
- pearson_real_time_action method is in model actions.
"""
self.assertIn(admin_action, self.modeladmin.actions)


class TestNelpUserAdmin(TestCase):
"""
Unit tests for the NelpUserAdmin class.
"""

def test_load_module(self):
"""
Test loading module of NelpUserAdmin when eox_support condition is bypassed.

Expected behavior:
- Test NelpUserAdmin is loaded in the user model key of admin registry.
- user_national_id is in list_display
- UserExtraInfoInline in inlines
"""
class ProtoAdminUserSupportModule:
"""Proto class to simulate module"""
class SupportUserAdmin(admin.ModelAdmin):
"""Base User Model admin for testing purposes. With empty tuple to add the nelp values"""
list_display = ((),)
search_fields = ((),)
inlines = ((),)
readonly_fields = ((),)

with patch.dict("sys.modules", {"eox_support.admin.user": ProtoAdminUserSupportModule}):
from eox_nelp.admin.user import ( # pylint: disable=import-outside-toplevel
NelpUserAdmin,
UserExtraInfoInline,
)

self.assertIsInstance(admin.site._registry[User], NelpUserAdmin) # pylint: disable=protected-access
self.assertIn("user_national_id", NelpUserAdmin.list_display)
self.assertIn(UserExtraInfoInline, NelpUserAdmin.inlines)
41 changes: 41 additions & 0 deletions eox_nelp/admin/user.py
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think you can simplify this file, basically the new model only has effects if eox-support is installed
so if you move this condition if find_spec('eox_support') and 'eox_support.apps.EoxSupportConfig' in settings.INSTALLED_APPS: to a higher level ( eox_vnelp/admin/init.py) you can remove all those repeated validations from this file.

Just one question, why you didn't include eox-support as requierement?

Copy link
Collaborator Author

@johanseto johanseto Nov 1, 2024

Choose a reason for hiding this comment

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

This is basically because the admin of eox_support uses a lot of edx_app wrappers. So, this started to bother with this kind of error in the test environment.

Screenshot from 2024-10-31 18-22-25

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also moved to init file the if condition.
606b4eb

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
This module defines the Django admin configuration for handling user model.

Classes:
NelpUserAdmin: Custom admin class for User model to include extra info fields like national_id.
UserExtraInfoInline: inline for extra info model
"""


from custom_reg_form.models import ExtraInfo
from django.contrib.admin import StackedInline
from django.contrib.auth import get_user_model
from eox_support.admin.user import SupportUserAdmin # pylint: disable=import-error

from eox_nelp.admin.register_admin_model import register_admin_model as register

User = get_user_model()


class UserExtraInfoInline(StackedInline):
""" Inline admin interface for Extra Info model. """
model = ExtraInfo
can_delete = False
verbose_name_plural = 'Extra info'


class NelpUserAdmin(SupportUserAdmin):
"""EoxNelp User admin class."""
list_display = SupportUserAdmin.list_display[:2] + ('user_national_id',) + SupportUserAdmin.list_display[2:]
search_fields = SupportUserAdmin.search_fields + ('extrainfo__national_id',)
inlines = SupportUserAdmin.inlines + (UserExtraInfoInline,)

def user_national_id(self, instance):
"""Return national_id associated with the user extra_info instance."""
if getattr(instance, "extrainfo", None):
return instance.extrainfo.national_id

return None


register(User, NelpUserAdmin)
Loading