def dl_component_tooltip(context, key, **kwargs): title = kwargs.get('title', '') default_if_none = kwargs.get('default_if_none', '') value = kwargs.get('value', '') id = kwargs.get('id', '') volumes = {} components_initial_data = context.get('differences', {}).get('components_initial_data', {}) if components_initial_data != {}: for rec in components_initial_data.get('components', {}): if rec.get('learning_component_year').get('id') == id: volumes = rec.get('volumes') break difference = get_component_volume_css(volumes, key, default_if_none, value) or 'title="{}"'.format( _(title)) value = get_style_of_value("", "", normalize_fraction(value)) html_id = "id='id_{}'".format(key.lower()) return mark_safe("<dl><dd {difference} {id}>{value}</dd></dl>".format( difference=difference, id=html_id, value=str(value))) return normalize_fraction(value) if value else default_if_none
def _get_error_volume_field_diff(field_diff, current_component, next_year_component, values_diff): current_value = values_diff.get('current') next_value = values_diff.get('next_year') return _( "The value of field '%(field)s' for the learning unit %(acronym)s (%(component_type)s) " "is different between year %(year)s - %(value)s and year %(next_year)s - %(next_value)s" ) % { 'field': COMPONENT_DETAILS[field_diff].lower(), 'acronym': current_component.learning_unit_year.acronym, 'component_type': dict(COMPONENT_TYPES)[current_component.type] if current_component.type else 'NT', 'year': current_component.learning_unit_year.academic_year, 'value': normalize_fraction(current_value) if current_value is not None else _('No data'), 'next_year': next_year_component.learning_unit_year.academic_year, 'next_value': normalize_fraction(next_value) if next_value is not None else _('No data') }
def get_difference_css(differences, parameter, default_if_none=""): if parameter in differences: value = differences[parameter] return mark_safe( ' data-toggle=tooltip title="{} : {}" class="{}" '.format( LABEL_VALUE_BEFORE_PROPOSAL, normalize_fraction(Decimal(value)) if parameter == "credits" else value or default_if_none, CSS_PROPOSAL_VALUE)) return None
def get_component_volume_css(values, parameter, default_if_none="", value=None): if parameter in values and values[parameter] != value: return mark_safe( " data-toggle=tooltip title='{} : {}' class='{}' ".format( LABEL_VALUE_BEFORE_PROPOSAL, normalize_fraction(values[parameter]) or default_if_none, CSS_PROPOSAL_VALUE ) ) return default_if_none
def dl_tooltip(context, instance, key, **kwargs): title = kwargs.get('title', '') label_text = kwargs.get('label_text', '') url = kwargs.get('url', '') default_if_none = kwargs.get('default_if_none', '') value = kwargs.get('value') inherited = kwargs.get('inherited', '') not_annualized = kwargs.get('not_annualized', '') differences = context['differences'] if not label_text: label_text = instance._meta.get_field(key).verbose_name.capitalize() if not value: value = get_verbose_field_value(instance, key) value = normalize_fraction(value) if isinstance(value, Decimal) else value difference = get_difference_css( differences, key, default_if_none) or 'title="{}"'.format( EXTERNAL_CREDIT_TOOLTIP if key == 'external_credits' else _(title)) if url: value = "<a href='{url}'>{value}</a>".format(value=value or '', url=url) if inherited == PARTIM: label_text = get_style_of_label_text( label_text, "color:grey", "The value of this attribute is inherited from the parent UE") value = get_style_of_value( "color:grey", "The value of this attribute is inherited from the parent UE", value) if not_annualized: label_text = get_style_of_label_text( label_text, "font-style:italic", "The value of this attribute is not annualized") value = get_style_of_value( "font-style:italic", "The value of this attribute is not annualized", value if value else default_if_none) return { 'difference': difference, 'id': key.lower(), 'label_text': label_text, 'value': value or '' }
def get_context(self, name, value, attrs): if isinstance(value, float): value = Decimal(value) if isinstance(value, Decimal) and self.render_value: value = normalize_fraction(value) return super(FloatFormatInput, self).get_context(name, value, attrs)
def test_normalize_fraction(self): input_output = [(Decimal('5.00'), 5), (Decimal('3E1'), 30), (Decimal('5'), 5)] for (inp, outp) in input_output: with self.subTest(inp=inp, outp=outp): self.assertEqual(outp, numbers.normalize_fraction(inp))