コード例 #1
0
ファイル: cli.py プロジェクト: gitter-badger/django-admin-cli
    def _get_field_width(self, modeladmin, field):
        """
        Try to define a column width from ``field`` string.

        :param modeladmin: ModelAdmin of model
        :type modeladmin: :class:`admin.ModelAdmin`

        :param field: Name of attribute, example ``'__str__'`` or ``'id'``
        :type field: ``str``

        :returns: Column's width
        :rtype: ``int``
        """
        field_obj = self._get_field_object(modeladmin, field)
        if isinstance(field_obj, models.CharField):
            return min(30, max(field_obj.max_length+1,
                       len(field_obj.verbose_name)))
        elif isinstance(field_obj, models.BooleanField):
            return max(6, len(self._get_field_name(modeladmin, field))+1)
        elif isinstance(field_obj, models.DateTimeField):
            return len(strftime(now(), settings.SHORT_DATETIME_FORMAT))+1
        elif isinstance(field_obj, models.DateField):
            return len(strftime(now(), settings.SHORT_DATE_FORMAT))+1
        else:
            return 21
コード例 #2
0
ファイル: cli.py プロジェクト: gitter-badger/django-admin-cli
    def _get_field_value(self, modeladmin, field, obj):
        """
        Get a value from ``field`` string.

        :param modeladmin: ModelAdmin of model
        :type modeladmin: :class:`admin.ModelAdmin`

        :param field: Name of attribute, example ``'__str__'`` or ``'id'``
        :type field: ``str``

        :param obj: Instance of model
        :type obj: :class:`models.Model`

        :returns: Value of attribute, called if callable
        :rtype: Trying ``str``
        """
        is_django16_fk = django.VERSION < (1, 7) and \
            field in obj._meta.get_all_field_names() and \
            isinstance(obj._meta.get_field(field), models.ForeignKey)
        if field.startswith('__'):
            value = getattr(obj, field)()
        # Django 1.6 doesn't support get empty FK
        elif is_django16_fk:  # pragma: no cover
            value = getattr(obj, obj._meta.get_field(field).attname)
        elif hasattr(modeladmin, field):
            value = getattr(modeladmin, field)
        elif hasattr(obj, field):
            value = getattr(obj, field)
            if isinstance(value, datetime):
                value = strftime(value, settings.SHORT_DATETIME_FORMAT)
            elif isinstance(value, date):
                value = strftime(value, settings.SHORT_DATE_FORMAT)
            elif value.__class__.__name__ == 'ManyRelatedManager':
                value = ','.join([str(a) for a in value.all()])
        else:
            value = 'N/A'
        if hasattr(value, '__call__'):
            # Some callable new instance as arg
            try:
                value = value(obj)
            except TypeError:
                value = value()
        # Remove HTML and new lines
        try:
            value = str(value).replace('\n', '')
            value = striptags(value)
        except:
            pass
        return value
コード例 #3
0
ファイル: tests.py プロジェクト: pombredanne/django-admin-cli
 def test_date(self):
     date = now().date()
     field = ['field='+strftime(date, se.SHORT_DATE_FORMAT)]
     call_command('cli', 'datemodel', 'add', field=field, stdout=self.stdout)
     self.assertEqual(1, models.DateModel.objects.count())
     self.assertEqual(date, models.DateModel.objects.get().field)