Skip to content
Open
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
17 changes: 17 additions & 0 deletions web/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from .models import (
Achievement,
Assignment,
AssignmentSubmission,
Badge,
BlogComment,
BlogPost,
Expand Down Expand Up @@ -610,6 +612,21 @@ class ChallengeSubmissionAdmin(admin.ModelAdmin):

# Unregister the default User admin and register our custom one
admin.site.unregister(User)
@admin.register(Assignment)
class AssignmentAdmin(admin.ModelAdmin):
list_display = ("title", "course", "status", "due_date", "max_score", "created_at")
list_filter = ("status", "course")
search_fields = ("title", "course__title")


@admin.register(AssignmentSubmission)
class AssignmentSubmissionAdmin(admin.ModelAdmin):
list_display = ("student", "assignment", "status", "score", "submitted_at")
list_filter = ("status",)
search_fields = ("student__username", "assignment__title")
Comment thread
coderabbitai[bot] marked this conversation as resolved.
raw_id_fields = ("student", "assignment", "graded_by")


admin.site.register(User, CustomUserAdmin)


Expand Down
93 changes: 93 additions & 0 deletions web/migrations/0064_add_assignment_and_submission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# Generated by Django 5.1.15 on 2026-03-21 19:19

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


class Migration(migrations.Migration):

dependencies = [
("web", "0063_virtualclassroom_virtualclassroomcustomization_and_more"),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name="Assignment",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("title", models.CharField(max_length=200)),
("description", models.TextField()),
("due_date", models.DateTimeField(blank=True, null=True)),
("max_score", models.PositiveIntegerField(default=100)),
(
"status",
models.CharField(
choices=[("draft", "Draft"), ("published", "Published")], default="draft", max_length=10
),
),
("allow_late_submissions", models.BooleanField(default=False)),
("created_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
(
"course",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, related_name="assignments", to="web.course"
),
),
],
options={
"ordering": ["-created_at"],
},
),
migrations.CreateModel(
name="AssignmentSubmission",
fields=[
("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
("text_response", models.TextField(blank=True)),
("file_submission", models.FileField(blank=True, null=True, upload_to="assignment_submissions/")),
(
"status",
models.CharField(
choices=[("submitted", "Submitted"), ("graded", "Graded"), ("returned", "Returned")],
default="submitted",
max_length=10,
),
),
("score", models.PositiveIntegerField(blank=True, null=True)),
("feedback", models.TextField(blank=True)),
("submitted_at", models.DateTimeField(auto_now_add=True)),
("updated_at", models.DateTimeField(auto_now=True)),
("graded_at", models.DateTimeField(blank=True, null=True)),
(
"assignment",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, related_name="submissions", to="web.assignment"
),
),
(
"graded_by",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="graded_submissions",
to=settings.AUTH_USER_MODEL,
),
),
(
"student",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="assignment_submissions",
to=settings.AUTH_USER_MODEL,
),
),
],
options={
"ordering": ["-submitted_at"],
"unique_together": {("assignment", "student")},
},
),
]
89 changes: 89 additions & 0 deletions web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3176,3 +3176,92 @@ class Meta:
ordering = ["-last_updated"]
verbose_name = "Virtual Classroom Whiteboard"
verbose_name_plural = "Virtual Classroom Whiteboards"


class Assignment(models.Model):
"""A teacher-created assignment for a course."""

STATUS_CHOICES = [
("draft", "Draft"),
("published", "Published"),
]

course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name="assignments")
title = models.CharField(max_length=200)
description = models.TextField()
due_date = models.DateTimeField(null=True, blank=True)
max_score = models.PositiveIntegerField(default=100, validators=[MinValueValidator(1)])
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default="draft")
allow_late_submissions = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)

class Meta:
ordering = ["-created_at"]

def __str__(self) -> str:
return f"{self.title} ({self.course.title})"

@property
def is_past_due(self) -> bool:
if self.due_date:
return timezone.now() > self.due_date
return False
Comment thread
coderabbitai[bot] marked this conversation as resolved.

@property
def submission_count(self) -> int:
return self.submissions.count()
Comment thread
coderabbitai[bot] marked this conversation as resolved.


class AssignmentSubmission(models.Model):
"""A student submission for an assignment."""

STATUS_CHOICES = [
("submitted", "Submitted"),
("graded", "Graded"),
("returned", "Returned"),
]

assignment = models.ForeignKey(Assignment, on_delete=models.CASCADE, related_name="submissions")
student = models.ForeignKey(User, on_delete=models.CASCADE, related_name="assignment_submissions")
text_response = models.TextField(blank=True)
file_submission = models.FileField(upload_to="assignment_submissions/", blank=True, null=True)
Comment thread
ayesha1145 marked this conversation as resolved.
status = models.CharField(max_length=10, choices=STATUS_CHOICES, default="submitted")
score = models.PositiveIntegerField(null=True, blank=True)
Comment thread
ayesha1145 marked this conversation as resolved.
feedback = models.TextField(blank=True)
Comment thread
ayesha1145 marked this conversation as resolved.
submitted_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
graded_at = models.DateTimeField(null=True, blank=True)
graded_by = models.ForeignKey(
User,
on_delete=models.SET_NULL,
null=True,
blank=True,
related_name="graded_submissions",
)

class Meta:
unique_together = ["assignment", "student"]
ordering = ["-submitted_at"]
Comment thread
ayesha1145 marked this conversation as resolved.

def __str__(self) -> str:
return f"{self.student.username} - {self.assignment.title}"

@property
def percentage(self) -> "int | None":
if self.score is not None and self.assignment.max_score > 0:
return round((self.score / self.assignment.max_score) * 100)
return None
Comment thread
coderabbitai[bot] marked this conversation as resolved.



from django.db.models.signals import post_delete as _post_delete


def _delete_submission_file(sender, instance, **kwargs):
"""Delete file from storage when AssignmentSubmission is deleted."""
if instance.file_submission:
instance.file_submission.delete(save=False)


_post_delete.connect(_delete_submission_file, sender=AssignmentSubmission)
24 changes: 24 additions & 0 deletions web/templates/courses/assignment_confirm_delete.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{% extends "base.html" %}
{% block title %}Delete Assignment - {{ assignment.title }}{% endblock title %}
{% block content %}
<div class="container mx-auto px-4 py-8 max-w-lg">
<div class="bg-white dark:bg-gray-800 rounded-lg shadow-lg p-6">
<h1 class="text-2xl font-bold text-gray-900 dark:text-white mb-2">Delete Assignment</h1>
<p class="text-gray-600 dark:text-gray-400 mb-6">
Are you sure you want to delete <span class="font-semibold text-gray-900 dark:text-white">{{ assignment.title }}</span>?
This will also delete all student submissions. This action cannot be undone.
</p>
<div class="flex items-center space-x-3">
<form method="post">
{% csrf_token %}
<button type="submit"
class="bg-red-500 hover:bg-red-600 dark:bg-red-700 dark:hover:bg-red-600 text-white font-semibold px-6 py-2 rounded-lg transition duration-200 focus:outline-none focus:ring-2 focus:ring-red-400">
Yes, Delete
</button>
</form>
<a href="{% url 'course_assignments' course.slug %}"
class="text-gray-500 hover:text-gray-700 dark:text-gray-400 text-sm">Cancel</a>
Comment thread
ayesha1145 marked this conversation as resolved.
</div>
</div>
</div>
{% endblock content %}
Loading
Loading