def label_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.to._default_manager.using(self.db).get(**{key: value}) label = [ ' <strong>%s</strong>' % escape(shorten_string(six.text_type(obj))) ] image = admin_thumbnail(obj) label.append(' <a href="{0}" target="_blank">preview</a>'.format( obj.file.url)) url = reverse('admin:{0}_{1}_change'.format(obj._meta.app_label, obj._meta.module_name), args=(obj.pk, )) label.append(' <a href="{0}" target="_blank">edit</a>'.format(url)) if image: label.append( '<br /><img src="%s" alt="" style="margin:1em 0 0 10em"' '/>' % image) return ''.join(label) except (ValueError, self.rel.to.DoesNotExist): return ''
def short_title(self): """ Title shortened for display. """ from feincms.utils import shorten_string return shorten_string(self.title)
def __init__(self, f, request, params, model, model_admin, field_path=None): from feincms.utils import shorten_string try: super(ParentFieldListFilter, self).__init__(f, request, params, model, model_admin, field_path) except TypeError: # Django 1.2 super(ParentFieldListFilter, self).__init__(f, request, params, model, model_admin) parent_ids = model.objects.exclude(parent=None).values_list( "parent__id", flat=True).order_by("parent__id").distinct() parents = model.objects.filter(pk__in=parent_ids).values_list( "pk", "title", "level") self.lookup_choices = [ (pk, "%s%s" % (" " * level, shorten_string(title, max_length=25))) for pk, title, level in parents ]
def test_shorten_string(self): string = shorten_string( "Der Wolf und die Grossmutter assen im Wald zu mittag", 15, ellipsis="_" ) self.assertEqual(string, "Der Wolf und_ag") self.assertEqual(len(string), 15) string = shorten_string( "Haenschen-Klein, ging allein, in den tiefen Wald hinein", 15 ) self.assertEqual(string, "Haenschen \u2026 ein") self.assertEqual(len(string), 15) string = shorten_string("Badgerbadgerbadgerbadgerbadger", 10, ellipsis="-") self.assertEqual(string, "Badger-ger") self.assertEqual(len(string), 10)
def __init__(self, f, request, params, model, model_admin): from feincms.utils import shorten_string super(ParentFilterSpec, self).__init__(f, request, params, model, model_admin) parent_ids = model.objects.exclude(parent=None).values_list("parent__id", flat=True).order_by("parent__id").distinct() parents = model.objects.filter(pk__in=parent_ids).values_list("pk", "title", "level") self.lookup_choices = [(pk, "%s%s" % (" " * level, shorten_string(title, max_length=25))) for pk, title, level in parents]
def test_shorten_string(self): string = shorten_string( u"Der Wolf und die Grossmutter assen im Wald zu mittag", 15, ellipsis=u"_") self.assertEqual(string, u'Der Wolf und_ag') self.assertEqual(len(string), 15) string = shorten_string( u"Haenschen-Klein, ging allein, in den tiefen Wald hinein", 15) self.assertEqual(string, u'Haenschen \u2026 ein') self.assertEqual(len(string), 15) string = shorten_string( u'Badgerbadgerbadgerbadgerbadger', 10, ellipsis=u'-') self.assertEqual(string, u'Badger-ger') self.assertEqual(len(string), 10)
def file_info(self): """ Method for showing the file name in admin. Note: This also includes a hidden field that can be used to extract the file name later on, this can be used to access the file name from JS, like for example a TinyMCE connector shim. """ from os.path import basename from feincms.utils import shorten_string return u'<input type="hidden" class="medialibrary_file_path" name="_media_path_%d" value="%s" /> %s' % ( self.id, self.file.name, shorten_string(basename(self.file.name), max_length = 28),)
def label_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.to._default_manager.using(self.db).get(**{key: value}) label = [u' <strong>%s</strong>' % escape( shorten_string(six.text_type(obj)))] image = admin_thumbnail(obj) if image: label.append(u'<br /><img src="%s" alt="" style="margin:1em 0 0 10em" />' % image) return u''.join(label) except (ValueError, self.rel.to.DoesNotExist): return ''
def file_info(self): """ Method for showing the file name in admin. Note: This also includes a hidden field that can be used to extract the file name later on, this can be used to access the file name from JS, like for example a TinyMCE connector shim. """ from os.path import basename from feincms.utils import shorten_string return u'<input type="hidden" class="medialibrary_file_path" name="_media_path_%d" value="%s" /> %s' % ( self.id, self.file.name, shorten_string(basename(self.file.name), max_length=28), )
def file_info(self, obj): """ Method for showing the file name in admin. Note: This also includes a hidden field that can be used to extract the file name later on, this can be used to access the file name from JS, like for example a TinyMCE connector shim. """ from feincms.utils import shorten_string return u'<input type="hidden" class="medialibrary_file_path" name="_media_path_%d" value="%s" id="_refkey_%d" /> %s <br />%s, %s' % ( obj.id, obj.file.name, obj.id, shorten_string(os.path.basename(obj.file.name), max_length=40), self.file_type(obj), self.formatted_file_size(obj), )
def label_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.to._default_manager.using(self.db).get( **{key: value}) label = [' <strong>%s</strong>' % escape( shorten_string(six.text_type(obj)))] image = admin_thumbnail(obj) if image: label.append( '<br /><img src="%s" alt="" style="margin:1em 0 0 10em"' '/>' % image) return ''.join(label) except (ValueError, self.rel.to.DoesNotExist): return ''
def __init__(self, f, request, params, model, model_admin, field_path=None): super(ParentFieldListFilter, self).__init__( f, request, params, model, model_admin, field_path ) parent_ids = ( model.objects.exclude(parent=None) .values_list("parent__id", flat=True) .order_by("parent__id") .distinct() ) parents = model.objects.filter(pk__in=parent_ids).values_list( "pk", "title", "level" ) self.lookup_choices = [ ( pk, "%s%s" % (" " * level, shorten_string(title, max_length=25)), ) for pk, title, level in parents ]
def __init__(self, f, request, params, model, model_admin, field_path, **kwargs): from feincms.utils import shorten_string to = f.rel.to opts = to._mptt_meta self.mppt_lookups = { opts.tree_id_attr: '%s__exact' % (opts.tree_id_attr), opts.left_attr: '%s__gte' % (opts.left_attr), opts.right_attr: '%s__lte' % (opts.left_attr) } super(MPTTFilterSpec, self).__init__(f, request, params, model, model_admin, field_path) parents = to.objects.all() if self.field_path == opts.parent_attr: # Filtering on (parent) relation to self. parent_id = "%s__id" % opts.parent_attr parent_ids = to.objects.exclude(parent=None).values_list(parent_id, flat=True).order_by(parent_id).distinct() parents = parents.filter(pk__in=parent_ids) self.title_suffix = '' else: # Filtering on relation to other object. self.title_suffix = ' ' + unicode(self.title) for k, v in self.mppt_lookups.items(): self.mppt_lookups[k] = "%s__%s" % (self.field_path, v) self.title = _('Ancestor') + self.title_suffix self.lookup_kwargs = self.mppt_lookups.values() self.lookup_params = dict([(k, request.GET.get(k, None)) for k in self.lookup_kwargs]) self.lookup_choices = [ ( # Indented title "%s%s" % (" " * getattr(parent, opts.level_attr), shorten_string(unicode(parent), max_length=25)), # MPTT lookup params dict([(lookup, str(getattr(parent, kwarg))) for kwarg, lookup in self.mppt_lookups.items()]) ) for parent in parents]
def __init__(self, f, request, params, model, model_admin, field_path, **kwargs): from feincms.utils import shorten_string to = f.rel.to opts = to._mptt_meta self.mppt_lookups = { opts.tree_id_attr: '%s__exact' % (opts.tree_id_attr), opts.left_attr: '%s__gte' % (opts.left_attr), opts.right_attr: '%s__lte' % (opts.left_attr) } super(MPTTFilterSpec, self).__init__(f, request, params, model, model_admin, field_path) parents = to.objects.all() if self.field_path == opts.parent_attr: # Filtering on (parent) relation to self. parent_id = "%s__id" % opts.parent_attr parent_ids = to.objects.exclude(parent=None).values_list(parent_id, flat=True).order_by(parent_id).distinct() parents = parents.filter(pk__in=parent_ids) self.title_suffix = '' else: # Filtering on relation to other object. self.title_suffix = ' ' + str(self.title) for k, v in self.mppt_lookups.items(): self.mppt_lookups[k] = "%s__%s" % (self.field_path, v) self.title = _('Ancestor') + self.title_suffix self.lookup_kwargs = self.mppt_lookups.values() self.lookup_params = dict([(k, request.GET.get(k, None)) for k in self.lookup_kwargs]) self.lookup_choices = [ ( # Indented title "%s%s" % (" " * getattr(parent, opts.level_attr), shorten_string(str(parent), max_length=25)), # MPTT lookup params dict([(lookup, str(getattr(parent, kwarg))) for kwarg, lookup in self.mppt_lookups.items()]) ) for parent in parents]
def __init__(self, f, request, params, model, model_admin, field_path=None): from feincms.utils import shorten_string try: super(ParentFieldListFilter, self).__init__(f, request, params, model, model_admin, field_path) except TypeError: # Django 1.2 super(ParentFieldListFilter, self).__init__(f, request, params, model, model_admin) # If the sites extension is installed, only show the parents of this site if hasattr(Site, 'page_set'): # What site is the admin displaying? if request.GET.has_key('site__id__exact'): admin_site = request.GET['site__id__exact'] else: # No site filtered in admin, so show current site admin_site = django_settings.SITE_ID parent_ids = model.objects.filter(site__id__exact=admin_site).exclude(parent=None).values_list("parent__id", flat=True).order_by("parent__id").distinct() else: parent_ids = model.objects.exclude(parent=None).values_list("parent__id", flat=True).order_by("parent__id").distinct() parents = model.objects.filter(pk__in=parent_ids).values_list("pk", "title", "level") self.lookup_choices = [(pk, "%s%s" % (" " * level, shorten_string(title, max_length=25))) for pk, title, level in parents]
def label_for_value(self, value): key = self.rel.get_related_field().name try: obj = self.rel.to._default_manager.using(self.db).get( **{key: value}) label = [' <strong>%s</strong>' % escape( shorten_string(six.text_type(obj)))] image = admin_thumbnail(obj) label.append(' <a href="{0}" target="_blank">preview</a>'.format(obj.file.url)) url = reverse('admin:{0}_{1}_change'.format(obj._meta.app_label, obj._meta.module_name), args=(obj.pk,)) label.append(' <a href="{0}" target="_blank">edit</a>'.format(url)) if image: label.append( '<br /><img src="%s" alt="" style="margin:1em 0 0 10em"' '/>' % image) return ''.join(label) except (ValueError, self.rel.to.DoesNotExist): return ''
def __init__(self, f, request, params, model, model_admin, field_path=None, **kwargs): from feincms.utils import shorten_string super(MPTTFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path) if getattr(self, 'field_path', None) is None: self.field_path = f.name to = f.rel.to opts = to._mptt_meta mppt_lookups = {opts.tree_id_attr: '%s__exact' % (opts.tree_id_attr), opts.left_attr: '%s__gte' % (opts.left_attr), opts.right_attr: '%s__lte' % (opts.left_attr)} parents = to.objects.all() if self.field_path == opts.parent_attr: parent_id = "%s__id" % opts.parent_attr parent_ids = to.objects.exclude(parent=None).values_list(parent_id, flat=True).order_by(parent_id).distinct() parents = parents.filter(pk__in=parent_ids) self.title_suffix = '' else: self.title_suffix = ' ' + str(super(MPTTFilterSpec, self).title()) for k, v in mppt_lookups.items(): mppt_lookups[k] = "%s__%s" % (self.field_path, v) self.lookup_kwargs = mppt_lookups.values() self.lookup_params = dict([(k, request.GET.get(k, None)) for k in self.lookup_kwargs]) self.lookup_choices = [("%s%s" % (" " * getattr(parent, opts.level_attr), shorten_string(str(parent), max_length=25)), dict([(lookup, str(getattr(parent, kwarg))) for kwarg, lookup in list(mppt_lookups.items())])) for parent in parents]
def short_title(self): """ Title shortened for display. """ return shorten_string(self.title)
def __init__(self, f, request, params, model, model_admin, field_path=None, **kwargs): from feincms.utils import shorten_string super(MPTTFilterSpec, self).__init__(f, request, params, model, model_admin, field_path=field_path) if getattr(self, 'field_path', None) is None: self.field_path = f.name to = f.rel.to opts = to._mptt_meta mppt_lookups = {opts.tree_id_attr: '%s__exact' % (opts.tree_id_attr), opts.left_attr: '%s__gte' % (opts.left_attr), opts.right_attr: '%s__lte' % (opts.left_attr)} parents = to.objects.all() if self.field_path == opts.parent_attr: parent_id = "%s__id" % opts.parent_attr parent_ids = to.objects.exclude(parent=None).values_list(parent_id, flat=True).order_by(parent_id).distinct() parents = parents.filter(pk__in=parent_ids) self.title_suffix = '' else: self.title_suffix = ' ' + unicode(super(MPTTFilterSpec, self).title()) for k, v in mppt_lookups.items(): mppt_lookups[k] = "%s__%s" % (self.field_path, v) self.lookup_kwargs = mppt_lookups.values() self.lookup_params = dict([(k, request.GET.get(k, None)) for k in self.lookup_kwargs]) self.lookup_choices = [("%s%s" % (" " * getattr(parent, opts.level_attr), shorten_string(unicode(parent), max_length=25)), dict([(lookup, str(getattr(parent, kwarg))) for kwarg, lookup in mppt_lookups.items()])) for parent in parents]
def short_title(self): return shorten_string(self.title)
def short_title(self): from feincms.utils import shorten_string return shorten_string(self.title)