def __init__(self, crmi, *args, **kwargs):
        from esp.program.controllers.classreg import get_custom_fields

        def hide_field(field, default=None):
            field.widget = forms.HiddenInput()
            if default is not None:
                field.initial = default
        def hide_choice_if_useless(field):
            """ Hide a choice field if there's only one choice """
            if len(field.choices) == 1:
                hide_field(field, default=field.choices[0][0])

        super(TeacherClassRegForm, self).__init__(*args, **kwargs)

        prog = crmi.program

        section_numbers = crmi.allowed_sections_actual
        section_numbers = zip(section_numbers, section_numbers)

        class_sizes = crmi.getClassSizes()
        class_sizes = zip(class_sizes, class_sizes)

        class_grades = crmi.getClassGrades()
        class_grades = zip(class_grades, class_grades)

        class_ranges = ClassSizeRange.get_ranges_for_program(prog)
        class_ranges = [(range.id, range.range_str()) for range in class_ranges]

        # num_sections: section_list; hide if useless
        self.fields['num_sections'].choices = section_numbers
        hide_choice_if_useless( self.fields['num_sections'] )
        # category: program.class_categories.all()
        self.fields['category'].choices = [ (x.id, x.category) for x in prog.class_categories.all() ]
        # grade_min, grade_max: crmi.getClassGrades
        self.fields['grade_min'].choices = class_grades
        self.fields['grade_max'].choices = class_grades
        if crmi.use_class_size_max:
            # class_size_max: crmi.getClassSizes
            self.fields['class_size_max'].choices = class_sizes
        else:
            del self.fields['class_size_max']

        if Tag.getBooleanTag('use_class_size_optimal', default=False):
            if not crmi.use_class_size_optimal:
                del self.fields['class_size_optimal']

            if crmi.use_optimal_class_size_range:
                self.fields['optimal_class_size_range'].choices = class_ranges
            else:
                del self.fields['optimal_class_size_range']

            if crmi.use_allowable_class_size_ranges:
                self.fields['allowable_class_size_ranges'].choices = class_ranges
            else:
                del self.fields['allowable_class_size_ranges']
        else:
            del self.fields['class_size_optimal']
            del self.fields['optimal_class_size_range']
            del self.fields['allowable_class_size_ranges']

        # decide whether to display certain fields

        # prereqs
        if not crmi.set_prereqs:
            self.fields['prereqs'].widget = forms.HiddenInput()

        # allow_lateness
        if not crmi.allow_lateness:
            self.fields['allow_lateness'].widget = forms.HiddenInput()
            self.fields['allow_lateness'].initial = 'False'

        self.fields['duration'].choices = sorted(crmi.getDurations())
        hide_choice_if_useless( self.fields['duration'] )

        # session_count
        if crmi.session_counts:
            session_count_choices = crmi.session_counts_ints
            session_count_choices = zip(session_count_choices, session_count_choices)
            self.fields['session_count'].choices = session_count_choices
        hide_choice_if_useless( self.fields['session_count'] )

        # requested_room
        if not crmi.ask_for_room:
            hide_field( self.fields['requested_room'] )

        #   Hide resource fields since separate forms are now being used. - Michael P
        #   Most have now been removed, but this one gets un-hidden by open classes.
        self.fields['requested_special_resources'].widget = forms.HiddenInput()

        #   Add program-custom form components (for inlining additional questions without
        #   introducing a separate program module)
        custom_fields = get_custom_fields()
        for field_name in custom_fields:
            self.fields[field_name] = custom_fields[field_name]

        #   Modify help text on these fields if necessary.
        custom_helptext_fields = ['duration', 'class_size_max', 'num_sections', 'requested_room', 'message_for_directors', 'purchase_requests', 'class_info'] + custom_fields.keys()
        for field in custom_helptext_fields:
            tag_data = Tag.getProgramTag('teacherreg_label_%s' % field, prog)
            if tag_data:
                self.fields[field].label = tag_data
            tag_data = Tag.getProgramTag('teacherreg_help_text_%s' % field, prog)
            if tag_data:
                self.fields[field].help_text = tag_data

        #   Hide fields as desired.
        tag_data = Tag.getProgramTag('teacherreg_hide_fields', prog)
        if tag_data:
            for field_name in tag_data.split(','):
                hide_field(self.fields[field_name])

        tag_data = Tag.getProgramTag('teacherreg_default_min_grade', prog)
        if tag_data:
            self.fields['grade_min'].initial = tag_data

        tag_data = Tag.getProgramTag('teacherreg_default_max_grade', prog)
        if tag_data:
            self.fields['grade_max'].initial = tag_data

        tag_data = Tag.getProgramTag('teacherreg_default_class_size_max', prog)
        if tag_data:
            self.fields['class_size_max'].initial = tag_data

        #   Rewrite difficulty label/choices if desired:
        if Tag.getTag('teacherreg_difficulty_choices'):
            self.fields['hardness_rating'].choices = json.loads(Tag.getTag('teacherreg_difficulty_choices'))
