Skip to content

Commit 2e692f5

Browse files
committed
Use django default is_superuser instead of custom admin field
1 parent f629e5b commit 2e692f5

File tree

6 files changed

+38
-19
lines changed

6 files changed

+38
-19
lines changed

src/firetower/auth/admin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class UserAdmin(BaseUserAdmin):
2929

3030
@admin.register(UserProfile)
3131
class UserProfileAdmin(admin.ModelAdmin):
32-
list_display = ["user", "is_admin", "avatar_url"]
33-
list_filter = ["is_admin"]
32+
list_display = ["user", "avatar_url"]
3433
search_fields = [
3534
"user__username",
3635
"user__email",
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Generated by Django 5.2.7 on 2025-10-21 19:36
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
dependencies = [
8+
("firetower_auth", "0002_rename_type_fields"),
9+
]
10+
11+
operations = [
12+
migrations.RemoveField(
13+
model_name="userprofile",
14+
name="is_admin",
15+
),
16+
]

src/firetower/auth/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ class UserProfile(models.Model):
1717
- email
1818
- first_name
1919
- last_name
20+
- is_superuser (for admin access)
2021
"""
2122

2223
user = models.OneToOneField(
2324
"auth.User", on_delete=models.CASCADE, related_name="userprofile"
2425
)
2526
avatar_url = models.URLField(blank=True)
26-
is_admin = models.BooleanField(default=False)
2727

2828
@property
2929
def user_incidents(self):

src/firetower/auth/tests/test_models.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ def test_userprofile_auto_created_on_user_creation(self):
1717

1818
assert hasattr(user, "userprofile")
1919
assert isinstance(user.userprofile, UserProfile)
20-
assert user.userprofile.is_admin is False
2120
assert user.userprofile.avatar_url == ""
2221

2322
def test_userprofile_str(self):

src/firetower/incidents/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,8 @@ def is_visible_to_user(self, user):
175175
if not self.is_private:
176176
return True
177177

178-
# Admins can see all incidents
179-
if hasattr(user, "userprofile") and user.userprofile.is_admin:
178+
# Superusers can see all incidents
179+
if user.is_superuser:
180180
return True
181181

182182
# Check if user is involved
@@ -239,7 +239,8 @@ def filter_visible_to_user(queryset, user):
239239
Returns:
240240
Filtered queryset
241241
"""
242-
if hasattr(user, "userprofile") and user.userprofile.is_admin:
242+
# Superusers see everything
243+
if user.is_superuser:
243244
return queryset
244245

245246
from django.db.models import Q

src/firetower/incidents/tests/test_models.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,13 @@ def test_private_incident_visible_to_participant(self):
159159
assert incident.is_visible_to_user(participant) is True
160160
assert incident.is_visible_to_user(other_user) is False
161161

162-
def test_private_incident_visible_to_admin(self):
163-
"""Test private incident is visible to admin users"""
164-
admin_user = User.objects.create_user(username="[email protected]")
165-
admin_user.userprofile.is_admin = True
166-
admin_user.userprofile.save()
162+
def test_private_incident_visible_to_superuser(self):
163+
"""Test private incident is visible to superusers"""
164+
superuser = User.objects.create_superuser(
165+
username="[email protected]",
166+
167+
password="test123",
168+
)
167169

168170
incident = Incident.objects.create(
169171
title="Private Incident",
@@ -172,7 +174,7 @@ def test_private_incident_visible_to_admin(self):
172174
is_private=True,
173175
)
174176

175-
assert incident.is_visible_to_user(admin_user) is True
177+
assert incident.is_visible_to_user(superuser) is True
176178

177179
def test_affected_areas_property(self):
178180
"""Test affected_areas property returns list of tag names"""
@@ -362,11 +364,13 @@ def test_external_link_str(self):
362364

363365
@pytest.mark.django_db
364366
class TestFilterVisibleToUser:
365-
def test_admin_sees_all_incidents(self):
366-
"""Test admin users see all incidents"""
367-
admin = User.objects.create_user(username="[email protected]")
368-
admin.userprofile.is_admin = True
369-
admin.userprofile.save()
367+
def test_superuser_sees_all_incidents(self):
368+
"""Test superusers see all incidents"""
369+
superuser = User.objects.create_superuser(
370+
username="[email protected]",
371+
372+
password="test123",
373+
)
370374

371375
public = Incident.objects.create(
372376
title="Public",
@@ -383,7 +387,7 @@ def test_admin_sees_all_incidents(self):
383387
)
384388

385389
queryset = Incident.objects.all()
386-
filtered = filter_visible_to_user(queryset, admin)
390+
filtered = filter_visible_to_user(queryset, superuser)
387391

388392
assert filtered.count() == 2
389393
assert public in filtered

0 commit comments

Comments
 (0)