Exemplo n.º 1
0
 def format(self, obj):
     try:
         url = None
         try:
             url = urlresolvers.reverse(self.url_param)
             url = '{0}?{1}={2}'.format(url, self.id_param, obj.id)
         except Exception:
             pass
         q = count = getattr(obj, self.name)
         if hasattr(q, 'count'):
             q = q.all()
             count = q.count()
             if count == 1:
                 # Link directly to the record if only one result.
                 link_obj = q[0]
                 url = utils.get_admin_change_url(link_obj)
             elif count > 1:
                 url = utils.get_admin_changelist_url(q[0])
                 url += '?{1}={2}'.format(url, self.id_param, obj.id)
         if count is None or count == 0:
             return count
         return ('<a href="%s" target="%s"><input type="button" ' + \
                 'value="View %d" /></a>') % (url, self.target, count)
     except Exception, e:
         return str(e)
Exemplo n.º 2
0
    def render(self, name, value, attrs=None):
        from django.template import Context, Template
        from django.template.context import Context
        if value is None:
            value = ''
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
        if value != '':
            # Only add the 'value' attribute if a value is non-empty.
            final_attrs['value'] = force_unicode(self._format_value(value))
        final_attrs['size'] = 10
        t = Template(u"""
{% load staticfiles %}
<input{{ attrs|safe }} />
{% if instance %}
    <a href="{{ changelist_url|safe }}?t=id" class="related-lookup" id="lookup_{{ id|safe }}" onclick="return showRelatedObjectLookupPopup(this);">
        <img src="{% static 'admin/img/selector-search.gif' %}" width="16" height="16" alt="Lookup" />
    </a>
    <strong><a href="{{ url|safe }}" target="_blank">{{ instance|safe }}</a></strong>
{% endif %}
        """)
        c = Context(dict(
            id=final_attrs['id'],
            attrs=flatatt(final_attrs),
            raw_value=self._raw_value,
            url=get_admin_change_url(self._instance),
            changelist_url=get_admin_changelist_url(self._model_class),
            instance=self._instance))
        return  mark_safe(t.render(c))
Exemplo n.º 3
0
    def render(self, name, value, attrs=None):
        if value is None:
            value = ''
        final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
        if value != '':
            # Only add the 'value' attribute if a value is non-empty.
            final_attrs['value'] = force_unicode(self._format_value(value))
        final_attrs['size'] = 10
        t = Template(u"""
{% load staticfiles %}
<input{{ attrs|safe }} />
{% if instance %}
    <a href="{{ changelist_url|safe }}?t=id" class="related-lookup" id="lookup_{{ id|safe }}" onclick="return showRelatedObjectLookupPopup(this);">
        <img src="{% static 'admin/img/selector-search.gif' %}" width="16" height="16" alt="Lookup" />
    </a>
    <strong><a href="{{ url|safe }}" target="_blank">{{ instance|safe }}</a></strong>
{% endif %}
        """)
        c = Context(
            dict(
                id=final_attrs['id'],
                attrs=flatatt(final_attrs),
                raw_value=self._raw_value,
                url=utils.get_admin_change_url(self._instance),
                changelist_url=utils.get_admin_changelist_url(
                    self._model_class),
                instance=self._instance,
            ))
        return mark_safe(t.render(c))
Exemplo n.º 4
0
 def format(self, obj):
     try:
         url = None
         try:
             url = urlresolvers.reverse(self.url_param)
             url = '{0}?{1}={2}'.format(url, self.id_param, obj.id)
         except Exception:
             pass
         q = count = getattr(obj, self.name)
         if hasattr(q, 'count'):
             q = q.all()
             count = q.count()
             if count == 1:
                 # Link directly to the record if only one result.
                 link_obj = q[0]
                 url = utils.get_admin_change_url(link_obj)
             elif count > 1:
                 url = utils.get_admin_changelist_url(q[0])
                 url += '?{1}={2}'.format(url, self.id_param, obj.id)
         if count is None or count == 0:
             return count
         return ('<a href="%s" target="%s"><input type="button" ' + \
                 'value="View %d" /></a>') % (url, self.target, count)
     except Exception, e:
         return str(e)