Skip to content
Snippets Groups Projects

Formulářová políčka; Přidání účastníka do soutěže; Editace registrace do ročníku

2 files
+ 49
22
Compare changes
  • Side-by-side
  • Inline

Files

+ 31
84
@@ -16,6 +16,8 @@ import mo.util
from mo.util import logger
from mo.util_format import format_decimal
reason = "import"
class ImportType(db.MOEnum):
participants = auto()
@@ -149,21 +151,16 @@ class Import:
return place
def parse_school(self, kod: str) -> Optional[db.Place]:
if kod == "":
return self.error('Škola je povinná')
if kod in self.school_place_cache:
return self.school_place_cache[kod]
place = db.get_place_by_code(kod, fetch_school=True)
if not place:
return self.error(f'Škola s kódem "{kod}" nenalezena'+
('. Nechybí vám # na začátku?' if re.fullmatch(r'\d+', kod) else ''))
if place.type != db.PlaceType.school:
return self.error(f'Kód školy "{kod}" neodpovídá škole')
try:
place = mo.users.validate_and_find_school(kod)
except mo.CheckError as e:
return self.error(str(e))
self.school_place_cache[kod] = place
return place
def parse_grade(self, rocnik: str, school: Optional[db.School]) -> Optional[str]:
@@ -174,48 +171,30 @@ class Import:
# lidé připisují všechny možné i nemožné znaky, které vypadají jako apostrof :)
rocnik = re.sub('[\'"\u00b4\u2019]', "", rocnik)
if (not re.fullmatch(r'\d(/\d)?', rocnik)):
return self.error(f'Ročník má neplatný formát, musí to být buď číslice, nebo číslice/číslice')
if (not school.is_zs and re.fullmatch(r'\d', rocnik)):
return self.error(f'Ročník pro střední školu ({school.place.name}) zapisujte ve formátu číslice/číslice')
if (not school.is_ss and re.fullmatch(r'\d/\d', rocnik)):
return self.error(f'Ročník pro základní školu ({school.place.name}) zapisujte jako číslici 1–9')
return rocnik
try:
return mo.users.normalize_grade(rocnik, school)
except mo.CheckError as e:
return self.error(str(e))
def parse_born(self, rok: str) -> Optional[int]:
if not re.fullmatch(r'\d{4}', rok):
return self.error('Rok narození musí být čtyřciferné číslo')
r = int(rok)
if r < 2000 or r > 2099:
return self.error('Rok narození musí být v intervalu [2000,2099]')
try:
mo.users.validate_born_year(r)
except mo.CheckError as e:
return self.error(str(e))
return r
def find_or_create_user(self, email: str, krestni: str, prijmeni: str, is_org: bool) -> Optional[db.User]:
sess = db.get_session()
user = sess.query(db.User).filter_by(email=email).one_or_none()
if user:
if user.first_name != krestni or user.last_name != prijmeni:
return self.error(f'Osoba již registrována s odlišným jménem {user.full_name()}')
if (user.is_admin or user.is_org) != is_org:
if is_org:
return self.error('Nelze předefinovat účastníka na organizátora')
else:
return self.error('Nelze předefinovat organizátora na účastníka')
else:
user = db.User(email=email, first_name=krestni, last_name=prijmeni, is_org=is_org)
sess.add(user)
sess.flush() # Aby uživatel dostal user_id
logger.info(f'Import: Založen uživatel user=#{user.user_id} email=<{user.email}>')
mo.util.log(
type=db.LogType.user,
what=user.user_id,
details={'action': 'import', 'new': db.row2dict(user)},
)
try:
user, is_new = mo.users.find_or_create_user(email, krestni, prijmeni, is_org, reason=reason)
except mo.CheckError as e:
return self.error(str(e))
if is_new:
self.cnt_new_users += 1
self.new_user_ids.append(user.user_id)
return user
@@ -235,53 +214,21 @@ class Import:
return pts
def find_or_create_participant(self, user: db.User, year: int, school_id: int, birth_year: int, grade: str) -> Optional[db.Participant]:
sess = db.get_session()
part = sess.query(db.Participant).get((user.user_id, year))
if part:
if (part.school != school_id
or part.grade != grade
or part.birth_year != birth_year):
return self.error('Účastník již zaregistrován s odlišnou školou/ročníkem/rokem narození')
else:
part = db.Participant(user=user, year=year, school=school_id, birth_year=birth_year, grade=grade)
sess.add(part)
logger.info(f'Import: Založen účastník #{user.user_id}')
mo.util.log(
type=db.LogType.participant,
what=user.user_id,
details={'action': 'import', 'new': db.row2dict(part)},
)
try:
part, is_new = mo.users.find_or_create_participant(user, year, school_id, birth_year, grade, reason=reason)
except mo.CheckError as e:
return self.error(str(e))
if is_new:
self.cnt_new_participants += 1
return part
def find_or_create_participation(self, user: db.User, contest: db.Contest, place: Optional[db.Place]) -> Optional[db.Participation]:
if place is None:
place = contest.place
sess = db.get_session()
pions = (sess.query(db.Participation)
.filter_by(user=user)
.filter(db.Participation.contest.has(db.Contest.round == contest.round))
.all())
if not pions:
pion = db.Participation(user=user, contest=contest, place_id=place.place_id, state=db.PartState.invited)
sess.add(pion)
logger.info(f'Import: Založena účast user=#{user.user_id} contest=#{contest.contest_id} place=#{place.place_id}')
mo.util.log(
type=db.LogType.participant,
what=user.user_id,
details={'action': 'add-to-contest', 'new': db.row2dict(pion)},
)
try:
pion, is_new = mo.users.find_or_create_participation(user, contest, place, reason=reason)
except mo.CheckError as e:
return self.error(str(e))
if is_new:
self.cnt_new_participations += 1
elif len(pions) == 1:
pion = pions[0]
if pion.place != place:
return self.error(f'Již se tohoto kola účastní v {contest.round.get_level().name_locative("jiném", "jiné", "jiném")} ({pion.place.get_code()})')
else:
return self.error('Již se tohoto kola účastní ve vice oblastech, což by nemělo být možné')
return pion
def obtain_contest(self, oblast: Optional[db.Place], allow_none: bool = False):
Loading