diff --git a/codechallenges/admin.py b/codechallenges/admin.py index 59c986b..1333768 100644 --- a/codechallenges/admin.py +++ b/codechallenges/admin.py @@ -6,7 +6,14 @@ @admin.register(Question) class CustomerQuestion(admin.ModelAdmin): - list_display = ("title", "body", "answer", "release_date", "expiration_date", "difficulty") + list_display = ( + "title", + "body", + "answer", + "release_date", + "expiration_date", + "difficulty" + ) @admin.register(Submission) diff --git a/codechallenges/migrations/0004_question_difficulty.py b/codechallenges/migrations/0004_question_difficulty.py index b520026..ddffb28 100644 --- a/codechallenges/migrations/0004_question_difficulty.py +++ b/codechallenges/migrations/0004_question_difficulty.py @@ -6,13 +6,18 @@ class Migration(migrations.Migration): dependencies = [ - ('codechallenges', '0003_submission_answer'), + ("codechallenges", "0003_submission_answer"), ] operations = [ migrations.AddField( - model_name='question', - name='difficulty', - field=models.CharField(blank=True, choices=[('e', 'easy'), ('m', 'medium'), ('h', 'hard')], max_length=1, null=True), + model_name="question", + name="difficulty", + field=models.CharField( + blank=True, + choices=[("e", "easy"), ("m", "medium"), ("h", "hard")], + max_length=1, + null=True, + ), ), ] diff --git a/codechallenges/models.py b/codechallenges/models.py index da916be..8763803 100644 --- a/codechallenges/models.py +++ b/codechallenges/models.py @@ -29,7 +29,9 @@ class Question(models.Model): ("m", "medium"), ("h", "hard"), ), - max_length=1, null=True, blank=True, + max_length=1, + null=True, + blank=True, ) event = models.ForeignKey( CodeChallengeEvent, on_delete=models.CASCADE, blank=True, null=True diff --git a/codechallenges/serializers.py b/codechallenges/serializers.py index b6e8c26..05b3751 100644 --- a/codechallenges/serializers.py +++ b/codechallenges/serializers.py @@ -25,4 +25,4 @@ class Meta: class SubmissionSerializer(serializers.ModelSerializer): class Meta: model = Submission - fields = ("email", "correct", "answer", "question") + fields = ("email", "correct", "answer", "question", "attempts") diff --git a/codechallenges/tests.py b/codechallenges/tests.py index f5811b6..e6e2486 100644 --- a/codechallenges/tests.py +++ b/codechallenges/tests.py @@ -1,11 +1,60 @@ from django.test import TestCase +from rest_framework.test import APIRequestFactory from codechallenges.models import Question, Submission +from .views import SubmissionList + import datetime # Create your tests here. class SubmissionTestCase(TestCase): + def test_submission(self): + question1 = Question.objects.create( + title="Yeet", + body="Blaghjdklahgjfkl", + format="t", + answer="Yote", + release_date=datetime.date.today(), + expiration_date=datetime.date(2022, 4, 29), + difficulty="e", + ) + factory = APIRequestFactory() + + # Makes and verifies successful request (first attempt) + attempt1 = factory.post( + "/api/codechallenges/submissions/", + { + "email": "robertbabaev@cmail.carleton.ca", + "question": 1, + "answer": "Yate", + }, + format="json", + ) + response = SubmissionList(attempt1) + self.assertEquals(response.status_code, 201) + + # Verifies submission creation + submissions = list(Submission.objects.all()) + self.assertEquals(submissions[0].attempts, 1) + + # Makes and verifies successful request (second attempt) + attempt2 = factory.post( + "/api/codechallenges/submissions/", + { + "email": "robertbabaev@cmail.carleton.ca", + "question": 1, + "answer": "Yote", + }, + format="json", + ) + response = SubmissionList(attempt2) + self.assertEquals(response.status_code, 200) + + # Verifies submission update + submissions = list(Submission.objects.all()) + self.assertEquals(submissions[0].attempts, 2) + def test_unique_together(self): question1 = Question.objects.create( title="Yeet", @@ -14,6 +63,7 @@ def test_unique_together(self): answer="Yote", release_date=datetime.date.today(), expiration_date=datetime.date(2022, 4, 29), + difficulty="e", ) # Verify question 1 is created @@ -39,7 +89,7 @@ def test_unique_together(self): attempts=5, ) - # Verify submission 2 is created + # Verify submission 3 is created self.assertNotEquals(submission2, None) # This should fail due to duplicate checking diff --git a/codechallenges/views.py b/codechallenges/views.py index daf19dc..622a478 100644 --- a/codechallenges/views.py +++ b/codechallenges/views.py @@ -20,6 +20,27 @@ def SubmissionList(request): except Question.DoesNotExist: return HttpResponse(status=500) + # Need to check if submission exists before creating one + try: + submission = Submission.objects.get( + question=data["question"], email=data["email"] + ) + submission.attempts += 1 + + if question.answer == data["answer"]: + submission.correct = True + else: + submission.correct = False + + submission.answer = data["answer"] + submission.save() + + serializer = SubmissionSerializer(submission) + + return JsonResponse(serializer.data, status=200) + except Submission.DoesNotExist: + data["attempts"] = 1 + if question.answer == data["answer"]: data["correct"] = True else: