diff --git a/app/core/admin.py b/app/core/admin.py index a4e11e9..036006a 100644 --- a/app/core/admin.py +++ b/app/core/admin.py @@ -1,3 +1,31 @@ -from django.contrib import admin # noqa +"""Django Admin customization for the core app.""" +from django.contrib import admin +from django.contrib.auth.admin import UserAdmin as BaseUserAdmin +from django.utils.translation import gettext_lazy as _ +from core import models -# Register your models here. +class UserAdmin(BaseUserAdmin): + """Customize the user admin page.""" + ordering = ['id'] + list_display = ['email', 'name'] + + fieldsets = ( + (None, {'fields': ('email', 'password')}), + ('Personal Info', {'fields': ('name',)}), + ( + _('Permissions'), + { + 'fields': ('is_active', 'is_staff', 'is_superuser'), + } + ), + ('Important dates', {'fields': ('last_login',)}), + ) + add_fieldsets = ( + (None, { + + 'classes': ('wide',), + 'fields': ('email', 'password1', 'password2'), + }), + ) + +admin.site.register(models.User, UserAdmin) \ No newline at end of file diff --git a/app/core/tests/test_admin.py b/app/core/tests/test_admin.py new file mode 100644 index 0000000..2b3d99f --- /dev/null +++ b/app/core/tests/test_admin.py @@ -0,0 +1,42 @@ +from django.test import TestCase, Client +from django.contrib.auth import get_user_model +from django.urls import reverse + +class AdminSiteTests(TestCase): + """Test the admin site""" + + def setUp(self): + """Create user & client for testing""" + self.client = Client() + self.admin_user = get_user_model().objects.create_superuser( + email='admin@example.com', + password='password123', + ) + self.client.force_login(self.admin_user) + self.user = get_user_model().objects.create_user( + email='user@example.com', + password='password123', + name='Test user full name', + ) + + def test_users_list(self): + """Test users are listed on user page""" + url = reverse('admin:core_user_changelist') + res = self.client.get(url) + + self.assertContains(res, self.user.name) + self.assertContains(res, self.user.email) + + def test_user_change_page(self): + """Test user edit page works""" + url = reverse('admin:core_user_change', args=[self.user.id]) + res = self.client.get(url) + + self.assertEqual(res.status_code, 200) + + def test_create_user_page(self): + """Test create user page works""" + url = reverse('admin:core_user_add') + res = self.client.get(url) + + self.assertEqual(res.status_code, 200) \ No newline at end of file