Пример #1
0
    def render(self, context):
        label, identifier = self.resolve_label_and_identifier(context)
        user = _resolve_variable(self.user, context)
        instance = _resolve_variable(self.instance, context)
        field_template = get_template(_resolve_variable(
                                      self.field_template, context))

        if not isinstance(identifier, basestring)\
           or not _identifier_regex.match(identifier):
            raise template.TemplateSyntaxError(
                'expected a string with the format "model.property" '
                'to follow "from" %s' % identifier)

        model_name, field_name = identifier.split('.', 1)
        model = self.get_model(context, model_name, instance)

        def _is_udf(model, udf_field_name):
            return (hasattr(model, 'udf_field_names') and
                    udf_field_name in model.udf_field_names)

        def _udf_dict(model, field_name):
            return model.get_user_defined_fields()\
                .filter(name=field_name.replace('udf:', ''))[0]\
                .datatype_dict

        def _field_type_label_choices(model, field_name, label):
            choices = None
            udf_field_name = field_name.replace('udf:', '')
            if not _is_udf(model, udf_field_name):
                field = model._meta.get_field(field_name)
                field_type = field.get_internal_type()
                try:
                    field_type = FieldNode._field_mappings[field_type]
                except KeyError:
                    raise Exception('This template tag only supports %s not %s'
                                    % (FieldNode._valid_field_keys,
                                       field_type))
                label = label if label else field.help_text
                choices = [{'value': choice[0], 'display_value': choice[1]}
                           for choice in field.choices]
                if choices and field.null:
                    choices = [{'value': '', 'display_value': ''}] + choices
            else:
                udf_dict = _udf_dict(model, field_name)
                field_type = udf_dict['type']
                label = label if label else udf_field_name
                if 'choices' in udf_dict:
                    values = [''] + udf_dict['choices']
                    choices = [{'value': value, 'display_value': value}
                               for value in values]

            return field_type, label, choices

        def _field_value(model, field_name):
            udf_field_name = field_name.replace('udf:', '')
            if field_name in model._meta.get_all_field_names():
                try:
                    val = getattr(model, field_name)
                except ObjectDoesNotExist:
                    val = None
            elif _is_udf(model, udf_field_name):
                val = model.udfs[udf_field_name]
            else:
                raise ValueError('Could not find field: %s' % field_name)

            return val

        if is_json_field_reference(field_name):
            field_value = get_attr_from_json_field(model, field_name)
            choices = None
            is_visible = is_editable = True
            data_type = "string"
        else:
            field_value = _field_value(model, field_name)
            data_type, label, choices = _field_type_label_choices(
                model, field_name, label)

            if user is not None and hasattr(model, 'field_is_visible'):
                is_visible = model.field_is_visible(user, field_name)
                is_editable = model.field_is_editable(user, field_name)
            else:
                # This tag can be used without specifying a user. In that case
                # we assume that the content is visible and upstream code is
                # responsible for only showing the content to the appropriate
                # user
                is_visible = True
                is_editable = True

        digits = (get_digits(model.instance, model_name, field_name)
                  if is_formattable(model_name, field_name) else '')

        units = ''
        if is_convertible(model_name, field_name):
            units = get_units(model.instance, model_name, field_name)

        if field_value is None:
            display_val = None
        elif data_type == 'date' and model.instance:
            display_val = dateformat.format(field_value,
                                            model.instance.short_date_format)
        elif data_type == 'date':
            display_val = dateformat.format(field_value,
                                            settings.SHORT_DATE_FORMAT)
        elif is_convertible_or_formattable(model_name, field_name):
            field_value, display_val = get_display_value(
                model.instance, model_name, field_name, field_value)
            if units is not '':
                display_val += (' %s' % units)
        else:
            display_val = unicode(field_value)

        context['field'] = {
            'label': label,
            'identifier': identifier,
            'value': field_value,
            'display_value': display_val,
            'units': units,
            'digits': digits,
            'data_type': data_type,
            'is_visible': is_visible,
            'is_editable': is_editable,
            'choices': choices
        }
        self.get_additional_context(context['field'])

        return field_template.render(context)
Пример #2
0
 def test_is_formatable(self):
     self.assertFalse(is_formattable('test', 'unit_only'))
     self.assertTrue(is_formattable('test', 'digit_only'))
     self.assertTrue(is_formattable('test', 'both'))
     self.assertFalse(is_formattable('test', 'separate_units'))
Пример #3
0
 def test_is_formatable(self):
     self.assertFalse(is_formattable('test', 'unit_only'))
     self.assertTrue(is_formattable('test', 'digit_only'))
     self.assertTrue(is_formattable('test', 'both'))
     self.assertFalse(is_formattable('test', 'separate_units'))
Пример #4
0
 def test_is_formatable(self):
     self.assertFalse(is_formattable("test", "unit_only"))
     self.assertTrue(is_formattable("test", "digit_only"))
     self.assertTrue(is_formattable("test", "both"))
     self.assertFalse(is_formattable("test", "separate_units"))