Skip to content

Commit

Permalink
Fix user admin object creation and deletion; DMOJ/online-judge#931, D…
Browse files Browse the repository at this point in the history
…MOJ/online-judge#2288

- Automatically create profile when adding a user through admin
- Disallow adding profiles through admin
- (Soft) disallow deleting profiles through admin
  • Loading branch information
Ninjaclasher authored and hieplpvip committed Oct 30, 2023
1 parent f0d2fc3 commit 2bfa6ad
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
5 changes: 4 additions & 1 deletion judge/admin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import User
from django.contrib.flatpages.models import FlatPage

from judge.admin.comments import CommentAdmin
from judge.admin.contest import ContestAdmin, ContestParticipationAdmin, ContestTagAdmin
from judge.admin.interface import BlogPostAdmin, FlatPageAdmin, LicenseAdmin, LogEntryAdmin, NavigationBarAdmin
from judge.admin.organization import OrganizationAdmin, OrganizationRequestAdmin
from judge.admin.problem import ProblemAdmin
from judge.admin.profile import ProfileAdmin
from judge.admin.profile import ProfileAdmin, UserAdmin
from judge.admin.runtime import JudgeAdmin, LanguageAdmin
from judge.admin.submission import SubmissionAdmin
from judge.admin.tag import TagAdmin, TagGroupAdmin, TagProblemAdmin
Expand Down Expand Up @@ -44,3 +45,5 @@
admin.site.register(Tag, TagAdmin)
admin.site.register(TagGroup, TagGroupAdmin)
admin.site.register(TagProblem, TagProblemAdmin)
admin.site.unregister(User)
admin.site.register(User, UserAdmin)
19 changes: 19 additions & 0 deletions judge/admin/profile.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin as OldUserAdmin
from django.forms import ModelForm
from django.urls import reverse_lazy
from django.utils.html import format_html
Expand Down Expand Up @@ -73,6 +74,17 @@ class ProfileAdmin(NoBatchDeleteMixin, VersionAdmin):
form = ProfileForm
inlines = [WebAuthnInline]

def has_add_permission(self, request, obj=None):
return False

# We can't use has_delete_permission here because we still want user profiles to be
# deleteable through related objects (i.e. User). Thus, we simply hide the delete button.
# If an admin wants to go directly to the delete endpoint to delete a profile, more
# power to them.
def render_change_form(self, request, context, **kwargs):
context['show_delete'] = False
return super().render_change_form(request, context, **kwargs)

def get_queryset(self, request):
return super(ProfileAdmin, self).get_queryset(request).select_related('user')

Expand Down Expand Up @@ -140,3 +152,10 @@ def get_form(self, request, obj=None, **kwargs):
mode='javascript', theme=request.profile.resolved_ace_theme,
)
return form


class UserAdmin(OldUserAdmin):
def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
if not change:
Profile.objects.create(user=obj)

0 comments on commit 2bfa6ad

Please sign in to comment.