Prevent blank professors from being created, while still allowing on-the-fly professo...
authorCharles Connell <charles@connells.org>
Mon, 21 Apr 2014 03:07:31 +0000 (23:07 -0400)
committerCharles Connell <charles@connells.org>
Mon, 21 Apr 2014 03:07:31 +0000 (23:07 -0400)
karmaworld/apps/courses/forms.py
karmaworld/apps/courses/models.py

index 4cfb4451601a62c93e42f9f00d7e3a73e97e79f1..f46d028ef96fadade1f5697126fa5b4ef587346d 100644 (file)
@@ -36,8 +36,7 @@ class ProfessorForm(NiceErrorModelForm, ACFieldModelForm):
     name = AutoCompleteSelectField('professor_object_by_name', help_text='',
         label=mark_safe('Professor\'s name <span class="required-field">(required)</span>'),
         # 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)
index 692c67be72c09fac7b3f7efb1f5e23c087879a8a..fdab3242ca8329bbf809397578edf088154c70b8 100644 (file)
@@ -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:
         """