-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add user to contest
form doesn't have an option for direct add (#307)
* Add some functionality * Finish add user to contest * Fix indentation * Simplify teacher check * Simplify add_user_to_contest view * Add some tests * Gotta improve tests * Works on tests * Some works... * Idk what to do... * Finalize the PR with some cherries on top * Shorten some lines * Shorten some lines
- Loading branch information
Showing
7 changed files
with
265 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from oioioi.participants.models import Participant | ||
from oioioi.teachers.models import ContestTeacher, Teacher | ||
from django.core.exceptions import ValidationError | ||
from django.utils.translation import gettext_lazy as _ | ||
|
||
|
||
def get_user_teacher_obj(user): | ||
try: | ||
return user.teacher | ||
except Teacher.DoesNotExist: | ||
return None | ||
|
||
|
||
def is_user_already_in_contest(user, contest): | ||
teacher = get_user_teacher_obj(user) | ||
|
||
if user.participant_set.filter(contest=contest) or \ | ||
(teacher and | ||
teacher.contestteacher_set.filter(contest=contest)): | ||
return True | ||
|
||
return False | ||
|
||
|
||
def validate_can_add_user_to_contest(user, contest, member_type): | ||
exists = False | ||
|
||
if not is_user_already_in_contest(user, contest): | ||
if member_type == 'pupil': | ||
exists = len(Participant.objects.filter( | ||
contest=contest, user=user | ||
)) > 0 | ||
elif member_type == 'teacher': | ||
if teacher := get_user_teacher_obj(user): | ||
exists = len(ContestTeacher.objects.filter( | ||
contest=contest, teacher=teacher | ||
)) > 0 | ||
else: | ||
raise ValidationError( | ||
_("User is not a teacher: \'%(user)s\'") | ||
% {"user": user }) | ||
else: | ||
raise ValueError("Invalid member type") | ||
else: | ||
exists = True | ||
|
||
if exists: | ||
raise ValidationError( | ||
_("User is already added: \'%(user)s\'") | ||
% { "user": user }) | ||
|
||
|
||
def add_user_to_contest_as(user, contest, member_type): | ||
validate_can_add_user_to_contest(user, contest, member_type) | ||
created = False | ||
|
||
if member_type == 'pupil': | ||
Participant.objects.get_or_create( | ||
contest=contest, user=user | ||
) | ||
elif member_type == 'teacher': | ||
teacher = get_user_teacher_obj(user) | ||
ContestTeacher.objects.get_or_create( | ||
contest=contest, teacher=teacher | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters