diff --git a/ubiquity/tutoring/admin.py b/ubiquity/tutoring/admin.py index 75cd65116251efe01fcb1a874281463766e8715f..6affc609a197202d52f3d03e921074be6ac003c5 100644 --- a/ubiquity/tutoring/admin.py +++ b/ubiquity/tutoring/admin.py @@ -1,13 +1,181 @@ """Module managing the tutoring application admin side of the tutoring application""" +from typing import Optional + from django.contrib import admin -from .models import SourceCode, CodeFile, CodeBlock, PracticalWork, Group, Teacher, Student, StudentGroup +from django.urls import reverse +from django.utils.safestring import mark_safe + +from .models import CodeFile, CodeBlock, PracticalWork, Group, Teacher, Student, StudentGroup +# pylint: disable=no-self-use + + +def _to_link(url: str, obj_id: int, to_print: Optional[str] = None): + """ + Returns the object link + :param url: The link of object to change + :param obj_id: The object id + :param to_print: The string to print + :return: The code file link + """ + if obj_id is None: + return None + url = reverse(f"admin:{url}_change", args=[obj_id]) + link = '<a href="%s">%s</a>' % (url, to_print if to_print is not None else obj_id) + return mark_safe(link) + + +@admin.register(Teacher) +@admin.register(Student) +class UserAdmin(admin.ModelAdmin): + """Class ModelAdmin managing the users""" + list_display = ('id', 'username_link', 'first_name', 'last_name', 'email') + + @admin.display(description='ID') + def id(self, obj): + """ + Displays the id + :param obj: The user + :return: The id + """ + return obj.user.id + + @admin.display(description='Username') + def username_link(self, obj): + """ + Displays the user link + :param obj: The user + :return: The user link + """ + return _to_link("auth_user", obj.user.id, obj.user.username) + + @admin.display(description='First name') + def first_name(self, obj): + """ + Displays the first name + :param obj: The user + :return: The first name + """ + return obj.user.first_name + + @admin.display(description='Last name') + def last_name(self, obj): + """ + Displays the last name + :param obj: The user + :return: The last name + """ + return obj.user.last_name + + @admin.display(description='Email') + def email(self, obj): + """ + Displays the email + :param obj: The user + :return: The email + """ + return obj.user.email + + +@admin.register(StudentGroup) +class StudentGroupAdmin(admin.ModelAdmin): + """Class ModelAdmin managing the student groups""" + list_display = ('id', 'practical_work_link', 'group_link', 'student_link', 'source_code_id') + + @admin.display(description='Practical work') + def practical_work_link(self, obj): + """ + Displays the practical work link + :param obj: The student group + :return: The practical work link + """ + return _to_link("tutoring_practicalwork", obj.group.practical_work.id, + f"{obj.group.practical_work.id}- {obj.group.practical_work.name}") + + @admin.display(description='Group') + def group_link(self, obj): + """ + Displays the group link + :param obj: The student group + :return: The group link + """ + return _to_link("tutoring_group", obj.group.id, + f"{obj.group.practical_work.id}-{obj.group.id}") + + @admin.display(description='Student') + def student_link(self, obj): + """ + Displays the student user link + :param obj: The student group + :return: The student user link + """ + return _to_link("auth_user", obj.student.user.id, obj.student.user.username) + + +@admin.register(Group) +class GroupAdmin(admin.ModelAdmin): + """Class ModelAdmin managing the groups""" + list_display = ('id', 'practical_work_link',) + + @admin.display(description='Practical work') + def practical_work_link(self, obj): + """ + Displays the practical work link + :param obj: The group + :return: The practical work link + """ + return _to_link("tutoring_practicalwork", obj.practical_work.id, + f"{obj.practical_work.id}- {obj.practical_work.name}") + + +@admin.register(PracticalWork) +class PracticalWorkAdmin(admin.ModelAdmin): + """Class ModelAdmin managing the practical works""" + list_display = ('id', 'name', 'source_code_id', 'owner_link') + + @admin.display(description='Owner') + def owner_link(self, obj): + """ + Displays the teacher user link + :param obj: The practical work + :return: The teacher user link + """ + return _to_link("auth_user", obj.teacher.user.id, obj.teacher.user.username) + + +@admin.register(CodeFile) +class CodeFileAdmin(admin.ModelAdmin): + """Class ModelAdmin managing the code files""" + list_display = ('id', 'path_file', 'source_code_id') + + +@admin.register(CodeBlock) +class CodeBlockAdmin(admin.ModelAdmin): + """Class ModelAdmin managing the code blocks""" + list_display = ('id', 'path_file', 'name', 'code_file_link', 'parent_link') + + @admin.display(description='Path file') + def path_file(self, obj): + """ + Displays the path file + :param obj: The code block + :return: The path file + """ + return obj.code_file.path_file + @admin.display(description='Code File') + def code_file_link(self, obj): + """ + Displays the code file link + :param obj: The code block + :return: The code file link + """ + return _to_link("tutoring_codefile", obj.code_file.id) -admin.site.register(SourceCode) -admin.site.register(CodeFile) -admin.site.register(CodeBlock) -admin.site.register(PracticalWork) -admin.site.register(Group) -admin.site.register(Teacher) -admin.site.register(Student) -admin.site.register(StudentGroup) + @admin.display(description='Parent') + def parent_link(self, obj): + """ + Displays the parent link + :param obj: The code block + :return: The parent link + """ + return _to_link("tutoring_codeblock", obj.parent.id if obj.parent is not None else None)