Skip to content

Commit

Permalink
Merge pull request #307 from ZdruzenieSTROM/sum_methods
Browse files Browse the repository at this point in the history
Refactor utils + fix sum methods
  • Loading branch information
mmihalik authored Dec 9, 2023
2 parents 715d44e + 45bb8b3 commit 2fd1ac9
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 167 deletions.
2 changes: 1 addition & 1 deletion base/management/commands/load_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from competition.models import (Competition, EventRegistration, Grade, Problem,
Semester, Series, Solution)
from competition.utils import get_school_year_by_date
from competition.utils.school_year_manipulation import get_school_year_by_date
from personal.models import Profile, School
from user.models import User

Expand Down
2 changes: 1 addition & 1 deletion base/management/commands/populate_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.utils.timezone import now

from competition.models import Competition, Event, Publication, PublicationType
from competition.utils import get_school_year_by_date
from competition.utils.school_year_manipulation import get_school_year_by_date


class Command(BaseCommand):
Expand Down
23 changes: 18 additions & 5 deletions competition/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@
from base.managers import UnspecifiedValueManager
from base.models import RestrictedFileField
from base.validators import school_year_validator
from competition import utils
from competition.exceptions import FreezingNotClosedResults
from competition.querysets import ActiveQuerySet
from competition.utils.school_year_manipulation import (
get_school_year_end_by_date, get_school_year_start_by_date)
from personal.models import Profile, School
from user.models import User

Expand Down Expand Up @@ -74,7 +75,7 @@ class Meta:

def can_user_participate(self, user):
if self.min_years_until_graduation:
return user.profile.year_of_graduation-utils.get_school_year_start_by_date() \
return user.profile.year_of_graduation-get_school_year_start_by_date() \
>= self.min_years_until_graduation
return True

Expand Down Expand Up @@ -239,6 +240,18 @@ def __str__(self):
return f'{self.competition.name}, {self.year}. ročník - {self.season} semester'


SERIES_SUM_METHODS = [
('series_simple_sum', 'Jednoduchý súčet bodov'),
('series_Malynar_sum', 'Bonifikácia Malynár'),
('series_Matik_sum', 'Bonifikácia Matik'),
('series_STROM_sum', 'Bonifikácia STROM'),
('series_Malynar_sum_until_2021', 'Bonifikácia Malynár (Do 2020/2021)'),
('series_Matik_sum_until_2021', 'Bonifikácia Matik (Do 2020/2021)'),
('series_STROM_sum_until_2021', 'Bonifikácia STROM (Do 2020/2021)'),
('series_STROM_4problems_sum', 'Bonifikácia STROM (4. úlohy)')
]


class Series(models.Model):
"""
Popisuje jednu sériu semestra
Expand All @@ -257,7 +270,7 @@ class Meta:
# Implementuje bonfikáciu
sum_method = models.CharField(
verbose_name='Súčtová metóda', max_length=50, blank=True,
choices=utils.SERIES_SUM_METHODS)
choices=SERIES_SUM_METHODS)
frozen_results = models.TextField(
null=True,
blank=True,
Expand Down Expand Up @@ -491,12 +504,12 @@ class Meta:
objects = UnspecifiedValueManager(unspecified_value_pk=13)

def get_year_of_graduation_by_date(self, date=None):
return utils.get_school_year_end_by_date(date) + self.years_until_graduation
return get_school_year_end_by_date(date) + self.years_until_graduation

@staticmethod
def get_grade_by_year_of_graduation(year_of_graduation, date=None):
years_until_graduation = year_of_graduation - \
utils.get_school_year_end_by_date(date)
get_school_year_end_by_date(date)

try:
grade = Grade.objects.get(
Expand Down
153 changes: 0 additions & 153 deletions competition/utils.py

This file was deleted.

Empty file added competition/utils/__init__.py
Empty file.
49 changes: 49 additions & 0 deletions competition/utils/results.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
def rank_results(results: list[dict]) -> list[dict]:
# Spodná hranica
current_rank = 1
n_teams = 1
last_points = None
for res in results:
if last_points != res['total']:
current_rank = n_teams
last_points = res['total']
res['rank_changed'] = True
else:
res['rank_changed'] = False
res['rank_start'] = current_rank
n_teams += 1

# Horná hranica
current_rank = len(results)
n_teams = len(results)
last_points = None
for res in reversed(results):
if last_points != res['total']:
current_rank = n_teams
last_points = res['total']
res['rank_end'] = current_rank
n_teams -= 1
return results


def generate_praticipant_invitations(
results_with_ranking: list[dict],
number_of_participants: int,
number_of_substitues: int) -> list[dict]:
invited_users = []
for i, result_row in enumerate(results_with_ranking):
if i < number_of_participants:
invited_users.append({
'first_name': result_row['registration']['profile']['first_name'],
'last_name': result_row['registration']['profile']['last_name'],
'school': result_row['registration']['school'],
'is_participant': True
})
elif i < number_of_participants+number_of_substitues:
invited_users.append({
'first_name': result_row['registration']['profile']['first_name'],
'last_name': result_row['registration']['profile']['last_name'],
'school': result_row['registration']['school'],
'is_participant': False
})
return invited_users
19 changes: 19 additions & 0 deletions competition/utils/school_year_manipulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from datetime import datetime
from typing import Optional

from django.utils.timezone import now


def get_school_year_start_by_date(date: Optional[datetime] = None) -> int:
if date is None:
date = now()

return date.year if date.month >= 9 else date.year - 1


def get_school_year_end_by_date(date: Optional[datetime] = None) -> int:
return get_school_year_start_by_date(date) + 1


def get_school_year_by_date(date: Optional[datetime] = None) -> str:
return f'{get_school_year_start_by_date(date)}/{get_school_year_end_by_date(date)}'
Loading

0 comments on commit 2fd1ac9

Please sign in to comment.