From f99329ace7549b8779e5ae69f500942a4437bed4 Mon Sep 17 00:00:00 2001 From: Bryan Date: Mon, 3 Mar 2014 03:22:57 +0000 Subject: [PATCH] using simpler django-ajax-selects-cascade, create dept when not found. --- karmaworld/apps/courses/forms.py | 47 ++++++++++++++++++++++---------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/karmaworld/apps/courses/forms.py b/karmaworld/apps/courses/forms.py index 42c5066..99647cc 100644 --- a/karmaworld/apps/courses/forms.py +++ b/karmaworld/apps/courses/forms.py @@ -7,14 +7,14 @@ import random from django.conf import settings from django.forms import ModelForm from django.forms import CharField +from django.forms import ValidationError from django.forms.util import ErrorList from ajax_select.fields import AutoCompleteSelectField -from ajax_select.fields import AutoCompleteSelectWidget from ajax_select_cascade.fields import AutoCompleteDependentSelectField -from ajax_select_cascade.fields import AutoCompleteDependentSelectWidget from karmaworld.apps.courses.models import Course +from karmaworld.apps.courses.models import Department # Django hard codes CSS attributes into ModelForm returned ErrorList @@ -43,19 +43,14 @@ class NiceErrorModelForm(ModelForm): class CourseForm(NiceErrorModelForm): - school = AutoCompleteSelectField( - 'school', - widget=AutoCompleteSelectWidget( - 'school', - attrs={'id': 'dom_autocomplete_school'} - ) - ) + # first argument is ajax channel name, defined in models as LookupChannel. + school = AutoCompleteSelectField('school', help_text='') department = AutoCompleteDependentSelectField( - 'dept_given_school', - widget=AutoCompleteDependentSelectWidget( - 'dept_given_school', - attrs={'data-upstream-id': 'dom_autocomplete_school'}, - ) + 'dept_given_school', help_text='', + # autocomplete department based on school + dependsOn=school, + # allows creating a new department on the fly + required=False ) def __init__(self, *args, **kwargs): @@ -73,6 +68,30 @@ class CourseForm(NiceErrorModelForm): fields = ('school', 'department', 'name', 'instructor_name', 'instructor_email', 'url') + def clean_department(self, *args, **kwargs): + """ Create a new department if one is not provided. """ + if 'department' not in self.cleaned_data or \ + not self.cleaned_data['department']: + # Department is missing. + # Can a new one be made? + if 'school' not in self.cleaned_data or \ + not self.cleaned_data['school']: + raise ValidationError('Cannot create a new department without a school.') + if 'department_text' not in self.data or \ + not self.data['department_text']: + raise ValidationError('Cannot create a new department without a name.') + + # Build a new Department. + school = self.cleaned_data['school'] + dept_name = self.data['department_text'] + dept = Department(name=dept_name, school=school) + dept.save() + + # Fill in cleaned data as though this department were chosen. + self.cleaned_data['department'] = dept + # Return the clean Department + return self.cleaned_data['department'] + def clean(self, *args, **kwargs): """ Additional form validation. """ -- 2.25.1