Skip to content

Commit

Permalink
Added ModelAdmin
Browse files Browse the repository at this point in the history
Also added __repr__ so I stop confusing myself
  • Loading branch information
JasonGrace2282 committed Nov 11, 2024
1 parent 9e544f9 commit c18ce89
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 81 deletions.
19 changes: 18 additions & 1 deletion tin/apps/assignments/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
CooldownPeriod,
FileAction,
Folder,
Language,
MossResult,
Quiz,
QuizLogMessage,
Expand All @@ -32,7 +33,15 @@ def assignments(self, obj):
@admin.register(Assignment)
class AssignmentAdmin(admin.ModelAdmin):
date_hierarchy = "due"
list_display = ("name", "course_name", "folder", "due", "visible", "quiz_icon")
list_display = (
"name",
"course_name",
"folder",
"due",
"visible",
"quiz_icon",
"language_details",
)
list_filter = ("course", "due")
ordering = ("-due",)
save_as = True
Expand All @@ -52,6 +61,14 @@ def quiz_icon(self, obj):
return bool(obj.is_quiz)


@admin.register(Language)
class LanguageAdmin(admin.ModelAdmin):
list_display = ("name", "language", "path")
ordering = ("language", "name")
save_as = True
list_filter = ("language", "path")


@admin.register(CooldownPeriod)
class CooldownPeriodAdmin(admin.ModelAdmin):
date_hierarchy = "start_time"
Expand Down
68 changes: 67 additions & 1 deletion tin/apps/assignments/migrations/0033_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,46 @@
from django.db import migrations, models


def replace_language_with_default_language(apps, schema_editor):
"""Creates a default :class:`.Language` model for each assignment.
This converts the old language field to a foreign key to the new language model.
"""

Language = apps.get_model("assignments", "Language")
db_alias = schema_editor.connection.alias
python_310 = Language.objects.using(db_alias).get_or_create(
name="Python 3.10",
path="/usr/bin/python3.10",
language="P",
)[0]

# this doesn't actually exist (yet), but it's a placeholder until Tin has true java support
java_11 = Language.objects.using(db_alias).get_or_create(
name="Java 11",
path="/usr/bin/java11",
language="J",
)[0]

Assignment = apps.get_model("assignments", "Assignment")
for assignment in Assignment.objects.using(db_alias).all():
if assignment.language == "P":
assignment.language_details = python_310
assignment.save()
elif assignment.language == "J":
assignment.language_details = java_11
assignment.save()


def revert_default_language(apps, schema_editor):
Assignment = apps.get_model("assignments", "Assignment")
db_alias = schema_editor.connection.alias
for assignment in Assignment.objects.using(db_alias).all():
assignment.language = assignment.language_details.language
assignment.save()



class Migration(migrations.Migration):

dependencies = [
Expand All @@ -15,8 +55,34 @@ class Migration(migrations.Migration):
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=50)),
('path', models.FilePathField()),
('path', models.CharField(max_length=100)),
('language', models.CharField(choices=[('P', 'Python 3'), ('J', 'Java')], max_length=1)),
],
),
migrations.AddField(
model_name="assignment",
name="language_details",
field=models.ForeignKey(
null=True,
on_delete=models.deletion.CASCADE,
related_name="assignment_set",
to="assignments.language",
),
),
migrations.RunPython(replace_language_with_default_language, revert_default_language),
migrations.AlterField(
model_name="assignment",
name="language_details",
field=models.ForeignKey(
null=False,
on_delete=models.deletion.CASCADE,
related_name="assignment_set",
to="assignments.language",
),
),
migrations.RemoveField(
model_name='assignment',
name='language',
),

]

This file was deleted.

5 changes: 4 additions & 1 deletion tin/apps/assignments/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,8 +683,11 @@ class Language(models.Model):
)

name = models.CharField(max_length=50)
path = models.FilePathField()
path = models.CharField(max_length=100)
language = models.CharField(max_length=1, choices=LANGUAGES)

def __str__(self):
return f"<{type(self).__name__}: {self.name}>"

def __repr__(self) -> str:
return f"<{type(self).__name__}: {self.name} ({self.language})>"
2 changes: 1 addition & 1 deletion tin/apps/venvs/migrations/0006_venv_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def delete_python_310_from_venvs(apps, schema_editor):

class Migration(migrations.Migration):
dependencies = [
("assignments", "0034_remove_assignment_language_and_more"),
("assignments", "0033_language"),
("venvs", "0005_auto_20240328_0033"),
]

Expand Down

0 comments on commit c18ce89

Please sign in to comment.