diff --git a/Website/api/models.py b/Website/api/models.py index 9dc22f2..d62f9dc 100644 --- a/Website/api/models.py +++ b/Website/api/models.py @@ -18,31 +18,32 @@ class Student(models.Model): super(Student, self).save(*args, **kwargs) def __str__(self): - return f"{self.user.username}'s Profile" + return f"{self.user.first_name} {self.user.last_name} ({self.user.username})" class Assignment(models.Model): owner = models.ForeignKey(User, null=True, blank=True, related_name='aowner', on_delete=models.CASCADE) - name=models.CharField(max_length=100, primary_key=True) due_date=models.DateTimeField() # files = models.ManyToManyField(DefFiles) files=models.CharField(max_length=100, default="", blank=True) - path=models.CharField(max_length=100) - classes=models.CharField(max_length=100) - teacher=models.CharField(max_length=100) + path=models.CharField(max_length=100, default="", blank=True) + classes=models.CharField(max_length=100, default="", blank=True) + teacher=models.CharField(max_length=100, default="", blank=True) def __str__(self): - return '%s' % (self.name) + return f'{self.name}' class Class(models.Model): owner = models.ForeignKey(User, null=True, blank=True, related_name='cowner', on_delete=models.CASCADE) - teacher = models.CharField(max_length=100) + teacher = models.CharField(max_length=100, blank=True) + subject = models.CharField(max_length=50, blank=True) + period = models.PositiveIntegerField(null=True, blank=True, default=0) name = models.CharField(primary_key=True, max_length=100) id = models.CharField(max_length=8, blank=True, null=True) - description = models.CharField(default="Class Description", max_length=500) + description = models.CharField(default="Class Description", max_length=500, blank=True) repo=models.URLField(default="", blank=True) - path=models.CharField(max_length=100, default="") + path=models.CharField(max_length=100, default="", blank=True) assignments=models.ManyToManyField(Assignment, blank=True) default_file=models.CharField(max_length=100, default="", blank=True) confirmed=models.ManyToManyField(Student, blank=True, related_name='confirmed') @@ -60,7 +61,7 @@ class Class(models.Model): return super(Class, self).save(*args, **kwargs) def __str__(self): - return self.name + return f"{self.user.first_name} {self.user.last_name} ({self.user.username})" class Teacher(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) diff --git a/Website/skoolos/forms.py b/Website/skoolos/forms.py index 6374f97..e249d96 100644 --- a/Website/skoolos/forms.py +++ b/Website/skoolos/forms.py @@ -1,6 +1,6 @@ from django import forms from django.contrib.auth.models import User -from api.models import Student, Teacher +from api.models import Student, Teacher, Class, Assignment import re class UserUpdateForm(forms.ModelForm): @@ -26,3 +26,54 @@ class TeacherUpdateForm(forms.ModelForm): class Meta: model = Teacher fields = ['git'] + +class ClassCreationForm (forms.ModelForm): + subject = forms.CharField(max_length=50) + period = forms.IntegerField(min_value=0, max_value=9) + description = forms.CharField(widget=forms.Textarea) + unconfirmed = forms.ModelMultipleChoiceField(queryset=Student.objects.all(), label="Invite students") + + + def clean_period(self): + pd = self.cleaned_data['period'] + if pd < 1 or pd > 9: + raise forms.ValidationError("Invalid period") + return pd; + + def __init__(self, *args, **kwargs): + super(ClassCreationForm, self).__init__(*args, **kwargs) + self.fields['period'].widget.attrs['min'] = 0 + # Only in case we build the form from an instance + # (otherwise, 'unconfirmed' list should be empty) + if kwargs.get('instance'): + # We get the 'initial' keyword argument or initialize it + # as a dict if it didn't exist. + initial = kwargs.setdefault('initial', {}) + # The widget for a ModelMultipleChoiceField expects + # a list of primary key for the selected data. + initial['unconfirmed'] = [t.pk for t in kwargs['instance'].unconfirmed.all()] + + # Overriding save allows us to process the value of 'unconfirmed' field + def save(self, commit=True): + # Get the unsave Pizza instance + instance = forms.ModelForm.save(self, False) + + # Prepare a 'save_m2m' method for the form, + old_save_m2m = self.save_m2m + def save_m2m(): + old_save_m2m() + # This is where we actually link the pizza with toppings + instance.topping_set.clear() + instance.topping_set.add(*self.cleaned_data['unconfirmed']) + self.save_m2m = save_m2m + + # Do we need to save all changes now? + if commit: + instance.save() + self.save_m2m() + + return instance + + class Meta: + model = Class + fields = ['subject', 'period', 'description', 'unconfirmed'] diff --git a/Website/skoolos/templates/skoolos/base.html b/Website/skoolos/templates/skoolos/base.html index 2ec152d..fa2f42c 100644 --- a/Website/skoolos/templates/skoolos/base.html +++ b/Website/skoolos/templates/skoolos/base.html @@ -34,6 +34,12 @@
+ {% empty %} + {% if isTeacher %} +Looks like you haven't made any assignments yet, hit the button in the top right to get started
+ {% else %} +Looks like there aren't any assignments at the moment, you got lucky this time!
+ {% endif %} {% endfor %} @@ -20,6 +26,8 @@Looks like you haven't created any classes yet, hit the button in the top right to get started.
+ {% else %} +Looks like you're not enrolled in any classes at the moment! Ask your teacher if you think this is wrong.
+ {% endif %} {% endfor %} {% endblock content %} diff --git a/Website/skoolos/templates/skoolos/profile_student.html b/Website/skoolos/templates/skoolos/profile_student.html index 3da2f41..947dab7 100644 --- a/Website/skoolos/templates/skoolos/profile_student.html +++ b/Website/skoolos/templates/skoolos/profile_student.html @@ -5,6 +5,7 @@Student
{{ user.email }} @@ -16,6 +17,8 @@