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

Public message in Submission view #412

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 9 additions & 3 deletions oioioi/base/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ def add_message(self):
self.assertEqual(response.status_code, 302)

# Check if message is visible
self.assertTrue(self.client.login(username='test_user2'))
self.assertTrue(self.client.login(username='test_user'))
url = reverse(self.viewname, kwargs=viewname_kwargs)
response = self.client.get(url)
self.assertContains(response, 'Test public message')
Expand All @@ -1623,7 +1623,7 @@ def contest_controller(self):
contest.save()
viewname_kwargs = getattr(self, 'viewname_kwargs', {'contest_id': contest.id})

self.assertTrue(self.client.login(username='test_user2'))
self.assertTrue(self.client.login(username='test_user'))
url = reverse(self.viewname, kwargs=viewname_kwargs)
response = self.client.get(url)
self.assertContains(response, 'Test public message')
Expand All @@ -1638,19 +1638,25 @@ def button_visibility(self):
response = self.client.get(url)
self.assertContains(response, edit_url)

self.assertTrue(self.client.login(username='test_user2'))
self.assertTrue(self.client.login(username='test_user'))
response = self.client.get(url)
self.assertNotContains(response, edit_url)


def test_add_message(self):
if hasattr(self, 'model'):
self.add_message()
else:
self.skipTest("model not defined")

def test_contest_controller(self):
if hasattr(self, 'controller_name'):
self.contest_controller()
else:
self.skipTest("controller_name not defined")

def test_button_visibility(self):
if hasattr(self, 'button_viewname') and hasattr(self, 'edit_viewname'):
self.button_visibility()
else:
self.skipTest("button_viewname or edit_viewname not defined")
2 changes: 2 additions & 0 deletions oioioi/base/utils/public_message.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
def get_public_message(request, model, controller_default_attribute=None):
if controller_default_attribute is None:
controller_default_attribute = ''
if not hasattr(request, 'contest') or request.contest is None:
return None
contest = request.contest
if not model.objects.filter(contest=contest).exists():
content = getattr(contest.controller, controller_default_attribute, '')
Expand Down
6 changes: 6 additions & 0 deletions oioioi/contests/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,3 +390,9 @@ class SubmitMessageForm(PublicMessageForm):
class Meta(object):
model = SubmitMessage
fields = ['content']


class SubmissionMessageForm(PublicMessageForm):
class Meta(object):
model = SubmitMessage
fields = ['content']
25 changes: 25 additions & 0 deletions oioioi/contests/migrations/0019_submissionmessage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.13 on 2024-10-23 14:53

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('contests', '0018_contest_show_contest_rules'),
]

operations = [
migrations.CreateModel(
name='SubmissionMessage',
fields=[
('contest', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='contests.contest')),
('content', models.TextField(blank=True, verbose_name='message')),
],
options={
'verbose_name': 'submission message',
'verbose_name_plural': 'submission messages',
},
),
]
8 changes: 8 additions & 0 deletions oioioi/contests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -772,3 +772,11 @@ class SubmitMessage(models.Model):
class Meta(object):
verbose_name = _("new submission message")
verbose_name_plural = _("new submission messages")

class SubmissionMessage(models.Model):
contest = models.OneToOneField(Contest, primary_key=True, on_delete=models.CASCADE)
content = models.TextField(verbose_name=_("message"), blank=True)

class Meta(object):
verbose_name = _("submission message")
verbose_name_plural = _("submission messages")
8 changes: 7 additions & 1 deletion oioioi/contests/templates/contests/my_submissions.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
class="btn btn-sm btn-outline-secondary"
href="{% url 'edit_submissions_message' contest_id=contest.id %}">
<i class="fa-solid fa-pencil"></i>
{% trans "Edit submissions message" %}
{% trans "Edit \"my submissions\" message" %}
</a>
<a role="button"
class="btn btn-sm btn-outline-secondary"
href="{% url 'edit_submission_message' contest_id=contest.id %}">
<i class="fa-solid fa-pencil"></i>
{% trans "Edit submission message" %}
</a>
{% endif %}
<a role="button"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
</div>
<h1 class="float-left">{% trans "Submission" %} {{ submission.submission.id }}</h1>
</div>
{% include "public_message/show.html" %}
{% endblock %}

{% block submission_details %}
Expand Down
23 changes: 22 additions & 1 deletion oioioi/contests/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
FilesMessage,
SubmissionsMessage,
SubmitMessage,
SubmissionMessage,
)
from oioioi.contests.scores import IntegerScore, ScoreValue
from oioioi.contests.tests import make_empty_contest_formset
Expand Down Expand Up @@ -4079,6 +4080,7 @@ class PublicMessageContestController(ProgrammingContestController):
files_message = 'Test public message'
submissions_message = 'Test public message'
submit_message = 'Test public message'
submission_message = 'Test public message'


class TestFilesMessage(TestPublicMessage):
Expand Down Expand Up @@ -4111,6 +4113,26 @@ class TestSubmitMessage(TestPublicMessage):
controller_name = 'oioioi.contests.tests.tests.PublicMessageContestController'


