Skip to content

Commit

Permalink
add: queue and region
Browse files Browse the repository at this point in the history
  • Loading branch information
meua committed Jan 18, 2024
1 parent 618ebee commit 256df6e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
8 changes: 5 additions & 3 deletions apps/challenges/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ def __init__(self, *args, **kwargs):
default="",
verbose_name="SQS queue name",
db_index=True,
null=False,
blank=False
)
is_docker_based = models.BooleanField(
default=False, verbose_name="Is Docker Based", db_index=True
Expand Down Expand Up @@ -154,10 +156,10 @@ def __init__(self, *args, **kwargs):
max_length=50, default="us-east-1", null=True, blank=True
)
queue_aws_region = models.CharField(
max_length=50, default="us-east-1", null=True, blank=True
max_length=50, default="us-east-1", null=False, blank=False
)
use_host_credentials = models.BooleanField(default=False)
use_host_sqs = models.BooleanField(default=False)
use_host_credentials = models.BooleanField(default=True)
use_host_sqs = models.BooleanField(default=True)
allow_resuming_submissions = models.BooleanField(default=False)
allow_host_cancel_submissions = models.BooleanField(default=False)
allow_cancel_running_submissions = models.BooleanField(default=False)
Expand Down
3 changes: 3 additions & 0 deletions apps/challenges/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class Meta:
"workers",
"created_at",
"queue",
"queue_aws_region",
"worker_cpu_cores",
"worker_memory",
"cpu_only_jobs",
Expand Down Expand Up @@ -288,6 +289,8 @@ class Meta:
"allow_cancel_running_submissions",
"allow_participants_resubmissions",
"is_docker_based",
"queue",
"queue_aws_region",
"is_static_dataset_code_upload",
"slug",
"max_docker_image_size",
Expand Down
50 changes: 32 additions & 18 deletions apps/challenges/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4430,7 +4430,6 @@ def create_or_update_github_challenge(request, challenge_host_team_pk):
@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
@authentication_classes((JWTAuthentication, ExpiringTokenAuthentication))
def create_or_update_challenge(request, challenge_host_team_pk):
print(f"request:{request}, challenge_host_team_pk:{challenge_host_team_pk}")
try:
challenge_host_team = ChallengeHostTeam.objects.get(
pk=challenge_host_team_pk
Expand All @@ -4446,6 +4445,11 @@ def create_or_update_challenge(request, challenge_host_team_pk):
"error": "Sorry, you do not belong to this Host Team!"
}
return Response(response_data, status=status.HTTP_401_UNAUTHORIZED)
queue = request.data.get("queue")
queue_aws_region = request.data.get("queue_aws_region")
if not queue or not queue_aws_region:
response_data = {"error": "queue and queue_aws_region cannot be empty"}
return Response(response_data, status=status.HTTP_400_BAD_REQUEST)

challenge_pk = request.data.get("id")
if not challenge_pk:
Expand All @@ -4468,11 +4472,6 @@ def create_or_update_challenge(request, challenge_host_team_pk):
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
serializer.save()
challenge = serializer.instance
queue_name = get_queue_name(challenge.title, challenge.pk)
challenge.queue = queue_name
challenge.save()

challenge = get_challenge_model(serializer.instance.pk)
serializer = ChallengeSerializer(challenge)
response_data = serializer.data
Expand All @@ -4491,14 +4490,30 @@ def create_or_update_challenge(request, challenge_host_team_pk):
"error": "The challenge title already exists. Please choose a different title."
}
return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
challenge.short_description = request.data.get("short_description")
challenge.description = request.data.get("description")
challenge.evaluation_details = request.data.get("evaluation_details")
challenge.terms_and_conditions = request.data.get("terms_and_conditions")
challenge.submission_guidelines = request.data.get("submission_guidelines")
challenge.leaderboard_description = request.data.get("leaderboard_description")
challenge.start_date = datetime.strptime(request.data.get("start_date"), "%Y-%m-%dT%H:%M:%S%z")
challenge.end_date = datetime.strptime(request.data.get("end_date"), "%Y-%m-%dT%H:%M:%S%z")
challenge_fields = [
"short_description",
"description",
"evaluation_details",
"terms_and_conditions",
"submission_guidelines",
"leaderboard_description",
"queue",
"queue_aws_region",
]
for field in challenge_fields:
field_value = request.data.get(field)
if field_value is not None:
setattr(challenge, field, field_value)

start_date_str = request.data.get("start_date")
end_date_str = request.data.get("end_date")
try:
challenge.start_date = datetime.strptime(start_date_str, "%Y-%m-%dT%H:%M:%S%z")
challenge.end_date = datetime.strptime(end_date_str, "%Y-%m-%dT%H:%M:%S%z")
except ValueError:
response_data = {"error": "Invalid date format"}
return Response(response_data, status=status.HTTP_400_BAD_REQUEST)

image = request.data.get("image")
if isinstance(image, str):
challenge.image = simple_image_url(image)
Expand All @@ -4512,11 +4527,10 @@ def create_or_update_challenge(request, challenge_host_team_pk):
challenge.approved_by_admin = True
try:
challenge.save()
except Exception as e: # noqa: E722
except Exception as e:
return Response({"error": str(e)}, status=status.HTTP_400_BAD_REQUEST)
response_data = {
"message": "Challenge updated successfully!"
}

response_data = {"message": "Challenge updated successfully!"}
return Response(response_data, status=status.HTTP_200_OK)


Expand Down
5 changes: 5 additions & 0 deletions apps/participants/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
views.get_teams_and_corresponding_challenges_for_a_participant,
name="get_teams_and_corresponding_challenges_for_a_participant",
),
url(
r"^participant_teams/user$",
views.get_teams_and_corresponding_challenges_by_user,
name="get_teams_and_corresponding_challenges_by_user",
),
url(
r"^participant_team$",
views.participant_team_list,
Expand Down
27 changes: 27 additions & 0 deletions apps/participants/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,33 @@ def get_teams_and_corresponding_challenges_for_a_participant(
return Response(response_data, status=status.HTTP_200_OK)


@api_view(["GET"])
@throttle_classes([UserRateThrottle])
@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
@authentication_classes((JWTAuthentication, ExpiringTokenAuthentication))
def get_teams_and_corresponding_challenges_by_user(request):
"""
Returns list of teams and corresponding challenges for a participant
"""
# first get list of all the participants and teams related to the user
participant_objs = Participant.objects.filter(
user=request.user
).prefetch_related("team")

challenge_set = []
for participant_obj in participant_objs:
participant_team = participant_obj.team

challenges = Challenge.objects.filter(
participant_teams=participant_team
)

if challenges.count():
for challenge in challenges:
challenge_set.append(challenge.id)
return Response(challenge_set, status=status.HTTP_200_OK)


@api_view(["DELETE"])
@throttle_classes([UserRateThrottle])
@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
Expand Down

0 comments on commit 256df6e

Please sign in to comment.