From c572c7ada87ac212350934b602403069d445adb2 Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Sun, 20 Apr 2014 23:07:31 -0400 Subject: [PATCH] Prevent blank professors from being created, while still allowing on-the-fly professor creation --- karmaworld/apps/courses/forms.py | 10 ++++++---- karmaworld/apps/courses/models.py | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/karmaworld/apps/courses/forms.py b/karmaworld/apps/courses/forms.py index 4cfb445..f46d028 100644 --- a/karmaworld/apps/courses/forms.py +++ b/karmaworld/apps/courses/forms.py @@ -36,8 +36,7 @@ class ProfessorForm(NiceErrorModelForm, ACFieldModelForm): name = AutoCompleteSelectField('professor_object_by_name', help_text='', label=mark_safe('Professor\'s name (required)'), # allows creating a new Professor on the fly - required=True, - widget=AutoCompleteSelectWidget('professor_object_by_name', attrs={'class': 'small-6 columns'})) + required=False) email = AutoCompleteSelectField('professor_object_by_email', help_text='', label="Professor's email address", # allows creating a new Professor on the fly @@ -48,7 +47,7 @@ class ProfessorForm(NiceErrorModelForm, ACFieldModelForm): # order the fields fields = ('name', 'email') - def _clean_distinct_field(self, field, *args, **kwargs): + def _clean_distinct_field(self, field, value_required=False, *args, **kwargs): """ Check to see if Professor model is found before grabbing the field. Ensure that if Professor was already found, the new field corresponds. @@ -62,6 +61,8 @@ class ProfessorForm(NiceErrorModelForm, ACFieldModelForm): oldprof = self.instance # Extract the field value, possibly replacing self.instance value = self._clean_field(field, *args, **kwargs) + if value_required and not value: + raise ValidationError('This field is required.') if oldprof and not value: # This field was not supplied, but another one determined the prof. # Grab field from prof model object. @@ -69,10 +70,11 @@ class ProfessorForm(NiceErrorModelForm, ACFieldModelForm): if oldprof and self.instance != oldprof: # Two different Professor objects have been found across fields. raise ValidationError('It looks like two or more different Professors have been autocompleted.') + return value def clean_name(self, *args, **kwargs): - return self._clean_distinct_field('name', *args, **kwargs) + return self._clean_distinct_field('name', value_required=True, *args, **kwargs) def clean_email(self, *args, **kwargs): return self._clean_distinct_field('email', *args, **kwargs) diff --git a/karmaworld/apps/courses/models.py b/karmaworld/apps/courses/models.py index 692c67b..fdab324 100644 --- a/karmaworld/apps/courses/models.py +++ b/karmaworld/apps/courses/models.py @@ -176,7 +176,7 @@ class ProfessorManager(models.Manager): """ Return a Professor defined by name and email address. """ - return self.get(name=name,email=email) + return self.get(name=name, email=email) class Professor(models.Model): @@ -187,7 +187,7 @@ class Professor(models.Model): name = models.CharField(max_length=255, verbose_name="Professor's name") email = models.EmailField(blank=True, null=True, - verbose_name="Professor's Email") + verbose_name="Professor's Email") class Meta: """ -- 2.25.1