From 587bf38ec916dc9ba9caace6235e1cf229ad48ae Mon Sep 17 00:00:00 2001 From: Sekhar-Kumar-Dash <119131588+Sekhar-Kumar-Dash@users.noreply.github.com> Date: Sun, 27 Apr 2025 19:42:25 +0530 Subject: [PATCH 1/5] fixed pylint errors in views.py --- apps/participants/views.py | 44 ++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/apps/participants/views.py b/apps/participants/views.py index 8a744ec7eb..aa397cf915 100644 --- a/apps/participants/views.py +++ b/apps/participants/views.py @@ -245,8 +245,10 @@ def invite_participant_to_team(request, pk): participant_email ) in participant_team.get_all_participants_email(): if participant_email in challenge.banned_email_ids: - message = "You cannot invite as you're a part of {} team and it has been banned " - "from this challenge. Please contact the challenge host." + message = ("You cannot invite as you're a part of {} " + "team and it has been banned ") + ("from this challenge. " + "Please contact the challenge host.") response_data = { "error": message.format(participant_team.team_name) } @@ -257,7 +259,8 @@ def invite_participant_to_team(request, pk): # Check if invited user is banned if email in challenge.banned_email_ids: - message = "You cannot invite as the invited user has been banned " + message = ("You cannot invite as the invited user " + "has been banned ") "from this challenge. Please contact the challenge host." response_data = {"error": message} return Response( @@ -267,7 +270,9 @@ def invite_participant_to_team(request, pk): # Check if user is in allowed list. if len(challenge.allowed_email_domains) > 0: if not is_user_in_allowed_email_domains(email, challenge_pk): - message = "Sorry, users with {} email domain(s) are only allowed to participate in this challenge." + message = ("Sorry, users with {} email domain(s) are " + "only allowed to participate " + "in this challenge.") domains = "" for domain in challenge.allowed_email_domains: domains = "{}{}{}".format(domains, "/", domain) @@ -279,7 +284,8 @@ def invite_participant_to_team(request, pk): # Check if user is in blocked list. if is_user_in_blocked_email_domains(email, challenge_pk): - message = "Sorry, users with {} email domain(s) are not allowed to participate in this challenge." + message = ("Sorry, users with {} email domain(s) are " + "not allowed to participate in this challenge.") domains = "" for domain in challenge.blocked_email_domains: domains = "{}{}{}".format(domains, "/", domain) @@ -329,7 +335,9 @@ def delete_participant_from_team(request, participant_team_pk, participant_pk): participant.user == request.user ): # when the user tries to remove himself response_data = { - "error": "You are not allowed to remove yourself since you are admin. Please delete the team if you want to do so!" + "error": "You are not allowed to remove yourself since you " + "are admin. Please delete the team if " + "you want to do so!" } # noqa: ignore=E501 return Response( response_data, status=status.HTTP_406_NOT_ACCEPTABLE @@ -339,7 +347,8 @@ def delete_participant_from_team(request, participant_team_pk, participant_pk): return Response(status=status.HTTP_204_NO_CONTENT) else: response_data = { - "error": "Sorry, you do not have permissions to remove this participant" + "error": "Sorry, you do not have permissions " + "to remove this participant" } return Response(response_data, status=status.HTTP_401_UNAUTHORIZED) @@ -415,7 +424,8 @@ def remove_self_from_participant_team(request, participant_team_pk): [participant_team] ).exists(): response_data = { - "error": "Sorry, you cannot delete this team since it has taken part in challenge(s)!" + "error": "Sorry, you cannot delete this team since " + "it has taken part in challenge(s)!" } return Response(response_data, status=status.HTTP_403_FORBIDDEN) else: @@ -439,7 +449,8 @@ def get_participant_team_details_for_challenge(request, challenge_pk): challenge_pk {[int]} -- Challenge primary key Returns: - {dict} -- Participant team detail that has participated in the challenge + {dict} -- Participant team detail that has + participated in the challenge """ challenge = get_challenge_model(challenge_pk) @@ -451,7 +462,8 @@ def get_participant_team_details_for_challenge(request, challenge_pk): return Response(serializer.data, status=status.HTTP_200_OK) else: response_data = { - "error": f"The user {request.user.username} has not participanted in {challenge.title}" + "error": f"The user {request.user.username} " + f"has not participanted in {challenge.title}" } return Response(response_data, status=status.HTTP_404_NOT_FOUND) @@ -478,10 +490,12 @@ def get_participant_team_details_for_challenge(request, challenge_pk): responses={ status.HTTP_200_OK: OpenApiResponse(description=""), status.HTTP_400_BAD_REQUEST: OpenApiResponse( - description="{'error': 'Team has not participated in the challenge'}" + description="{'error': 'Team has not participated " + "in the challenge'}" ), status.HTTP_401_UNAUTHORIZED: OpenApiResponse( - description="{'error': 'Sorry, you do not have permissions to remove this participant team'}" + description="{'error': 'Sorry, you do not have permissions " + "to remove this participant team'}" ), }, ) @@ -519,7 +533,8 @@ def remove_participant_team_from_challenge( ) if submissions.count() > 0: response_data = { - "error": "Unable to remove team as you have already made submission to the challenge" + "error": "Unable to remove team as you have already " + "made submission to the challenge" } return Response( response_data, status=status.HTTP_400_BAD_REQUEST @@ -534,6 +549,7 @@ def remove_participant_team_from_challenge( return Response(response_data, status=status.HTTP_400_BAD_REQUEST) else: response_data = { - "error": "Sorry, you do not have permissions to remove this participant team" + "error": "Sorry, you do not have permissions to " + "remove this participant team" } return Response(response_data, status=status.HTTP_401_UNAUTHORIZED) From 02b12228be19c9615bf2a3b0e64271561222c2cb Mon Sep 17 00:00:00 2001 From: Sekhar-Kumar-Dash <119131588+Sekhar-Kumar-Dash@users.noreply.github.com> Date: Sun, 27 Apr 2025 19:43:06 +0530 Subject: [PATCH 2/5] fixed pylint errors in utils.py --- apps/participants/utils.py | 105 +++++++++++++++++++++++++++++++------ 1 file changed, 90 insertions(+), 15 deletions(-) diff --git a/apps/participants/utils.py b/apps/participants/utils.py index 140c16574f..651d713ca0 100644 --- a/apps/participants/utils.py +++ b/apps/participants/utils.py @@ -7,7 +7,15 @@ def is_user_part_of_participant_team(user, participant_team): - """Returns boolean if the user belongs to the participant team or not""" + """Returns boolean if the user belongs to the participant team or not. + + Parameters: + user (User): User object + participant_team (ParticipantTeam): ParticipantTeam object + + Returns: + bool: True if user is part of the team, False otherwise + """ return Participant.objects.filter( user=user, team=participant_team ).exists() @@ -16,24 +24,58 @@ def is_user_part_of_participant_team(user, participant_team): def has_participant_team_participated_in_challenge( participant_team_id, challenge_id ): - """Returns boolean if the Participant Team participated in particular Challenge""" + """Returns boolean if the Participant Team + participated in particular Challenge. + + Parameters: + participant_team_id (int): ID of the participant team + challenge_id (int): ID of the challenge + + Returns: + bool: True if team participated in the challenge, False otherwise + """ return Challenge.objects.filter( - pk=challenge_id, participant_team__id=participant_team_id + pk=challenge_id, + participant_team__id=participant_team_id ).exists() def get_participant_teams_for_user(user): - """Returns participant team ids for a particular user""" - return Participant.objects.filter(user=user).values_list("team", flat=True) + """Returns participant team ids for a particular user. + + Parameters: + user (User): User object + + Returns: + QuerySet: List of team IDs the user belongs to + """ + return (Participant.objects. + filter(user=user).values_list("team", flat=True)) def is_user_creator_of_participant_team(user, participant_team): - """Returns boolean if a user is the creator of participant team""" + """Returns boolean if a user is the creator of participant team. + + Parameters: + user (User): User object + participant_team (ParticipantTeam): ParticipantTeam object + + Returns: + bool: True if user is the creator of the team, False otherwise + """ return participant_team.created_by.pk == user.pk def has_user_participated_in_challenge(user, challenge_id): - """Returns boolean if the user has participated in a particular challenge""" + """Returns boolean if the user has participated in a particular challenge. + + Parameters: + user (User): User object + challenge_id (int): ID of the challenge + + Returns: + bool: True if user participated in the challenge, False otherwise + """ participant_teams = get_participant_teams_for_user(user) return Challenge.objects.filter( pk=challenge_id, participant_teams__in=participant_teams @@ -41,33 +83,66 @@ def has_user_participated_in_challenge(user, challenge_id): def get_participant_team_id_of_user_for_a_challenge(user, challenge_id): - """Returns the participant team id for a particular user for a particular challenge""" + """Returns the participant team id for a particular + user for a particular challenge. + + Parameters: + user (User): User object + challenge_id (int): ID of the challenge + + Returns: + int or None: ID of the participant team if found, None otherwise + """ participant_teams = get_participant_teams_for_user(user) for participant_team in participant_teams: if Challenge.objects.filter( pk=challenge_id, participant_teams=participant_team ).exists(): return participant_team - return + return None def get_participant_team_of_user_for_a_challenge(user, challenge_id): - """Returns the participant team object for a particular user for a particular challenge""" - # TODO: Remove other functions and use this function + """Returns the participant team object for a particular + user for a particular challenge. + + Parameters: + user (User): User object + challenge_id (int): ID of the challenge + + Returns: + ParticipantTeam or None: Team object if found, None otherwise + """ participant_team_id = get_participant_team_id_of_user_for_a_challenge( user, challenge_id ) if participant_team_id: return ParticipantTeam.objects.get(pk=participant_team_id) - return + return None -def get_list_of_challenges_for_participant_team(participant_teams=[]): - """Returns list of challenges participated by a team""" +def get_list_of_challenges_for_participant_team(participant_teams=None): + """Returns list of challenges participated by a team. + + Parameters: + participant_teams (list, optional): List of participant team objects + + Returns: + QuerySet: Challenges the teams participated in + """ + if participant_teams is None: + participant_teams = [] return Challenge.objects.filter(participant_teams__in=participant_teams) def get_list_of_challenges_participated_by_a_user(user): - """Returns list of challenges participated by a user""" + """Returns list of challenges participated by a user. + + Parameters: + user (User): User object + + Returns: + QuerySet: Challenges the user participated in + """ participant_teams = get_participant_teams_for_user(user) return get_list_of_challenges_for_participant_team(participant_teams) From 31a487b84f690cb5c283885841303d0d1b0e1994 Mon Sep 17 00:00:00 2001 From: Sekhar-Kumar-Dash <119131588+Sekhar-Kumar-Dash@users.noreply.github.com> Date: Sun, 27 Apr 2025 19:44:31 +0530 Subject: [PATCH 3/5] fixed the pylint errors in urls.py --- apps/participants/urls.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/apps/participants/urls.py b/apps/participants/urls.py index bf447bfb36..aa648a0855 100644 --- a/apps/participants/urls.py +++ b/apps/participants/urls.py @@ -9,17 +9,21 @@ name="invite_participant_to_team", ), url( - r"^participant_team/(?P[0-9]+)/challenge$", + r"^participant_team/(?P" + r"[0-9]+)/challenge$", views.get_participant_team_challenge_list, name="get_participant_team_challenge_list", ), url( - r"^remove_self_from_participant_team/(?P[0-9]+)$", + r"^remove_self_from_participant_team/" + r"(?P[0-9]+)$", views.remove_self_from_participant_team, name="remove_self_from_participant_team", ), url( - r"^participant_team/(?P[0-9]+)/participant/(?P[0-9]+)$", + r"^participant_team/" + r"(?P[0-9]+)" + r"/participant/(?P[0-9]+)$", views.delete_participant_from_team, name="delete_participant_from_team", ), @@ -39,12 +43,14 @@ name="get_participant_team_details", ), url( - r"^challenges/(?P[0-9]+)/team_details/$", + r"^challenges/" + r"(?P[0-9]+)/team_details/$", views.get_participant_team_details_for_challenge, name="get_participant_team_details_for_challenge", ), url( - r"^challenges/(?P[0-9]+)/team/(?P[0-9]+)/remove_participant_team/$", + r"^challenges/(?P[0-9]+)/team/" + r"(?P[0-9]+)/remove_participant_team/$", views.remove_participant_team_from_challenge, name="remove_participant_team_from_challenge", ), From 7ee460afefed687c4093cac6e0692f3090cf89c1 Mon Sep 17 00:00:00 2001 From: Sekhar-Kumar-Dash <119131588+Sekhar-Kumar-Dash@users.noreply.github.com> Date: Sun, 27 Apr 2025 19:45:19 +0530 Subject: [PATCH 4/5] fixed pylint errors in serializers.py --- apps/participants/serializers.py | 79 +++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/apps/participants/serializers.py b/apps/participants/serializers.py index 323949b5dc..e2b5c23093 100644 --- a/apps/participants/serializers.py +++ b/apps/participants/serializers.py @@ -17,13 +17,14 @@ class ParticipantTeamSerializer(serializers.ModelSerializer): ) def __init__(self, *args, **kwargs): - super(ParticipantTeamSerializer, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) context = kwargs.get("context") if context: request = context.get("request") kwargs["data"]["created_by"] = request.user.username class Meta: + """Meta class for ParticipantTeamSerializer.""" model = ParticipantTeam fields = ("id", "team_name", "created_by", "team_url") @@ -34,26 +35,26 @@ class InviteParticipantToTeamSerializer(serializers.Serializer): email = serializers.EmailField() def __init__(self, *args, **kwargs): - super(InviteParticipantToTeamSerializer, self).__init__( - *args, **kwargs - ) + super().__init__(*args, **kwargs) context = kwargs.get("context") if context: self.participant_team = context.get("participant_team") self.user = context.get("request").user def validate_email(self, value): + """Validate email for participant invitation.""" if value == self.user.email: raise serializers.ValidationError( "A participant cannot invite himself" ) try: User.objects.get(email=value) - except User.DoesNotExist: - raise serializers.ValidationError("User does not exist") + except User.DoesNotExist as exc: + raise serializers.ValidationError("User does not exist") from exc return value - def save(self): + def save(self, **kwargs): + """Save method for creating participant.""" email = self.validated_data.get("email") return Participant.objects.get_or_create( user=User.objects.get(email=email), @@ -72,6 +73,7 @@ class ParticipantSerializer(serializers.ModelSerializer): email = serializers.SerializerMethodField() class Meta: + """Meta class for ParticipantSerializer.""" model = Participant fields = ( "member_name", @@ -82,19 +84,29 @@ class Meta: "profile", ) - def get_member_name(self, obj): + @staticmethod + def get_member_name(obj): + """Get username of participant.""" return obj.user.username - def get_first_name(self, obj): + @staticmethod + def get_first_name(obj): + """Get first name of participant.""" return obj.user.first_name - def get_last_name(self, obj): + @staticmethod + def get_last_name(obj): + """Get last name of participant.""" return obj.user.last_name - def get_email(self, obj): + @staticmethod + def get_email(obj): + """Get email of participant.""" return obj.user.email - def get_profile(self, obj): + @staticmethod + def get_profile(obj): + """Get profile of participant.""" user_profile = Profile.objects.get(user=obj.user) serializer = UserProfileSerializer(user_profile) return serializer.data @@ -109,34 +121,39 @@ class ParticipantTeamDetailSerializer(serializers.ModelSerializer): ) class Meta: + """Meta class for ParticipantTeamDetailSerializer.""" model = ParticipantTeam fields = ("id", "team_name", "created_by", "members", "team_url") - def get_members(self, obj): + @staticmethod + def get_members(obj): + """Get all members of a team.""" participants = Participant.objects.filter(team__pk=obj.id) serializer = ParticipantSerializer(participants, many=True) return serializer.data -class ChallengeParticipantTeam(object): +class ChallengeParticipantTeam: """Serializer to map Challenge and Participant Teams.""" def __init__(self, challenge, participant_team): + """Initialize with challenge and participant team.""" self.challenge = challenge self.participant_team = participant_team class ChallengeParticipantTeamSerializer(serializers.Serializer): - """Serializer to initialize Challenge and Participant's Team""" + """Serializer to initialize Challenge and Participant's Team.""" challenge = ChallengeSerializer() participant_team = ParticipantTeamSerializer() -class ChallengeParticipantTeamList(object): +class ChallengeParticipantTeamList: """Class to create a list of Challenge and Participant Teams.""" def __init__(self, challenge_participant_team_list): + """Initialize with challenge participant team list.""" self.challenge_participant_team_list = challenge_participant_team_list @@ -148,41 +165,57 @@ class ChallengeParticipantTeamListSerializer(serializers.Serializer): ) datetime_now = serializers.SerializerMethodField() - def get_datetime_now(self, obj): + @staticmethod + def get_datetime_now(obj): + """Get current datetime.""" return timezone.now() -class ParticipantTeamCount(object): +class ParticipantTeamCount: + """Class to hold participant team count.""" + def __init__(self, participant_team_count): + """Initialize with participant team count.""" self.participant_team_count = participant_team_count class ParticipantTeamCountSerializer(serializers.Serializer): + """Serializer for participant team count.""" participant_team_count = serializers.IntegerField() -class ParticipantCount(object): +class ParticipantCount: + """Class to hold participant count.""" + def __init__(self, participant_count): + """Initialize with participant count.""" self.participant_count = participant_count class ParticipantCountSerializer(serializers.Serializer): + """Serializer for participant count.""" participant_count = serializers.IntegerField() class ChallengeParticipantSerializer(serializers.Serializer): + """Serializer for challenge participants.""" team_name = serializers.SerializerMethodField() team_members = serializers.SerializerMethodField() team_members_email_ids = serializers.SerializerMethodField() class Meta: + """Meta class for ChallengeParticipantSerializer.""" model = Challenge fields = ("team_name", "team_members", "team_members_email_ids") - def get_team_name(self, obj): + @staticmethod + def get_team_name(obj): + """Get team name.""" return obj.team_name - def get_team_members(self, obj): + @staticmethod + def get_team_members(obj): + """Get team members' usernames.""" try: participant_team = ParticipantTeam.objects.get( team_name=obj.team_name @@ -199,7 +232,9 @@ def get_team_members(self, obj): ) ) - def get_team_members_email_ids(self, obj): + @staticmethod + def get_team_members_email_ids(obj): + """Get team members' email ids.""" try: participant_team = ParticipantTeam.objects.get( team_name=obj.team_name From 00d14ddea585dd30c2264115112631764ceda323 Mon Sep 17 00:00:00 2001 From: Sekhar-Kumar-Dash <119131588+Sekhar-Kumar-Dash@users.noreply.github.com> Date: Sun, 27 Apr 2025 19:46:35 +0530 Subject: [PATCH 5/5] fixed pylint errors in models.py --- apps/participants/models.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/apps/participants/models.py b/apps/participants/models.py index 77dfb130e1..ec853408df 100644 --- a/apps/participants/models.py +++ b/apps/participants/models.py @@ -1,12 +1,18 @@ +"""Module for participant-related models in the competition system.""" from __future__ import unicode_literals -from base.models import TimeStampedModel from django.contrib.auth.models import User from django.db import models +from base.models import TimeStampedModel + class Participant(TimeStampedModel): - """Model representing the Participant of the competition.""" + """Model representing a participant of the competition. + + This model stores information about individual participants, their + status in the competition, and their team affiliation. + """ UNKNOWN = "Unknown" SELF = "Self" @@ -34,29 +40,39 @@ class Participant(TimeStampedModel): ) def __str__(self): - return "{}".format(self.user) + return str(self.user) class Meta: + """Meta class for Participant model.""" app_label = "participants" db_table = "participant" class ParticipantTeam(TimeStampedModel): - """Model representing the Teams associated with different challenges""" + """Model representing teams associated with different challenges. + + Each team can have multiple participants and is created by a user. + """ team_name = models.CharField(max_length=100, unique=True) created_by = models.ForeignKey(User, null=True, on_delete=models.CASCADE) team_url = models.CharField(max_length=1000, default="", blank=True) def __str__(self): - return "{}".format(self.team_name) + return str(self.team_name) def get_all_participants_email(self): + """Get list of email addresses for all team participants. + + Returns: + list: List of email addresses of team participants. + """ email_ids = Participant.objects.filter(team=self).values_list( "user__email", flat=True ) return list(email_ids) class Meta: + """Meta class for ParticipantTeam model.""" app_label = "participants" db_table = "participant_team"