Fixed some fields and started more work on forms

This commit is contained in:
Rushil Umaretiya 2020-06-16 18:24:16 -04:00
parent f825713f2f
commit dab0534592
3 changed files with 18 additions and 26 deletions

View File

@ -61,7 +61,7 @@ class Class(models.Model):
return super(Class, self).save(*args, **kwargs)
def __str__(self):
return f"{self.user.first_name} {self.user.last_name} ({self.user.username})"
return f"{self.name}"
class Teacher(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
@ -70,7 +70,7 @@ class Teacher(models.Model):
ion_user=models.CharField(primary_key=True, max_length=100)
def __str__(self):
return f"{self.user.username}'s Profile"
return f"{self.user.first_name} {self.user.last_name} ({self.user.username})"
def save(self, *args, **kwargs):
super(Teacher, self).save(*args, **kwargs)

View File

@ -54,23 +54,16 @@ class ClassCreationForm (forms.ModelForm):
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)
def save(self, username=""):
cleaned_data = self.cleaned_data
print(self)
# 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()
# Get the unsave Class instance
instance = forms.ModelForm.save(self)
instance.unconfirmed.clear()
instance.unconfirmed.add(*cleaned_data['unconfirmed'])
instance.name = cleaned_data['subject'] + str(cleaned_data['period']) + "_" + username
print("Class name: " + instance.name)
return instance

View File

@ -139,14 +139,13 @@ def createClassHelper(request):
teacher = request.user.teacher
if request.method == "POST":
userForm = UserUpdateForm(request.POST, instance=request.user)
profileForm = TeacherUpdateForm(request.POST,
instance=request.user.teacher)
if userForm.is_valid() and profileForm.is_valid():
userForm.save()
profileForm.save()
messages.success(request, "Your account has been updated!")
return redirect('profile')
classForm = ClassCreationForm(request.POST)
if classForm.is_valid():
cleaned_data = classForm.clean()
print(cleaned_data)
classForm.save(username=teacher.user.username)
messages.success(request, cleaned_data['subject'].capitalize() + " has been created!")
return redirect('home')
else:
classForm = ClassCreationForm()