'clean form'

def clean(self):

    (user, course_id) = (self.cleaned_data.get('user'), self.cleaned_data.get('course_id'))
    if (user and course_id):
        self.cleaned_data['enrollment'] = enrollment = CourseEnrollment.get_or_create_enrollment(user, course_id)
        if enrollment.refundable():
            msg = _('Course    {course_id}    not    past    the    refund    window.').format(course_id=course_id)
            raise forms.ValidationError(msg)
        try:
            self.cleaned_data['cert'] = enrollment.certificateitem_set.filter(mode='verified', status='purchased')[0]
        except IndexError:
            msg = _('No    order    found    for    {user}    in    course    {course_id}').format(user=user, course_id=course_id)
            raise forms.ValidationError(msg)
    return self.cleaned_data
