Skip to content

Commit

Permalink
Make sure Profile.ban_reason is either None or non-empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip committed Mar 16, 2024
1 parent 4c3b73e commit e233f12
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 6 additions & 0 deletions judge/admin/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ def get_form(self, request, obj=None, **kwargs):
)
return form

def save_model(self, request, obj, form, change):
super().save_model(request, obj, form, change)
if form.changed_data and 'ban_reason' in form.changed_data and form.cleaned_data['ban_reason'] == '':
obj.ban_reason = None
obj.save()


class UserAdmin(OldUserAdmin):
def save_model(self, request, obj, form, change):
Expand Down
4 changes: 2 additions & 2 deletions judge/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ def is_new_user(self):

@cached_property
def is_banned(self):
return self.ban_reason and not self.user.is_active
return not self.user.is_active and self.ban_reason is not None

def can_be_banned_by(self, staff):
return self.user != staff and not self.user.is_superuser and staff.has_perm('judge.ban_user')
Expand Down Expand Up @@ -370,7 +370,7 @@ def ban_user(self, reason):
ban_user.alters_data = True

def unban_user(self):
self.ban_reason = ''
self.ban_reason = None
self.display_rank = Profile._meta.get_field('display_rank').get_default()
self.is_unlisted = False
self.save(update_fields=['ban_reason', 'display_rank', 'is_unlisted'])
Expand Down

0 comments on commit e233f12

Please sign in to comment.