Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

[feature request] Adicionar sistema de pontuação para usuários #106

Merged
merged 16 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion apps/tasks/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,11 @@ def setUp(self) -> None:
self.task = Task._default_manager.create(
title="Example task",
description="Some example task",
score=200,
contest=self.contest,
output_file="Hello, World!\n",
)

self.user = User._default_manager.create(
email="[email protected]",
username="user",
Expand All @@ -210,7 +213,7 @@ def setUp(self) -> None:
author=self.user,
task=self.task,
code="print('Hello, World!')",
status=SubmissionStatus.ACCEPTED,
status=SubmissionStatus.WAITING_JUDGE,
)

self.url = reverse("tasks:detail", args=[self.task.id])
Expand Down Expand Up @@ -320,6 +323,50 @@ def test_handle_submission_with_wrong_output(self) -> None:
fetch_redirect_response=True,
)

def test_submission_with_AC_status_increases_user_score(self) -> None:
handle_submission(
bitterteriyaki marked this conversation as resolved.
Show resolved Hide resolved
self.submission.code, self.task.id, self.submission.id
)

self.submission.refresh_from_db()
self.user.refresh_from_db()

self.assertEqual(self.submission.status, SubmissionStatus.ACCEPTED)
self.assertEqual(self.user.score, self.task.score)

def test_submission_with_non_AC_status_does_not_increase_user_score(
self,
) -> None:
self.submission.code = 'print("Hello, Word")'

handle_submission.apply(
args=(self.submission.code, self.task.id, self.submission.id)
)

self.user.refresh_from_db()

self.assertEqual(self.user.score, 0)

def test_second_AC_submission_does_not_increase_user_score(self) -> None:
handle_submission.apply(
args=(self.submission.code, self.task.id, self.submission.id)
)

second_submission = Submission._default_manager.create(
author=self.user,
task=self.task,
code="print('Hello, World!')",
status=SubmissionStatus.WAITING_JUDGE,
)

handle_submission.apply(
args=(second_submission.code, self.task.id, second_submission.id)
)

self.user.refresh_from_db()

self.assertEqual(self.user.score, self.task.score)
bitterteriyaki marked this conversation as resolved.
Show resolved Hide resolved

def test_form_success_url(self) -> None:
self.assertEqual(self.view.get_success_url(), self.url)

Expand Down
19 changes: 18 additions & 1 deletion apps/tasks/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.views.generic.edit import FormMixin

from apps.submissions.forms import SubmissionForm
from apps.submissions.models import Submission
from apps.submissions.models import Submission, SubmissionStatus
from apps.tasks.models import Task
from server import celery

Expand Down Expand Up @@ -49,6 +49,23 @@ def handle_submission(code: str, task_id: int, submission_id: int) -> None:
submission.status = "AC" if output == task.output_file else "WA"
submission.save()

has_already_scored = (
Submission._default_manager.filter(
author=submission.author,
task=task,
status=SubmissionStatus.ACCEPTED,
)
.exclude(id=submission.id)
bitterteriyaki marked this conversation as resolved.
Show resolved Hide resolved
.exists()
)

if (
submission.status == SubmissionStatus.ACCEPTED
and not has_already_scored
):
submission.author.score += task.score
submission.author.save()


class DetailView(FormMixinBase, DetailViewBase):
model = Task
Expand Down
17 changes: 17 additions & 0 deletions apps/users/migrations/0002_user_score.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.7 on 2023-12-01 19:35

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("users", "0001_initial"),
]

operations = [
migrations.AddField(
model_name="user",
name="score",
field=models.IntegerField(default=0),
),
]
3 changes: 2 additions & 1 deletion apps/users/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import TYPE_CHECKING, Any, List

from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin
from django.db.models import BooleanField, CharField, EmailField
from django.db.models import BooleanField, CharField, EmailField, IntegerField

from apps.users.managers import UserManager
from core.models import TimestampedModel
Expand All @@ -19,6 +19,7 @@ class User(AbstractBaseUser, PermissionsMixin, TimestampedModel):

email = EmailField(db_index=True, max_length=256, unique=True)
username = CharField(db_index=True, max_length=128, unique=True)
score = IntegerField(default=0)

# When a user no longer wishes to use our platform, they may try to
# delete there account. That's a problem for us because the data we
Expand Down