Skip to content

Commit

Permalink
added "remove" for project, closes #69
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeygrigorev committed Oct 2, 2024
1 parent 2bde53a commit 1fb5439
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 19 deletions.
57 changes: 47 additions & 10 deletions courses/templates/projects/project.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h2 class="mb-3 text-center">{{ project.title }} for <a href="{% url 'course' co

<h3 class="mb-3">Project submission</h3>

<form method="post" class="needs-validation" novalidate>
<form method="post" id="project-form" class="needs-validation" novalidate>
{% if is_authenticated %}
{% csrf_token %}
{% endif %}
Expand Down Expand Up @@ -163,16 +163,27 @@ <h3 class="mb-3">Project submission</h3>
{% if accepting_submissions %}
<div class="mt-4 text-center">
{% if is_authenticated %}
<button
{% if not submission %}
<button
id="submit-button"
type="submit"
class="btn btn-primary">
{% if not submission %}
Submit
{% else %}
Update
{% endif %}
</button>
Submit
</button>
{% else %}
<button
id="submit-button"
type="submit"
class="btn btn-primary">
Update
</button>
<button
id="delete-button"
type="button"
class="btn btn-danger">
Remove
</button>
{% endif %}
{% else %}
<p>Please <a href="{% url 'login' %}">log in</a> to submit your project.</p>
{% endif %}
Expand All @@ -181,13 +192,39 @@ <h3 class="mb-3">Project submission</h3>
</form>

{% if submission %}
<p class="text-muted question-text m-0">Last submission at: {{ submission.submitted_at|date:"F d, Y H:i" }}</p>
<p class="text-muted question-text m-0 mt-4">
Last submission at: {{ submission.submitted_at|date:"F d, Y H:i" }}
</p>
{% endif %}

<script>
$(function () {
$(function() {
$('[data-toggle="tooltip"]').tooltip()
});

$(document).ready(function() {
const $form = $('#project-form');
const $updateButton = $('#update-button');
const $deleteButton = $('#delete-button');

$updateButton.on('click', function(e) {
e.preventDefault();
$form.submit();
});

$deleteButton.on('click', function(e) {
e.preventDefault();
if (confirm('Are you sure you want to delete your project submission?')) {
$('<input>').attr({
type: 'hidden',
name: 'action',
value: 'delete'
}).appendTo($form);

$form.submit();
}
});
});
</script>

<script src="{% static 'homework.js' %}"></script>
Expand Down
46 changes: 46 additions & 0 deletions courses/tests/test_project_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,52 @@ def test_project_submission_post_with_submissions(self):
submission.faq_contribution, data["faq_contribution"]
)

def test_remove_project_submission(self):
self.client.login(**credentials)

# Create an initial submission
ProjectSubmission.objects.create(
project=self.project,
student=self.user,
enrollment=self.enrollment,
github_link="https://httpbin.org/status/200",
commit_id="123456a",
)

count_sumissions = ProjectSubmission.objects.filter(
student=self.user,
project=self.project,
enrollment=self.enrollment,
).count()

self.assertEqual(count_sumissions, 1)


url = reverse(
"project", args=[self.course.slug, self.project.slug]
)

data = {
"github_link": "https://httpbin.org/status/200",
"commit_id": "123456e",
"time_spent": "3",
"problems_comments": "No issues encountered.",
"faq_contribution": "Helped a peer with their problem.",
"action": "delete"
}

response = self.client.post(url, data)
self.assertEqual(response.status_code, 302)

count_sumissions = ProjectSubmission.objects.filter(
student=self.user,
project=self.project,
enrollment=self.enrollment,
).count()

self.assertEqual(count_sumissions, 0)


# this test requires a redesing of the project view
# skipping for now
# def test_submission_exist_post_with_error(self):
Expand Down
39 changes: 30 additions & 9 deletions courses/views/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,24 @@ def project_submit_post(request: HttpRequest, project: Project) -> None:
)


def project_delete_submission(request: HttpRequest, project: Project) -> None:
user = request.user

project_submission = ProjectSubmission.objects.filter(
project=project, student=request.user
).first()

if project_submission:
project_submission.delete()

messages.success(
request,
"Your project submission is deleted. You can still make a new submission if you want.",
extra_tags="homework",
)



def project_view(request, course_slug, project_slug):
course = get_object_or_404(Course, slug=course_slug)
project = get_object_or_404(
Expand Down Expand Up @@ -122,15 +140,18 @@ def project_view(request, course_slug, project_slug):
project_slug=project.slug,
)

try:
project_submit_post(request, project)
except ValidationError as e:
for message in e.messages:
messages.error(
request,
f"Failed to submit the project: {message}",
extra_tags="alert-danger",
)
if 'action' in request.POST and request.POST['action'] == 'delete':
project_delete_submission(request, project)
else:
try:
project_submit_post(request, project)
except ValidationError as e:
for message in e.messages:
messages.error(
request,
f"Failed to submit the project: {message}",
extra_tags="alert-danger",
)

return redirect(
"project",
Expand Down

0 comments on commit 1fb5439

Please sign in to comment.