Beispiel #2
0
    def __init__(self, module, *args, **kwargs):
        from esp.program.controllers.classreg import get_custom_fields

        def hide_field(field, default=None):
            field.widget = forms.HiddenInput()
            if default is not None:
                field.initial = default

        def hide_choice_if_useless(field):
            """ Hide a choice field if there's only one choice """
            if len(field.choices) == 1:
                hide_field(field, default=field.choices[0][0])

        super(TeacherClassRegForm, self).__init__(*args, **kwargs)

        prog = module.get_program()

        section_numbers = module.allowed_sections_actual
        section_numbers = zip(section_numbers, section_numbers)

        class_sizes = module.getClassSizes()
        class_sizes = zip(class_sizes, class_sizes)

        class_grades = module.getClassGrades()
        class_grades = zip(class_grades, class_grades)

        class_ranges = ClassSizeRange.get_ranges_for_program(prog)
        class_ranges = [(range.id, range.range_str())
                        for range in class_ranges]

        # num_sections: section_list; hide if useless
        self.fields['num_sections'].choices = section_numbers
        hide_choice_if_useless(self.fields['num_sections'])
        # category: program.class_categories.all()
        self.fields['category'].choices = [
            (x.id, x.category) for x in prog.class_categories.all()
        ]
        # grade_min, grade_max: module.getClassGrades
        self.fields['grade_min'].choices = class_grades
        self.fields['grade_max'].choices = class_grades
        if module.use_class_size_max:
            # class_size_max: module.getClassSizes
            self.fields['class_size_max'].choices = class_sizes
        else:
            del self.fields['class_size_max']

        if Tag.getTag('use_class_size_optimal', default=False):
            if not module.use_class_size_optimal:
                del self.fields['class_size_optimal']

            if module.use_optimal_class_size_range:
                self.fields['optimal_class_size_range'].choices = class_ranges
            else:
                del self.fields['optimal_class_size_range']

            if module.use_allowable_class_size_ranges:
                self.fields[
                    'allowable_class_size_ranges'].choices = class_ranges
            else:
                del self.fields['allowable_class_size_ranges']
        else:
            del self.fields['class_size_optimal']
            del self.fields['optimal_class_size_range']
            del self.fields['allowable_class_size_ranges']

        # global_resources: module.getResourceTypes(is_global=True)
        self.fields['global_resources'].choices = module.getResourceTypes(
            is_global=True)
        # resources: module.getResourceTypes(is_global=False)
        resource_choices = module.getResourceTypes(is_global=False)

        # decide whether to display certain fields
        # resources
        if len(resource_choices) > 0:
            self.fields['resources'].choices = resource_choices
        else:
            self.fields['resources'].widget = forms.HiddenInput()

        # prereqs
        if not module.set_prereqs:
            self.fields['prereqs'].widget = forms.HiddenInput()

        # allow_lateness
        if not module.allow_lateness:
            self.fields['allow_lateness'].widget = forms.HiddenInput()
            self.fields['allow_lateness'].initial = 'False'

        self.fields['duration'].choices = sorted(module.getDurations())
        hide_choice_if_useless(self.fields['duration'])

        # session_count
        if module.session_counts:
            session_count_choices = module.session_counts_ints
            session_count_choices = zip(session_count_choices,
                                        session_count_choices)
            self.fields['session_count'].choices = session_count_choices
        hide_choice_if_useless(self.fields['session_count'])

        # requested_room
        if not module.ask_for_room:
            hide_field(self.fields['requested_room'])

        #   Hide resource fields since separate forms are now being used. - Michael P
        resource_fields = [
            'has_own_space', 'global_resources', 'resources',
            'requested_special_resources'
        ]
        for field in resource_fields:
            self.fields[field].widget = forms.HiddenInput()

        #   Add program-custom form components (for inlining additional questions without
        #   introducing a separate program module)
        custom_fields = get_custom_fields()
        for field_name in custom_fields:
            self.fields[field_name] = custom_fields[field_name]

        #   Modify help text on these fields if necessary.
        custom_helptext_fields = [
            'duration', 'class_size_max', 'num_sections', 'requested_room',
            'message_for_directors', 'purchase_requests', 'class_info'
        ] + custom_fields.keys()
        for field in custom_helptext_fields:
            tag_data = Tag.getTag('teacherreg_label_%s' % field)
            if tag_data:
                self.fields[field].label = tag_data
            tag_data = Tag.getTag('teacherreg_help_text_%s' % field)
            if tag_data:
                self.fields[field].help_text = tag_data

        #   Hide fields as desired.
        tag_data = Tag.getProgramTag('teacherreg_hide_fields', prog)
        if tag_data:
            for field_name in tag_data.split(','):
                hide_field(self.fields[field_name])

        tag_data = Tag.getProgramTag('teacherreg_default_min_grade', prog)
        if tag_data:
            print tag_data
            self.fields['grade_min'].initial = tag_data

        tag_data = Tag.getProgramTag('teacherreg_default_max_grade', prog)
        if tag_data:
            print tag_data
            self.fields['grade_max'].initial = tag_data

        tag_data = Tag.getProgramTag('teacherreg_default_class_size_max', prog)
        if tag_data:
            print tag_data
            self.fields['class_size_max'].initial = tag_data

        #   Rewrite difficulty label/choices if desired:
        if Tag.getTag('teacherreg_difficulty_choices'):
            self.fields['hardness_rating'].choices = json.loads(
                Tag.getTag('teacherreg_difficulty_choices'))
        if Tag.getTag('teacherreg_difficulty_label'):
            self.fields['hardness_rating'].label = Tag.getTag(
                'teacherreg_difficulty_label')
                # durations when every interface assumes they're identical?
                current_duration = current_data[
                    'duration'] or newclass.sections.all()[0].duration
                rounded_duration = 0
                for k, v in self.crmi.getDurations() + [(0, '')]:
                    new_delta = abs(k - current_duration)
                    if old_delta is None or new_delta < old_delta:
                        old_delta = new_delta
                        rounded_duration = k
                current_data['duration'] = rounded_duration
                current_data['category'] = newclass.category.id
                current_data['num_sections'] = newclass.sections.count()
                current_data['allow_lateness'] = newclass.allow_lateness
                current_data['title'] = newclass.title
                current_data['url'] = newclass.emailcode()
                for field_name in get_custom_fields():
                    if field_name in newclass.custom_form_data:
                        current_data[field_name] = newclass.custom_form_data[
                            field_name]
                if newclass.optimal_class_size_range:
                    current_data[
                        'optimal_class_size_range'] = newclass.optimal_class_size_range.id
                if newclass.allowable_class_size_ranges.all():
                    current_data['allowable_class_size_ranges'] = list(
                        newclass.allowable_class_size_ranges.all().values_list(
                            'id', flat=True))

                # Makes importing a class from a previous program work
                # These are the only three fields that can currently be hidden
                # If another one is added later, this will need to be changed
                hidden_fields = Tag.getProgramTag('teacherreg_hide_fields',
Beispiel #4
0
    def __init__(self, crmi, *args, **kwargs):
        from esp.program.controllers.classreg import get_custom_fields

        def hide_field(field, default=None):
            field.widget = forms.HiddenInput()
            if default is not None:
                field.initial = default
        def hide_choice_if_useless(field):
            """ Hide a choice field if there's only one choice """
            if len(field.choices) == 1:
                hide_field(field, default=field.choices[0][0])

        super(TeacherClassRegForm, self).__init__(*args, **kwargs)

        prog = crmi.program

        section_numbers = crmi.allowed_sections_actual
        section_numbers = zip(section_numbers, section_numbers)

        class_sizes = crmi.getClassSizes()
        class_sizes = zip(class_sizes, class_sizes)

        class_grades = crmi.getClassGrades()
        class_grades = zip(class_grades, class_grades)

        class_ranges = ClassSizeRange.get_ranges_for_program(prog)
        class_ranges = [(range.id, range.range_str()) for range in class_ranges]

        # num_sections: section_list; hide if useless
        self.fields['num_sections'].choices = section_numbers
        hide_choice_if_useless( self.fields['num_sections'] )
        # category: program.class_categories.all()
        self.fields['category'].choices = [ (x.id, x.category) for x in prog.class_categories.all() ]
        # grade_min, grade_max: crmi.getClassGrades
        self.fields['grade_min'].choices = class_grades
        self.fields['grade_max'].choices = class_grades
        if Tag.getTag('grade_ranges'):
            grade_ranges = json.loads(Tag.getTag('grade_ranges'))
            self.fields['grade_range'].choices = [(range,str(range[0]) + " - " + str(range[1])) for range in grade_ranges]
            self.fields['grade_range'].required = True
            hide_field( self.fields['grade_min'] )
            self.fields['grade_min'].required = False
            hide_field( self.fields['grade_max'] )
            self.fields['grade_max'].required = False
        else:
            hide_field( self.fields['grade_range'] )
        if crmi.use_class_size_max:
            # class_size_max: crmi.getClassSizes
            self.fields['class_size_max'].choices = class_sizes
        else:
            del self.fields['class_size_max']

        if Tag.getBooleanTag('use_class_size_optimal', default=False):
            if not crmi.use_class_size_optimal:
                del self.fields['class_size_optimal']

            if crmi.use_optimal_class_size_range:
                self.fields['optimal_class_size_range'].choices = class_ranges
            else:
                del self.fields['optimal_class_size_range']

            if crmi.use_allowable_class_size_ranges:
                self.fields['allowable_class_size_ranges'].choices = class_ranges
            else:
                del self.fields['allowable_class_size_ranges']
        else:
            del self.fields['class_size_optimal']
            del self.fields['optimal_class_size_range']
            del self.fields['allowable_class_size_ranges']

        # decide whether to display certain fields

        # prereqs
        if not crmi.set_prereqs:
            self.fields['prereqs'].widget = forms.HiddenInput()

        # allow_lateness
        if not crmi.allow_lateness:
            self.fields['allow_lateness'].widget = forms.HiddenInput()
            self.fields['allow_lateness'].initial = 'False'

        self.fields['duration'].choices = sorted(crmi.getDurations())
        hide_choice_if_useless( self.fields['duration'] )

        # session_count
        if crmi.session_counts:
            session_count_choices = crmi.session_counts_ints
            session_count_choices = zip(session_count_choices, session_count_choices)
            self.fields['session_count'].choices = session_count_choices
        hide_choice_if_useless( self.fields['session_count'] )

        # requested_room
        if not crmi.ask_for_room:
            hide_field( self.fields['requested_room'] )

        #   Hide resource fields since separate forms are now being used. - Michael P
        #   Most have now been removed, but this one gets un-hidden by open classes.
        self.fields['requested_special_resources'].widget = forms.HiddenInput()

        #   Add program-custom form components (for inlining additional questions without
        #   introducing a separate program module)
        custom_fields = get_custom_fields()
        for field_name in custom_fields:
            self.fields[field_name] = custom_fields[field_name]

        #   Modify help text on these fields if necessary.
        #   TODO(benkraft): Is there a reason not to allow this on all fields?
        custom_helptext_fields = [
            'duration', 'class_size_max', 'num_sections', 'requested_room',
            'message_for_directors', 'purchase_requests', 'class_info',
            'grade_max', 'grade_min'] + custom_fields.keys()
        for field in custom_helptext_fields:
            tag_data = Tag.getProgramTag('teacherreg_label_%s' % field, prog)
            if tag_data:
                self.fields[field].label = tag_data
            tag_data = Tag.getProgramTag('teacherreg_help_text_%s' % field, prog)
            if tag_data:
                self.fields[field].help_text = tag_data

        #   Hide fields as desired.
        tag_data = Tag.getProgramTag('teacherreg_hide_fields', prog)
        if tag_data:
            for field_name in tag_data.split(','):
                hide_field(self.fields[field_name])

        tag_data = Tag.getProgramTag('teacherreg_default_min_grade', prog)
        if tag_data:
            self.fields['grade_min'].initial = tag_data

        tag_data = Tag.getProgramTag('teacherreg_default_max_grade', prog)
        if tag_data:
            self.fields['grade_max'].initial = tag_data

        tag_data = Tag.getProgramTag('teacherreg_default_class_size_max', prog)
        if tag_data:
            self.fields['class_size_max'].initial = tag_data

        #   Rewrite difficulty label/choices if desired:
        if Tag.getTag('teacherreg_difficulty_choices'):
            self.fields['hardness_rating'].choices = json.loads(Tag.getTag('teacherreg_difficulty_choices'))

        # Get class_style_choices from tag, otherwise hide the field
        if Tag.getTag('class_style_choices'):
            self.fields['class_style'].choices = json.loads(Tag.getTag('class_style_choices'))
            self.fields['class_style'].required = True
        else:
            hide_field(self.fields['class_style'])