Something went wrong. Try again.
Code Review Workflow for IT Programming at BCTC West
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260""" This file defines the forms used in the Main app"""
from json import JSONDecoder, JSONDecodeErrorfrom re import fullmatchfrom typing import List
from django.db.models import Qfrom django.forms import ModelForm, TextInput, Textarea, ValidationErrorfrom django.forms.fields import CharFieldfrom jsonschema.validators import Draft202012Validator
from Instructor.models import RubricRow, ScoredRowfrom . import models
class IsDraftWidget(TextInput): input_type = "hidden"
class GradeReviewWidget(TextInput): """ This widget displays a table the user can click to grade a review
:cvar template_name: The template to use to render this widget :cvar input_type: The type of the <input> element :cvar rubric: The rubric that the Review is using """
template_name = "widgets/rubric_grade.html" input_type = "hidden" rubric = None review = None
class Media: """ This internal class specifies additional resources the widget needs
:cvar css: The CSS to include with the widget :cvar js: The JavaScript to use with the widget """
css = {"all": ("css/rubrics/rubric_table.css",)} js = ("js/rubrics/rubric-grade-widget.js",)
def get_context(self, name, value, attrs) -> dict[str, object]: """ This function defines context to pass when rendering this widget
:return: The context for the template :rtype: dict """
context = super().get_context(name, value, attrs) context["widget"]["rubric"] = self.rubric context["widget"]["review"] = self.review return context
class ReviewForm(ModelForm): """ This form is used when requesting/editing a review """
SCHOOLOGY_ID_ERROR_MESSAGE = ( "Schoology ID should be in format: XX.XX.XX where X'es are numbers" )
def __init__(self, *args, **kwargs): """ This function is run to initialize an instance of the form It sets self.user to the user kwarg """
if "user" in kwargs.keys(): self.user = kwargs.pop("user") else: self.user = None super().__init__(*args, **kwargs)
class Meta: """ This internal class specifies options for this form
:cvar; The model this form edits :cvar fields: The fields to include on the form """
model = models.Review fields = ["schoology_id", "rubric"]
def clean(self) -> None: """ This function is run to validate form data. It ensures that a student can only have two requested reviews at once. It also validates the schoology id. """
if ( self.user is not None and models.Review.objects.filter(student=self.user) .filter( Q(status=models.Review.Status.OPEN) | Q(status=models.Review.Status.ASSIGNED) ) .count() >= 2 ): raise ValidationError("You can only have 2 requested reviews at once.")
super(ReviewForm, self).clean() schoology_id = self.cleaned_data.get("schoology_id", None) if (schoology_id is not None) and not fullmatch( r"\d{2}\.\d{2}\.\d{2}", schoology_id ): self.add_error("schoology_id", self.SCHOOLOGY_ID_ERROR_MESSAGE)
def save(self, commit=True) -> models.Review: """ This function is run to save the new Review It sets the review's student to the user that submitted the form
:param commit: Whether to actually save the new Review :type commit: bool :returns: The new review :rtype: models.Review """ new_review: models.Review = super(ReviewForm, self).save(commit=False) if self.instance: if self.initial.get("rubric", None) != new_review.rubric.id: self.instance.scoredrow_set.all().delete() if self.user is not None: new_review.student = self.user if commit: new_review.save() return new_review
class GradeReviewForm(ModelForm): """ This form is used to grade Reviews
:cvar scores: The scores as a JSON string to be encoded into ScoredRow objects :cvar _validation_schema: The schema used to ensure JSON is valid :cvar field_order: The order in which fields will appear in the form :ivar _json_validator: The validator used to ensure JSON is formatted correctly """
is_draft = CharField(widget=IsDraftWidget, required=True, initial="false") scores = CharField( max_length=200, widget=GradeReviewWidget(), help_text="Please fill out each row in the rubric.", )
def __init__(self, *args, **kwargs): """ This function is run to initialize a new instance of the form It loads the instance passed as a kwarg
:raises: ValueError: If a Review object is not passed in kwargs """
instance: models.Review = kwargs.get("instance", None) super().__init__(*args, **kwargs) self._validation_schema = {"type": "array", "items": {"type": "number"}} if instance: self.rubric = instance.rubric self.fields["scores"].widget.rubric = self.rubric self.fields["scores"].widget.review = instance else: raise ValueError("An instance must be provided to GradeReviewForm!") target_item_length = self.rubric.rubricrow_set.count() self._validation_schema["maxItems"] = target_item_length self._validation_schema["minItems"] = target_item_length self._json_validator = Draft202012Validator(self._validation_schema)
class Meta: """ This internal class defines settings for the Form
:cvar model: The model the form is working with :cvar fields: The fields to use in the form :cvar widgets: The widgets each field will use """
model = models.Review fields = ["additional_comments"] widgets = {"additional_comments": Textarea(attrs={"style": "height: 150px;"})}
def save(self, commit=True) -> models.Review: """ This function runs when the Form is saving It reads the scores from JSON and makes ScoredRow objects
:param commit: Whether to save the changes to the database :type commit: bool :return: The newly graded Review :rtype: models.Review """
new_review: models.Review = super(GradeReviewForm, self).save(commit=False) scores_array: List[str] = JSONDecoder().decode(self.cleaned_data.get("scores")) for i in range(len(scores_array)): score = float(scores_array[i]) row = RubricRow.objects.get(parent_rubric=new_review.rubric, index=i) scored_row = ScoredRow.objects.get_or_create( source_row=row, parent_review=new_review, defaults={"score": score} ) scored_row[0].score = score scored_row[0].save() new_review.status = ( models.Review.Status.CLOSED if self.cleaned_data.get("is_draft") == "false" else models.Review.Status.ASSIGNED ) if commit: new_review.save() return new_review
field_order = ["scores", "additional_comments"]
def clean(self) -> None: """ This function is used to validate input data. It ensures the JSON submitted for scores is valid and readable.
:return: The cleaned input data :rtype: dict """
cleaned_data: dict = super(GradeReviewForm, self).clean() scores_json: str = cleaned_data.get("scores")
if scores_json: try: parsed = JSONDecoder().decode(scores_json) errors = sorted(self._json_validator.iter_errors(parsed), key=str) [self.add_error("scores", f"{error.message}") for error in errors] if len(errors) == 0: for index, row in enumerate(self.rubric.rubricrow_set.all()): if parsed[index] < 0 and not cleaned_data.get("is_draft"): self.add_error( "scores", f"Row {index + 1} is -1.", ) if ( parsed[index] != -1 and row.rubriccell_set.filter(score=parsed[index]).exists() is False ): self.add_error( "scores", f"Invalid score: {parsed[index]} for row {index + 1}", )
except JSONDecodeError: self.add_error("scores", "Invalid JSON")