-
Notifications
You must be signed in to change notification settings - Fork 10
Add new models to admin with additional inlines. Include tests #581
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
Merged
AdrianDAlessandro
merged 3 commits into
576-add-new-models
from
577-new-framework-admin
Feb 17, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Generated by Django 6.0.1 on 2026-02-10 12:12 | ||
|
|
||
| import django.contrib.auth.models | ||
| from django.db import migrations | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('main', '0012_learningresource_provider_skill_learning_resources_and_more'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='UserProxy', | ||
| fields=[ | ||
| ], | ||
| options={ | ||
| 'verbose_name': 'User Skills', | ||
| 'verbose_name_plural': 'User Skills', | ||
| 'proxy': True, | ||
| 'indexes': [], | ||
| 'constraints': [], | ||
| }, | ||
| bases=('main.user',), | ||
| managers=[ | ||
| ('objects', django.contrib.auth.models.UserManager()), | ||
| ], | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| """Test suite for the main app admin views.""" | ||
|
|
||
| from http import HTTPStatus | ||
| from typing import Any, ClassVar | ||
|
|
||
| import pytest | ||
| from django.contrib.auth import get_user_model | ||
| from django.db.models import Model | ||
|
|
||
| from main.admin import UserProxy | ||
| from main.models import ( | ||
| Category, | ||
| LearningResource, | ||
| Provider, | ||
| Skill, | ||
| SkillLevel, | ||
| Tool, | ||
| ) | ||
|
|
||
|
|
||
| class AdminMixin: | ||
| """Mixin for testing models that have a visible admin view. | ||
|
|
||
| Note: Using this requires the test class to define: | ||
| - A `_model` variable that is the model being tested | ||
| - A `_data` dict with the kwargs needed to create an instance of the model | ||
| """ | ||
|
|
||
| _model: type[Model] | ||
| _data: ClassVar[dict[str, Any]] = {} | ||
|
|
||
| def test_list_view(self, admin_client): | ||
| """Test the list view of the admin model.""" | ||
| response = admin_client.get(f"/admin/main/{self._model._meta.model_name}/") | ||
| assert response.status_code == HTTPStatus.OK | ||
|
|
||
| def test_add_get(self, admin_client): | ||
| """Test the GET request is successful.""" | ||
| response = admin_client.get(f"/admin/main/{self._model._meta.model_name}/add/") | ||
| assert response.status_code == HTTPStatus.OK | ||
|
|
||
| def _create_object(self): | ||
| """Create the object being changed.""" | ||
| return self._model.objects.create(**self._data) | ||
|
|
||
| @pytest.mark.django_db | ||
| def test_change_get(self, admin_client): | ||
| """Test the GET request to the change view is successful.""" | ||
| obj = self._create_object() | ||
| response = admin_client.get( | ||
| f"/admin/main/{self._model._meta.model_name}/{obj.pk}/change/" | ||
| ) | ||
| assert response.status_code == HTTPStatus.OK | ||
|
|
||
|
|
||
| class TestUserAdmin(AdminMixin): | ||
| """Test suite for the User admin views.""" | ||
|
|
||
| _model = get_user_model() | ||
| _data: ClassVar[dict[str, Any]] = { | ||
| "username": "TestUser", | ||
| "password": "testpassword", | ||
| } | ||
|
|
||
|
|
||
| class TestCategoryAdmin(AdminMixin): | ||
| """Test suite for the Category admin views.""" | ||
|
|
||
| _model = Category | ||
| _data: ClassVar[dict[str, Any]] = { | ||
| "name": "Test Category", | ||
| "description": "Test Description", | ||
| } | ||
|
|
||
|
|
||
| class TestProviderAdmin(AdminMixin): | ||
| """Test suite for the Provider admin views.""" | ||
|
|
||
| _model = Provider | ||
| _data: ClassVar[dict[str, Any]] = { | ||
| "name": "Test Provider", | ||
| "description": "Test Description", | ||
| "url": "https://example.com", | ||
| "ror": "https://ror.org/12345", | ||
| } | ||
|
|
||
|
|
||
| class TestLearningResourceAdmin(AdminMixin): | ||
| """Test suite for the LearningResource admin views.""" | ||
|
|
||
| _model = LearningResource | ||
| _data: ClassVar[dict[str, Any]] = { | ||
| "name": "Test LearningResource", | ||
| "description": "Test Description", | ||
| "language": "en", | ||
| "url": "https://example.com/resource", | ||
| "provider": None, | ||
| } | ||
|
|
||
|
|
||
| class TestToolAdmin(AdminMixin): | ||
| """Test suite for the Tool admin views.""" | ||
|
|
||
| _model = Tool | ||
| _data: ClassVar[dict[str, Any]] = { | ||
| "name": "Test Tool", | ||
| "description": "Test Description", | ||
| } | ||
|
|
||
|
|
||
| class TestSkillAdmin(AdminMixin): | ||
| """Test suite for the Skill admin views.""" | ||
|
|
||
| _model = Skill | ||
| _data: ClassVar[dict[str, Any]] = { | ||
| "name": "Test Skill", | ||
| "description": "Test Description", | ||
| } | ||
|
|
||
| def _create_object(self): | ||
| """Overwrite to also create the Category for the Skill being changed.""" | ||
| parent_category = Category.objects.create( | ||
| name="Test Parent Category", description="Test Parent" | ||
| ) | ||
| subcategory = Category.objects.create( | ||
| name="Test Subcategory", | ||
| description="Test Subcategory", | ||
| parent_category=parent_category, | ||
| ) | ||
| return Skill.objects.create(category=subcategory, **self._data) | ||
|
|
||
|
|
||
| class TestSkillLevelAdmin(AdminMixin): | ||
| """Test suite for the SkillLevel admin views.""" | ||
|
|
||
| _model = SkillLevel | ||
| _data: ClassVar[dict[str, Any]] = { | ||
| "name": "Test SkillLevel", | ||
| "description": "Test Description", | ||
| "level": 1, | ||
| } | ||
|
|
||
|
|
||
| class TestCustomUserSkillAdmin(AdminMixin): | ||
| """Test suite for the CustomUserSkill admin views. | ||
|
|
||
| This Model should not have an add view. | ||
| """ | ||
|
|
||
| _model = UserProxy | ||
| _data: ClassVar[dict[str, Any]] = { | ||
| "username": "TestUser", | ||
| "password": "testpassword", | ||
| } | ||
|
|
||
| def test_add_get(self, admin_client): | ||
| """Overwrite to test the GET request to add an object is forbidden.""" | ||
| response = admin_client.get(f"/admin/main/{self._model._meta.model_name}/add/") | ||
| assert response.status_code == HTTPStatus.FORBIDDEN |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.