Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
hieplpvip committed Oct 20, 2023
1 parent 42538ba commit 61161c3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
4 changes: 2 additions & 2 deletions judge/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ def clean_code(self):
if self.org_pk is None:
return code
org = Organization.objects.get(pk=self.org_pk)
prefix = ''.join(x for x in org.slug.lower() if x.isalnum()) + '_'
prefix = org.slug_prefix
if not code.startswith(prefix):
raise forms.ValidationError(_('Problem id code must starts with `%s`') % (prefix, ),
'problem_id_invalid_prefix')
Expand Down Expand Up @@ -692,7 +692,7 @@ def clean_key(self):
if self.org_pk is None:
return key
org = Organization.objects.get(pk=self.org_pk)
prefix = ''.join(x for x in org.slug.lower() if x.isalnum()) + '_'
prefix = org.slug_prefix
if not key.startswith(prefix):
raise forms.ValidationError(_('Contest id must starts with `%s`') % (prefix, ),
'contest_id_invalid_prefix')
Expand Down
27 changes: 27 additions & 0 deletions judge/models/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from sortedm2m.fields import SortedManyToManyField

from judge.models.choices import ACE_THEMES, MATH_ENGINES_CHOICES, SITE_THEMES, TIMEZONE
from judge.models.contest import Contest
from judge.models.runtime import Language
from judge.ratings import rating_class
from judge.utils.float_compare import float_compare_equal
Expand Down Expand Up @@ -68,6 +69,10 @@ class Organization(models.Model):

_pp_table = [pow(settings.VNOJ_ORG_PP_STEP, i) for i in range(settings.VNOJ_ORG_PP_ENTRIES)]

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__original_slug = self.slug

def calculate_points(self, table=_pp_table):
data = self.members.get_queryset().order_by('-performance_points') \
.values_list('performance_points', flat=True).filter(performance_points__gt=0)
Expand All @@ -84,6 +89,14 @@ def on_user_changes(self):
self.member_count = member_count
self.save(update_fields=['member_count'])

@classmethod
def get_slug_prefix(cls, slug):
return ''.join(x for x in slug.lower() if x.isalnum()) + '_'

@cached_property
def slug_prefix(self):
return self.get_slug_prefix(self.slug)

@cached_property
def admins_list(self):
return self.admins.all()
Expand All @@ -110,6 +123,20 @@ def get_absolute_url(self):
def get_users_url(self):
return reverse('organization_users', args=[self.slug])

def save(self, *args, **kwargs):
super().save(*args, **kwargs)

if self.__original_slug and self.slug != self.__original_slug:
# Rename contests to new prefix
# TODO: rename problems
old_prefix = self.get_slug_prefix(self.__original_slug)
contests = Contest.objects.filter(organizations=self, key__startswith=old_prefix).all()
for contest in contests:
contest.key = self.slug_prefix + contest.key[len(old_prefix):]
Contest.objects.bulk_update(contests, ['key'])

save.alters_data = True

class Meta:
ordering = ['name']
permissions = (
Expand Down
6 changes: 3 additions & 3 deletions judge/views/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ class ProblemCreateOrganization(AdminOrganizationMixin, ProblemCreate):
def get_initial(self):
initial = super(ProblemCreateOrganization, self).get_initial()
initial = initial.copy()
initial['code'] = ''.join(x for x in self.organization.slug.lower() if x.isalnum()) + '_'
initial['code'] = self.organization.slug_prefix
return initial

def get_form_kwargs(self):
Expand Down Expand Up @@ -644,7 +644,7 @@ def form_valid(self, form):
with revisions.create_revision(atomic=True):
post = form.save()
post.authors.add(self.request.user.profile)
post.slug = ''.join(x for x in self.organization.slug.lower() if x.isalnum()) # Initial post slug
post.slug = self.organization.slug_prefix # Initial post slug
post.organization = self.organization
post.save()

Expand All @@ -660,7 +660,7 @@ class ContestCreateOrganization(AdminOrganizationMixin, CreateContest):
def get_initial(self):
initial = super(ContestCreateOrganization, self).get_initial()
initial = initial.copy()
initial['key'] = ''.join(x for x in self.organization.slug.lower() if x.isalnum()) + '_'
initial['key'] = self.organization.slug_prefix
return initial

def get_form_kwargs(self):
Expand Down

0 comments on commit 61161c3

Please sign in to comment.