def test_float_format_input(self): input_output = [(Decimal('5.00'), '5'), (Decimal('3E1'), '30'), (Decimal('5'), '5'), (None, None), ('aa', 'aa'), (5.0, '5')] for (inp, outp) in input_output: with self.subTest(inp=inp, outp=outp): float_format_input = DecimalFormatInput(render_value=True) context = float_format_input.get_context(name='yolo', value=inp, attrs=None) self.assertEqual(outp, context['widget']['value'])
class Meta: model = LearningUnitYear fields = ('academic_year', 'acronym', 'specific_title', 'specific_title_english', 'credits', 'session', 'quadrimester', 'status', 'internship_subtype', 'attribution_procedure', 'professional_integration', 'campus', 'language', 'periodicity') field_classes = {'acronym': AcronymField} error_messages = { 'credits': { # Override unwanted DecimalField standard error messages 'max_digits': _('Ensure this value is less than or equal to %(limit_value)s.' ) % { 'limit_value': MAXIMUM_CREDITS }, 'max_whole_digits': _('Ensure this value is less than or equal to %(limit_value)s.' ) % { 'limit_value': MAXIMUM_CREDITS } } } widgets = { 'credits': DecimalFormatInput(render_value=True), }
def __init__(self, *args, **kwargs): self.component = kwargs.pop('component') self.learning_unit_year = kwargs.pop('learning_unit_year') self.entities = kwargs.pop('entities', []) self.is_faculty_manager = kwargs.pop('is_faculty_manager', False) self.title = _(self.component.get_type_display( )) + ' ' if self.component.type else self.component.acronym self.title_help = _(self.component.get_type_display() ) + ' ' if self.component.type else '' self.title_help += self.component.acronym super().__init__(*args, **kwargs) help_volume_global = "{} = {} * {}".format(_('volume total global'), _('Volume total annual'), _('Planned classes')) # Append dynamic fields entities_to_add = [ entity for entity in REQUIREMENT_ENTITIES if entity in self.entities ] size_entities_to_add = len(entities_to_add) if size_entities_to_add > 1: self.fields["opening_brackets_entities_field"] = EmptyField( label='[') for i, key in enumerate(entities_to_add): entity = self.entities[key] if entity: self.fields["volume_" + key.lower()] = VolumeField( label=entity.acronym, help_text=entity.title, widget=DecimalFormatInput(render_value=True), required=False) if i != len(entities_to_add) - 1: self.fields["add" + key.lower()] = EmptyField(label='+') if size_entities_to_add > 1: self.fields["closing_brackets_entities_field"] = EmptyField( label=']') if self.is_faculty_manager \ and self.learning_unit_year.is_full() \ and self.learning_unit_year.learning_container_year.container_type \ in LEARNING_CONTAINER_YEAR_TYPES_CANT_UPDATE_BY_FACULTY: self._disable_central_manager_fields()
def __init__(self, *args, **kwargs): self.component = kwargs.pop('component') self.learning_unit_year = kwargs.pop('learning_unit_year') self.entities = kwargs.pop('entities', []) self.is_faculty_manager = kwargs.pop('is_faculty_manager', False) self.title = _(self.component.get_type_display( )) + ' ' if self.component.type else self.component.acronym self.title_help = _(self.component.get_type_display() ) + ' ' if self.component.type else '' self.title_help += self.component.acronym super().__init__(*args, **kwargs) # Append dynamic fields entities_to_add = [ entity for entity in REQUIREMENT_ENTITIES if entity in self.entities ] size_entities_to_add = len(entities_to_add) if size_entities_to_add > 1: self.fields["opening_brackets_entities_field"] = EmptyField( label='[') for i, key in enumerate(entities_to_add): entity = self.entities[key] if entity: self.fields["volume_" + key.lower()] = VolumeField( label=entity.acronym, help_text=entity.title, widget=DecimalFormatInput(render_value=True), required=False) if i != len(entities_to_add) - 1: self.fields["add" + key.lower()] = EmptyField(label='+') if size_entities_to_add > 1: self.fields["closing_brackets_entities_field"] = EmptyField( label=']') if self.is_faculty_manager \ and self.learning_unit_year.is_full() \ and self.learning_unit_year.learning_container_year.container_type \ in LCY_TYPES_WITH_FIXED_ACRONYM: self._disable_central_manager_fields()
class Meta: model = LearningComponentYear fields = ('hourly_volume_total_annual', 'hourly_volume_partial_q1', 'hourly_volume_partial_q2', 'planned_classes', 'repartition_volume_requirement_entity', 'repartition_volume_additional_entity_1', 'repartition_volume_additional_entity_2') widgets = { 'hourly_volume_total_annual': DecimalFormatInput(attrs={ 'title': _("The annual volume must be equal to the sum of the volumes Q1 and Q2" ) }, render_value=True), 'hourly_volume_partial_q1': DecimalFormatInput(attrs={ 'title': _("Volume Q1"), 'style': STYLE_MIN_WIDTH_VOLUME }, render_value=True), 'hourly_volume_partial_q2': DecimalFormatInput(attrs={ 'title': _("Volume Q2"), 'style': STYLE_MIN_WIDTH_VOLUME }, render_value=True), 'planned_classes': forms.TextInput(attrs={'title': _("Planned classes")}), 'repartition_volume_requirement_entity': DecimalFormatInput(attrs={'style': STYLE_MIN_WIDTH_VOLUME}, render_value=True), 'repartition_volume_additional_entity_1': DecimalFormatInput(attrs={'style': STYLE_MIN_WIDTH_VOLUME}, render_value=True), 'repartition_volume_additional_entity_2': DecimalFormatInput(attrs={'style': STYLE_MIN_WIDTH_VOLUME}, render_value=True), }
class VolumeEditionForm(forms.Form): requirement_entity_key = 'volume_' + entity_types.REQUIREMENT_ENTITY.lower( ) additional_requirement_entity_1_key = 'volume_' + entity_types.ADDITIONAL_REQUIREMENT_ENTITY_1.lower( ) additional_requirement_entity_2_key = 'volume_' + entity_types.ADDITIONAL_REQUIREMENT_ENTITY_2.lower( ) opening_brackets_field = EmptyField(label='[') opening_parenthesis_field = EmptyField(label='(') volume_q1 = VolumeField( label=_('Q1'), help_text=_('Volume Q1'), widget=DecimalFormatInput(render_value=True), required=False, ) add_field = EmptyField(label='+') volume_q2 = VolumeField( label=_('Q2'), help_text=_('Volume Q2'), widget=DecimalFormatInput(render_value=True), required=False, ) closing_parenthesis_field = EmptyField(label=')') equal_field_1 = EmptyField(label='=') volume_total = VolumeField( label=_('Vol. annual'), help_text= _('The annual volume must be equal to the sum of the volumes Q1 and Q2' ), widget=DecimalFormatInput(render_value=True), required=False, ) help_volume_total = "{} = {} + {}".format(_('Volume total annual'), _('Volume Q1'), _('Volume Q2')) closing_brackets_field = EmptyField(label=']') mult_field = EmptyField(label='*') planned_classes = forms.IntegerField(label=_('Planned classes'), help_text=_('Planned classes'), min_value=0, widget=forms.TextInput(), required=False) equal_field_2 = EmptyField(label='=') _post_errors = [] _parent_data = {} _faculty_manager_fields = ['volume_q1', 'volume_q2'] def __init__(self, *args, **kwargs): self.component = kwargs.pop('component') self.learning_unit_year = kwargs.pop('learning_unit_year') self.entities = kwargs.pop('entities', []) self.is_faculty_manager = kwargs.pop('is_faculty_manager', False) self.title = _(self.component.get_type_display( )) + ' ' if self.component.type else self.component.acronym self.title_help = _(self.component.get_type_display() ) + ' ' if self.component.type else '' self.title_help += self.component.acronym super().__init__(*args, **kwargs) help_volume_global = "{} = {} * {}".format(_('volume total global'), _('Volume total annual'), _('Planned classes')) # Append dynamic fields entities_to_add = [ entity for entity in REQUIREMENT_ENTITIES if entity in self.entities ] size_entities_to_add = len(entities_to_add) if size_entities_to_add > 1: self.fields["opening_brackets_entities_field"] = EmptyField( label='[') for i, key in enumerate(entities_to_add): entity = self.entities[key] if entity: self.fields["volume_" + key.lower()] = VolumeField( label=entity.acronym, help_text=entity.title, widget=DecimalFormatInput(render_value=True), required=False) if i != len(entities_to_add) - 1: self.fields["add" + key.lower()] = EmptyField(label='+') if size_entities_to_add > 1: self.fields["closing_brackets_entities_field"] = EmptyField( label=']') if self.is_faculty_manager \ and self.learning_unit_year.is_full() \ and self.learning_unit_year.learning_container_year.container_type \ in LEARNING_CONTAINER_YEAR_TYPES_CANT_UPDATE_BY_FACULTY: self._disable_central_manager_fields() def _disable_central_manager_fields(self): for key, field in self.fields.items(): if key not in self._faculty_manager_fields: field.disabled = True def clean(self): """ Prevent faculty users to a volume to 0 if there was a value other than 0. Also, prevent the faculty user from putting a volume if its value was 0. """ cleaned_data = super().clean() input_names = { 'volume_q1': 'volume_q1', 'volume_q2': 'volume_q2', 'volume_total': 'volume_total', 'planned_classes': 'planned_classes', 'volume_requirement_entity': 'volume_requirement_entity', 'volume_additional_requirement_entity_1': 'volume_additional_requirement_entity_1', 'volume_additional_requirement_entity_2': 'volume_additional_requirement_entity_2', } strategy = { True: CompleteVolumeEditionFacultyStrategy, False: VolumeEditionNoFacultyStrategy, } strategy[self.is_faculty_manager](self, input_names).is_valid() return cleaned_data def get_entity_fields(self): entity_keys = [ self.requirement_entity_key, self.additional_requirement_entity_1_key, self.additional_requirement_entity_2_key ] return [self.fields[key] for key in entity_keys if key in self.fields] def save(self, postponement): if not self.changed_data: return None conflict_report = {} if postponement: conflict_report = edition.get_postponement_conflict_report( self.learning_unit_year) luy_to_update_list = conflict_report['luy_without_conflict'] else: luy_to_update_list = [self.learning_unit_year] with transaction.atomic(): for component in self._find_learning_components_year( luy_to_update_list): self._save(component) # Show conflict error if exists check_postponement_conflict_report_errors(conflict_report) def _save(self, component): component.hourly_volume_total_annual = self.cleaned_data[ 'volume_total'] component.hourly_volume_partial_q1 = self.cleaned_data['volume_q1'] component.hourly_volume_partial_q2 = self.cleaned_data['volume_q2'] component.planned_classes = self.cleaned_data['planned_classes'] self._set_requirement_entities(component) return component.save() def _set_requirement_entities(self, component): updated_repartition_volumes = {} for entity_container_type in component.repartition_volumes.keys(): repartition_volume = self.cleaned_data.get( 'volume_' + entity_container_type.lower()) updated_repartition_volumes[ entity_container_type] = repartition_volume component.set_repartition_volumes(updated_repartition_volumes) def _find_learning_components_year(self, luy_to_update_list): return [ lcy for lcy in LearningComponentYear.objects.filter( learning_unit_year__in=luy_to_update_list) if lcy.type == self.component.type ]