From: Charles Connell Date: Mon, 21 Apr 2014 03:07:31 +0000 (-0400) Subject: Prevent blank professors from being created, while still allowing on-the-fly professo... X-Git-Tag: release-20150131~139 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=c572c7ada87ac212350934b602403069d445adb2;p=oweals%2Fkarmaworld.git Prevent blank professors from being created, while still allowing on-the-fly professor creation --- 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: """