From c18ce895c2d87535e2b8370da178be49262e7209 Mon Sep 17 00:00:00 2001 From: JasonGrace2282 Date: Mon, 11 Nov 2024 10:00:04 -0500 Subject: [PATCH] Added ModelAdmin Also added __repr__ so I stop confusing myself --- tin/apps/assignments/admin.py | 19 ++++- .../assignments/migrations/0033_language.py | 68 +++++++++++++++- ...034_remove_assignment_language_and_more.py | 77 ------------------- tin/apps/assignments/models.py | 5 +- .../venvs/migrations/0006_venv_language.py | 2 +- 5 files changed, 90 insertions(+), 81 deletions(-) delete mode 100644 tin/apps/assignments/migrations/0034_remove_assignment_language_and_more.py diff --git a/tin/apps/assignments/admin.py b/tin/apps/assignments/admin.py index 4413d92a..077d07d5 100644 --- a/tin/apps/assignments/admin.py +++ b/tin/apps/assignments/admin.py @@ -9,6 +9,7 @@ CooldownPeriod, FileAction, Folder, + Language, MossResult, Quiz, QuizLogMessage, @@ -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 @@ -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" diff --git a/tin/apps/assignments/migrations/0033_language.py b/tin/apps/assignments/migrations/0033_language.py index 082c235f..ac4db9dc 100644 --- a/tin/apps/assignments/migrations/0033_language.py +++ b/tin/apps/assignments/migrations/0033_language.py @@ -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 = [ @@ -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', + ), + ] diff --git a/tin/apps/assignments/migrations/0034_remove_assignment_language_and_more.py b/tin/apps/assignments/migrations/0034_remove_assignment_language_and_more.py deleted file mode 100644 index 8c1766ab..00000000 --- a/tin/apps/assignments/migrations/0034_remove_assignment_language_and_more.py +++ /dev/null @@ -1,77 +0,0 @@ -# Generated by Django 4.2.16 on 2024-11-11 01:56 - -from django.db import migrations, models -import django.db.models.deletion - - -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 = [ - ("assignments", "0033_language"), - ] - - operations = [ - migrations.AddField( - model_name="assignment", - name="language_details", - field=models.ForeignKey( - null=True, - on_delete=django.db.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=django.db.models.deletion.CASCADE, - related_name="assignment_set", - to="assignments.language", - ), - ), - migrations.RemoveField( - model_name='assignment', - name='language', - ), - ] diff --git a/tin/apps/assignments/models.py b/tin/apps/assignments/models.py index f2f29c80..b0c49afa 100644 --- a/tin/apps/assignments/models.py +++ b/tin/apps/assignments/models.py @@ -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})>" diff --git a/tin/apps/venvs/migrations/0006_venv_language.py b/tin/apps/venvs/migrations/0006_venv_language.py index 9a906881..8851c1c3 100644 --- a/tin/apps/venvs/migrations/0006_venv_language.py +++ b/tin/apps/venvs/migrations/0006_venv_language.py @@ -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"), ]