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

Edit submission metadata #186

Merged
merged 13 commits into from
Nov 6, 2024
1 change: 1 addition & 0 deletions frx_challenges/frx_challenges/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@
"type": "string",
"title": "Repository",
"helpText": "Link to repository containing code and stuff",
"format": "uri",
}
},
}
Expand Down
9 changes: 7 additions & 2 deletions frx_challenges/web/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
from django.urls import reverse
from django.utils.safestring import mark_safe
from django_jsonform.forms.fields import JSONFormField
from web.models import Submission

from .md import MARKDOWN_RENDERER


class SubmissionForm(forms.Form):
class SubmissionForm(forms.ModelForm):
"""Form to create a new submission"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

self.helper = FormHelper()
self.helper.form_method = "post"
self.helper.form_action = "submissions-create"
self.helper.add_input(
Submit("submit", "Submit", css_class="form-control btn btn-secondary")
)

self.fields["name"] = forms.CharField()
self.fields["name"].label = "Submission name"
self.fields["description"] = forms.CharField(required=False)
self.fields["metadata"] = JSONFormField(
schema=settings.SITE_SUBMISSION_FORM_SCHEMA
Expand All @@ -34,6 +35,10 @@ def __init__(self, *args, **kwargs):
required=True,
)

class Meta:
model = Submission
fields = ["name", "description", "metadata", "toc_accepted"]


class UploadForm(forms.Form):
"""Form to upload a version of a submission to be evaluated"""
Expand Down
2 changes: 1 addition & 1 deletion frx_challenges/web/templates/results.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ <h1>Explanatory Headline</h1>
const resultsTable = new DataTable("#results", {
order: [
// Apply reverse chronological ordering by default
[3, "desc"]
[3, "asc"]
],
columnDefs: [
{
Expand Down
2 changes: 2 additions & 0 deletions frx_challenges/web/templates/submission/create.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<div class="row py-2">
<div class="col py-2">
<h1>Create a submission</h1>
<br>
{{ html_content|safe }}
</div>
</div>
<div class="row py-2">
Expand Down
20 changes: 16 additions & 4 deletions frx_challenges/web/templates/submission/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@
<div class="container py-2">
<div class="row py-2">
<div class="col py-2">
<h1>{{ submission.name }}</h1>
{% if submission.description %}<p>{{ submission.description }}</p>{% endif %}
<div class="row py-2">
<div class="col-8">
<h1>{{ submission.name }}</h1>
{% if submission.description %}<p>{{ submission.description }}</p>{% endif %}
</div>
<div class="col-4 text-end">
<a href="{% url 'submissions-edit' submission.id %}"
class="btn btn-secondary mt-2 float-end">Edit submission</a>
</div>
</div>
<div class="row py-2">
{% for md in metadata_display %}
<div class="col">
<b class="d-inline">{{ md.display_name }}</b>
{% if md.value %}
{% if md.value and md.format == 'uri' %}
<p>
<a href={{ md.value }}>{{ md.value }}</a>
</p>
{% elif md.value %}
<p>{{ md.value }}</p>
{% else %}
<p>–</p>
Expand Down Expand Up @@ -74,7 +86,7 @@ <h1>{{ submission.name }}</h1>
const resultsTable = new DataTable("#results", {
order: [
// Apply reverse chronological ordering by default
[2, "desc"]
[2, "asc"]
],
columnDefs: [
{
Expand Down
17 changes: 17 additions & 0 deletions frx_challenges/web/templates/submission/edit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% extends "page.html" %}
{% load static %}
{% load crispy_forms_tags %}
{% block body %}
<div class="container py-2">
<div class="row py-2">
<div class="col py-2">
<h1>Edit a submission</h1>
<br>
{{ html_content|safe }}
</div>
</div>
<div class="row py-2">
<div class="col py-2">{% crispy form %}</div>
</div>
</div>
{% endblock body %}
19 changes: 8 additions & 11 deletions frx_challenges/web/templates/submission/list.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,23 @@
{% block body %}
<div class="container py-2">
<div class="row py-2">
<div class="col py-2">
<div class="col-8 py-2">
<h1>My Submissions</h1>
</div>
<div class="col-4">
<a href="{% url 'submissions-create' %}"
class="btn btn-secondary mt-2 float-end">Create submission</a>
</div>
</div>
<div class="row">
<div class="col">
<p>
Create, view and edit submissions. Each submission can contain multiple entries. Click on the submission name to view details.
</p>
<p>Create, view and edit submissions. Each submission can contain multiple entries.</p>
<p>Click on the submission name to view details.</p>
</div>
</div>
</div>
<div class="container py-2">
{% if submissions %}
<div class="row py-2">
<div class="col">
<a href="{% url 'submissions-create' %}"
class="btn btn-secondary mt-2 float-end">Create submission</a>
</div>
</div>
<div class="row py-2">
<table id="results" class="table table-striped table-sm">
<thead>
Expand Down Expand Up @@ -80,7 +77,7 @@ <h1>My Submissions</h1>
const resultsTable = new DataTable("#results", {
order: [
// Apply reverse chronological ordering by default
[3, "desc"]
[3, "asc"]
],
columnDefs: [
{
Expand Down
1 change: 1 addition & 0 deletions frx_challenges/web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
path("submissions/", submissions.list, name="submissions-list"),
path("submissions/create", submissions.create, name="submissions-create"),
path("submissions/<int:id>", submissions.detail, name="submissions-detail"),
path("submissions/<int:id>/edit", submissions.edit, name="submissions-edit"),
path(
"evaluation/<int:id>", submissions.detail_evaluation, name="evaluation-detail"
),
Expand Down
34 changes: 34 additions & 0 deletions frx_challenges/web/views/submissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def detail(request: HttpRequest, id: int) -> HttpResponse:
"display_name": v["title"],
"help_string": v.get("helpText"),
"value": submission.metadata.get(k),
"format": v["format"],
}
)
else:
Expand All @@ -84,6 +85,39 @@ def detail(request: HttpRequest, id: int) -> HttpResponse:
)


@login_required
def edit(request: HttpRequest, id: int) -> HttpResponse:
"""
Edit submission metadata
"""

html_content = MARKDOWN_RENDERER.render(
settings.SITE_SUBMISSION_INSTRUCTIONS_MARKDOWN
)

# Get model instance to pre-populate form
submission = Submission.objects.get(id=id)

if request.method == "POST":
form = SubmissionForm(request.POST, instance=submission)
if form.is_valid():
submission.user = request.user
submission.name = form.cleaned_data["name"]
submission.description = form.cleaned_data["description"]
submission.metadata = form.cleaned_data["metadata"]
submission.toc_accepted = form.cleaned_data["toc_accepted"]
submission.save()
return HttpResponseRedirect(
reverse("submissions-detail", args=[submission.id])
)
else:
form = SubmissionForm(instance=submission)

return render(
request, "submission/edit.html", {"form": form, "html_content": html_content}
)


@login_required
def detail_evaluation(request: HttpRequest, id: int) -> HttpResponse:
"""
Expand Down