diff --git a/Instructor/admin.py b/Instructor/admin.py index 3a9db53..b697f24 100644 --- a/Instructor/admin.py +++ b/Instructor/admin.py @@ -1,7 +1,12 @@ +""" + This file defines how our models interact with Django's admin interface +""" + from django.conf import settings from django.contrib import admin from . import models +# We'll be using our own system for Rubric creation in production, so we only run this in debug if settings.DEBUG: admin.site.register(models.Rubric) diff --git a/Instructor/apps.py b/Instructor/apps.py index b059a30..f55e7ea 100644 --- a/Instructor/apps.py +++ b/Instructor/apps.py @@ -1,3 +1,7 @@ +""" + This file provides configuration data to Django about this app +""" + from django.apps import AppConfig diff --git a/Instructor/forms.py b/Instructor/forms.py index a3b064a..013100a 100644 --- a/Instructor/forms.py +++ b/Instructor/forms.py @@ -1,3 +1,7 @@ +""" + This file defines the forms used in the Instructor app +""" + from json import JSONDecoder, JSONDecodeError from django.forms import ModelForm, TextInput @@ -9,15 +13,37 @@ from . import models class CreateRubricWidget(TextInput): + """ + This widget is used to create a rubric + + :cvar template_name: The html file used to represent this widget + :cvar input_type: The type to use on the element + """ + template_name = "widgets/rubric_create.html" input_type = 'hidden' class Media: + """ + This internal class defines what resources the widget needs + + :cvar css: The CSS this widget will use + :cvar js: The JavaScript this widget will use + """ + css = {'all': ('css/rubrics/rubric_table.css',)} js = ('js/rubrics/rubric-create-widget.js',) class RubricForm(ModelForm): + """ + This Form is used in the creation of a Rubric + + :cvar rubric: This field will take a JSON representation of our Rubric + :cvar _validation_schema: This dictionary defines what the Rubric JSON should look like + :ivar _json_validator: We use this object to validate inputted JSON against our schema + """ + rubric = CharField(widget=CreateRubricWidget, help_text="This rubric will be used by reviewers to grade code") _validation_schema = { "type": "array", @@ -55,10 +81,22 @@ class RubricForm(ModelForm): } class Meta: + """ + This internal class defines data about the form itself + + :cvar model: The model django will auto-create certain form fields from + :cvar fields: The fields to use in the form + """ + model = models.Rubric fields = ['name'] def __init__(self, *args, **kwargs): + """ + This function is run on creation of the form + It loads the Rubric into JSON and then sets up the schema + """ + instance: models.Rubric = kwargs.get("instance", None) initial: dict = kwargs.get("initial", {}) if instance is not None: @@ -74,6 +112,16 @@ class RubricForm(ModelForm): super().__init__(*args, **kwargs) def save(self, commit=True) -> models.Rubric: + """ + This function is run to save the Rubric to the database + It loads the JSON, and creates the needed `RubricRow` and `RubricCell` objects to represent the Rubric + + :param commit: Whether to actually save the new Rubric to the database + :type commit: bool + :return: The new rubric + :rtype: models.Rubric + """ + new_rubric: models.Rubric = super().save(commit=False) new_rubric.max_score = 0 new_rubric.save() @@ -123,6 +171,15 @@ class RubricForm(ModelForm): @staticmethod def explain_json_error(error: ValidationError) -> str: + """ + In the event of an error when parsing the JSON for the rubric, this function will exaplin what is wrong + + :param error: The ValidationError that occured + :type error: ValidationError + :return: An explanation on what is wrong with the JSON + :rtype: str + """ + if len(error.relative_path) > 0: row_num = error.relative_path[0] + 1 location = f"in row {row_num}" @@ -156,6 +213,14 @@ class RubricForm(ModelForm): return "Unknown Error" def clean(self) -> dict: + """ + This function is run to validate the entered data in the form. + It ensures that the JSON is valid. If it isn't valid, it explains the error + + :return: The cleaned data of the form + :rtype: dict + """ + super().clean() raw_json: str = self.cleaned_data.get("rubric") diff --git a/Instructor/models.py b/Instructor/models.py index ee1f390..4d83dec 100644 --- a/Instructor/models.py +++ b/Instructor/models.py @@ -1,3 +1,7 @@ +""" + This file defines models that will converted to tables in the database for the Instructor app +""" + from json import JSONEncoder from django.db import models @@ -6,17 +10,44 @@ from Main.models import BaseModel class RubricCell(BaseModel): + """ + This model is used to represent a cell in a rubric in the database + + :cvar score: The score the student will receive + :cvar description: The description of the cell, what the student has to do to get this score + :cvar parent_row: The `RubricRow` object that this cell is in + :cvar index: The position of this cell in a `RubricRow` + """ + description = models.TextField() score = models.FloatField() parent_row = models.ForeignKey('RubricRow', on_delete=models.CASCADE) index = models.SmallIntegerField() class Meta: + """ + This internal class defines how the model will function in the database + + :cvar ordering: We order the cells by their index + :cvar unique_together: We dont want two cells at the same position on a row, + so we make `RubricCell.parent_row` and `RubricCell.index` unique together + """ + ordering = ['index'] unique_together = (('parent_row', 'index'),) class RubricRow(BaseModel): + """ + This model represents a row in a rubric in the database + + :cvar name: The name of the row + :cvar description: The description of the row + :cvar max_score: The max score a row can have + :cvar parent_rubric: The `Rubric` that this row is inside of + :cvar index: The position of this row in the rubric + """ + name = models.CharField(max_length=20) description = models.TextField() max_score = models.FloatField() @@ -24,21 +55,51 @@ class RubricRow(BaseModel): index = models.SmallIntegerField() class Meta: + """ + This internal class defines how the model will function in the database + + :cvar ordering: Rows should be ordered by their index + :cvar unique_together: We dont want two rows at the same position on a rubric, + so we make `RubricRow.parent_rubric` and `RubricRow.index` unique together + """ + ordering = ['index'] unique_together = (('parent_rubric', 'index'),) class ScoredRow(BaseModel): + """ + This model represents a row that has been graded by a reviewer in the database + + :cvar parent_review: The review that this score belongs to + :cvar source_row: The row that has been scored + :cvar score: The score that was put in by the reviewer + """ + parent_review = models.ForeignKey('Main.Review', on_delete=models.CASCADE) source_row = models.ForeignKey('RubricRow', on_delete=models.CASCADE) score = models.FloatField() class Rubric(BaseModel): + """ + This model represents a Rubric in the database + + :cvar name: The name of the rubric + :cvar max_score: The max possible score that a student can get with this rubric + """ + name = models.CharField(max_length=20, help_text="The name the students will use to pick a rubric when requesting a review") max_score = models.FloatField() def to_json(self) -> str: + """ + This function converts the Rubric to json for use in the front-end + + :returns: A JSON representation of this rubric + :rtype: str + """ + new_obj = [] for row in list(self.rubricrow_set.all()): @@ -50,4 +111,11 @@ class Rubric(BaseModel): return JSONEncoder().encode(new_obj) def __str__(self) -> str: + """ + This function defines how this object is casted to a string + + :returns: The name of the Rubric + :rtype: str + """ + return self.name diff --git a/Instructor/static/css/rubrics/rubric_table.css b/Instructor/static/css/rubrics/rubric_table.css index 718af2f..9954c47 100644 --- a/Instructor/static/css/rubrics/rubric_table.css +++ b/Instructor/static/css/rubrics/rubric_table.css @@ -1,4 +1,4 @@ -/*!* Use to render rubric tables *!*/ +/*!* This file describes styles for Rubrics *!*/ .rubric-graded td.selected, .rubric-grading td.selected { background-color: #edffdd !important; diff --git a/Instructor/static/js/rubrics/rubric-create-widget.js b/Instructor/static/js/rubrics/rubric-create-widget.js index 52ce242..5d714e2 100644 --- a/Instructor/static/js/rubrics/rubric-create-widget.js +++ b/Instructor/static/js/rubrics/rubric-create-widget.js @@ -1,7 +1,15 @@ +/** + * This file is used when creating or editting a rubric + */ + let table = undefined; let add_row = undefined; function convert_from_json(src_json) { + /** + * This function loads JSON and shows it on the table + * This is used when loading a non-empty Rubric for editing + */ add_row.click(); @@ -36,6 +44,9 @@ function convert_from_json(src_json) { } function convert_to_json() { + /** + * This function converts the table into JSON to send to the backend + */ let new_rubric = []; @@ -68,18 +79,30 @@ function convert_to_json() { } function delete_row_callback(event) { + /** + * This function is run when the "Delete" button is clicked on a row + * It deletes the row. + */ $(event.target).parents("tr").remove(); } function delete_cell_callback(event) { + /** + * This function is run when the "Delete" button is clicked on a cell + * It deletes the cell + */ $(event.target).parents("td").remove(); } function add_row_callback() { + /** + * This function is run to add a row to the table, it loads HTML from the template div + */ + let new_row = $("