class TestSubmissionMessage(TestPublicMessage):
fixtures = [
'test_users',
'test_contest',
'test_full_package',
'test_problem_instance',
'test_submission',
]
model = SubmissionMessage
button_viewname = 'my_submissions'
edit_viewname = 'edit_submission_message'
viewname = 'submission'
controller_name = 'oioioi.contests.tests.tests.PublicMessageContestController'

def setUp(self):
super().setUp()
contest = Contest.objects.get()
submission = Submission.objects.get()
self.viewname_kwargs = {'contest_id': contest.id, 'submission_id': submission.id}


class TestContestArchived(TestCase):
fixtures = [
Expand Down Expand Up @@ -4199,4 +4221,3 @@ def test_score_badge(self):
self.assertIn('badge-success', self._get_badge_for_problem(response.content, 'zad1'))
self.assertIn('badge-warning', self._get_badge_for_problem(response.content, 'zad2'))
self.assertIn('badge-danger', self._get_badge_for_problem(response.content, 'zad3'))

5 changes: 5 additions & 0 deletions oioioi/contests/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ def glob_namespaced_patterns(namespace):
views.edit_submissions_message_view,
name='edit_submissions_message',
),
re_path(
r'^submission_edit_message/$',
views.edit_submission_message_view,
name='edit_submission_message',
),
re_path(r'^files/$', views.contest_files_view, name='contest_files'),
re_path(
r'^files_edit_message/$',
Expand Down
9 changes: 9 additions & 0 deletions oioioi/contests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
FilesMessage,
SubmissionsMessage,
SubmitMessage,
SubmissionMessage,
)


Expand Down Expand Up @@ -600,6 +601,14 @@ def get_submit_message(request):
)


def get_submission_message(request):
return get_public_message(
request,
SubmissionMessage,
'submission_message',
)


@make_request_condition
@request_cached
def is_contest_archived(request):
Expand Down
19 changes: 19 additions & 0 deletions oioioi/contests/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
FilesMessageForm,
SubmissionsMessageForm,
SubmitMessageForm,
SubmissionMessageForm,
)
from oioioi.contests.models import (
Contest,
Expand Down Expand Up @@ -62,6 +63,7 @@
get_results_visibility,
are_rules_visible,
get_scoring_desription,
get_submission_message,
)
from oioioi.filetracker.utils import stream_file
from oioioi.problems.models import ProblemAttachment, ProblemStatement
Expand Down Expand Up @@ -381,6 +383,23 @@ def edit_submissions_message_view(request):
)


@enforce_condition(contest_exists & is_contest_basicadmin)
def edit_submission_message_view(request):
instance = get_submission_message(request)
if request.method == 'POST':
form = SubmissionMessageForm(request, request.POST, instance=instance)
if form.is_valid():
form.save()
return redirect('my_submissions', contest_id=request.contest.id)
else:
form = SubmissionMessageForm(request, instance=instance)
return TemplateResponse(
request,
'public_message/edit.html',
{'form': form, 'title': _("Edit submission message")},
)


@enforce_condition(not_anonymous)
def all_submissions_view(request):
submissions = []
Expand Down
2 changes: 2 additions & 0 deletions oioioi/programs/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
is_contest_admin,
is_contest_basicadmin,
is_contest_observer,
get_submission_message,
)
from oioioi.evalmgr.tasks import (
add_before_placeholder,
Expand Down Expand Up @@ -702,6 +703,7 @@ def render_submission(self, request, submission):
submission
),
'can_admin': can_admin,
'message': get_submission_message(request),
},
)

Expand Down
3 changes: 2 additions & 1 deletion oioioi/quizzes/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from oioioi.contests.controllers import submission_template_context
from oioioi.contests.models import ScoreReport, Submission, SubmissionReport
from oioioi.contests.utils import is_contest_basicadmin
from oioioi.contests.utils import is_contest_basicadmin, get_submission_message
from oioioi.problems.controllers import ProblemController
from oioioi.problems.utils import can_admin_problem_instance
from oioioi.programs.controllers import ContestController
Expand Down Expand Up @@ -259,6 +259,7 @@ def render_submission(self, request, submission):
submission
),
'can_admin': can_admin,
'message': get_submission_message(submission),
}
return render_to_string(
'quizzes/submission_header.html', request=request, context=context
Expand Down
2 changes: 2 additions & 0 deletions oioioi/testrun/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from oioioi.base.utils.archive import Archive
from oioioi.contests.controllers import submission_template_context
from oioioi.contests.models import ScoreReport, Submission, SubmissionReport
from oioioi.contests.utils import get_submission_message
from oioioi.evalmgr.tasks import extend_after_placeholder
from oioioi.problems.utils import can_admin_problem
from oioioi.programs.controllers import (
Expand Down Expand Up @@ -283,6 +284,7 @@ def render_submission(self, request, submission):
'submission': submission_template_context(request, sbm_testrun),
'supported_extra_args': self.get_supported_extra_args(submission),
'input_is_zip': is_zipfile(sbm_testrun.input_file.read_using_cache()),
'message': get_submission_message(submission),
},
)

Expand Down
Loading