Exemplo n.º 1
0
    def render(self, name, value, attrs=None):
        inputfield = super(MediaFileWidget, self).render(name, value, attrs)
        if value:
            try:
                mf = MediaFile.objects.get(pk=value)
            except MediaFile.DoesNotExist:
                return inputfield

            try:
                caption = mf.translation.caption
            except (AttributeError, ObjectDoesNotExist):
                caption = _('(no caption)')

            image = admin_thumbnail(mf)
            if image:
                image = u'<img src="%(url)s" alt="" /><br />' % {'url': image}
            else:
                image = u''

            return mark_safe(u"""
                <div style="margin-left:10em">%(image)s
                <a href="%(url)s" target="_blank">%(caption)s - %(url)s</a><br />
                %(inputfield)s
                </div>""" % {
                    'image': image,
                    'url': mf.file.url,
                    'caption': caption,
                    'inputfield': inputfield})

        return inputfield
Exemplo n.º 2
0
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 = [
            '&nbsp;<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 ''
Exemplo n.º 3
0
    def mediafile_reverse_url(self, url, ctx):
        """
        Tries to intuit what the user dropped onto us. This might be
        a link to a MediaFile in case she dragged the "Title" column
        over, or it might be a link to a file in the media library
        if she dragged the image itself.
        """
        mediafile = None

        try:
            if url.path.startswith(self.mediachange_url):
                # Dropped a MediaFile url
                rest = url.path[len(self.mediachange_url):-1]
                mediafile = MediaFile.objects.get(pk=int(rest))
            elif url.path.startswith(self.mediaurl.path):
                # Dropped an image url (from media library)
                file_path = url.path[len(self.mediaurl.path):]
                mediafile = MediaFile.objects.get(file=file_path)
        except MediaFile.DoesNotExist:
            pass
        else:
            if mediafile is not None:
                logger.debug("%s converted \"%s\" into %s(pk=%d)",
                             self.__class__.__name__, url.path, mediafile.__class__.__name__, mediafile.pk)

                image = admin_thumbnail(mediafile, dimensions="150x100")

                ctx['mediafile_id'] = mediafile.id
                ctx['mediafile_type'] = mediafile.type
                ctx['mediafile_caption'] = unicode(mediafile)
                ctx['mediafile_url'] = image
                ctx['status'] = 200
Exemplo n.º 4
0
    def render(self, name, value, attrs=None):
        inputfield = super(MediaFileWidget, self).render(name, value, attrs)
        if value:
            try:
                mf = MediaFile.objects.get(pk=value)
            except MediaFile.DoesNotExist:
                return inputfield

            try:
                caption = mf.translation.caption
            except (AttributeError, ObjectDoesNotExist):
                caption = _('(no caption)')

            image = admin_thumbnail(mf)
            if image:
                image = u'<img src="%(url)s" alt="" /><br />' % {'url': image}
            else:
                image = u''

            return mark_safe(
                u"""
                <div style="margin-left:10em">%(image)s
                <a href="%(url)s" target="_blank">%(caption)s - %(url)s</a><br />
                %(inputfield)s
                </div>""" % {
                    'image': image,
                    'url': mf.file.url,
                    'caption': caption,
                    'inputfield': inputfield
                })

        return inputfield
Exemplo n.º 5
0
    def admin_thumbnail(self, instance):
        if not instance.image:
            return u''
        image = admin_thumbnail(instance.image)
        if image:
            return mark_safe(u"""
                <a href="%(url)s" target="_blank">
                    <img src="%(image)s" alt="" />
                </a>""" % {
                'url': instance.image.url,
                'image': image,
            })

        return u''
Exemplo n.º 6
0
    def admin_thumbnail(self, instance):
        if not instance.image:
            return u''
        image = admin_thumbnail(instance.image)
        if image:
            return mark_safe(
                u"""
                <a href="%(url)s" target="_blank">
                    <img src="%(image)s" alt="" />
                </a>""" % {
                    'url': instance.image.url,
                    'image': image,
                })

        return u''
Exemplo n.º 7
0
    def render(self, name, value, attrs=None):
        html = super(MediaFileWidget, self).render(name, value, attrs)

        if value:
            try:
                id = int(value)
                mf = MediaFile.objects.get(pk=id)
            except (MediaFile.DoesNotExist, ValueError):
                return html

            image = admin_thumbnail(mf, dimensions="150x100")
            if image:
                image = u'<img class="image-preview" src="%(url)s" />' % {'url': image}
                html = html + image
                return mark_safe(html)

        return html
Exemplo n.º 8
0
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 = ['&nbsp;<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 ''