Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Environment URL field in Phase model #2392

Merged
merged 10 commits into from
Dec 23, 2019
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.11.20 on 2019-07-17 09:25
from __future__ import unicode_literals

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('challenges', '0057_add_task_def_arn_and_workers_field_to_challenge_model'),
]

operations = [
migrations.AddField(
model_name='challengephase',
name='environment_url',
field=models.CharField(max_length=2128, null=True, validators=[django.core.validators.URLValidator()]),
),
]
2 changes: 2 additions & 0 deletions apps/challenges/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import unicode_literals

from django.contrib.auth.models import User
from django.core.validators import URLValidator
from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.utils import timezone
Expand Down Expand Up @@ -232,6 +233,7 @@ def __init__(self, *args, **kwargs):
null=True,
)
slug = models.SlugField(max_length=200, null=True, unique=True)
environment_url = models.CharField(validators=[URLValidator()], null=True, max_length=2128) # Max length of URL and tag is 2000 and 128 respectively

class Meta:
app_label = "challenges"
Expand Down
5 changes: 5 additions & 0 deletions apps/challenges/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,9 @@
views.get_challenge_phase_by_slug,
name="get_challenge_phase_by_slug",
),
url(
r"^phase/environment/(?P<slug>[\w-]+)/$",
views.get_challenge_phase_environment_url,
name="get_challenge_phase_environment_url",
),
]
32 changes: 32 additions & 0 deletions apps/challenges/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2171,3 +2171,35 @@ def get_challenge_phase_by_slug(request, slug):
serializer = ChallengePhaseSerializer(challenge_phase)
response_data = serializer.data
return Response(response_data, status=status.HTTP_200_OK)


@api_view(["GET"])
@throttle_classes([UserRateThrottle])
@permission_classes((permissions.IsAuthenticated, HasVerifiedEmail))
@authentication_classes((ExpiringTokenAuthentication,))
def get_challenge_phase_environment_url(request, slug):
"""
Returns environment image url and tag required for RL challenge evaluation
"""
try:
challenge_phase = ChallengePhase.objects.get(slug=slug)
except ChallengePhase.DoesNotExist:
response_data = {
"error": "Challenge phase with slug {} does not exist".format(slug)
}
return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
challenge = get_challenge_model(challenge_phase.challenge.pk)
if not is_user_a_host_of_challenge(request.user, challenge.pk):
response_data = {
"error": "Sorry, you are not authorized to access this."
}
return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
if not challenge.is_docker_based:
response_data = {
"error": "Invalid resource"
}
return Response(response_data, status=status.HTTP_400_BAD_REQUEST)
response_data = {
"environment_url": challenge_phase.environment_url
}
return Response(response_data, status=status.HTTP_200_OK)
4 changes: 4 additions & 0 deletions scripts/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,15 @@ def create_challenge_phases(challenge, number_of_phases=1):
challenge_phases = []
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please open up a new PR with the changes in this file?

Copy link
Member Author

@krtkvrm krtkvrm Dec 23, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done: #2533

for i in range(number_of_phases):
name = "{} Phase".format(fake.first_name())
year = datetime.date.today().year
slug = '{t}-{y}'.format(t=name, y=year)
slug = slug.lower().replace(" ", "-")
with open(os.path.join(settings.BASE_DIR, 'examples', 'example1', 'test_annotation.txt'), 'rb') as data_file:
data = data_file.read()
data = data or None
challenge_phase = ChallengePhase.objects.create(
name=name,
slug=slug,
description=fake.paragraph(),
leaderboard_public=True,
is_public=True,
Expand Down