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):
        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.º 3
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.º 4
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.º 5
0
 def render(self, name, value, attrs=None, *args, **kwargs):
     output = super(LinkedSelect, self).render(name, value, attrs=attrs, *args, **kwargs)
     model = self.choices.field.queryset.model
     to_field_name = self.choices.field.to_field_name or 'id'
     try:
         kwargs = {to_field_name:value}
         obj = model.objects.get(**kwargs)
         view_url = get_admin_change_url(obj)
         output += mark_safe('&nbsp;<a href="%s" target="_blank">view</a>&nbsp;' % (view_url,))
     except model.DoesNotExist:
         pass
     return output
Exemplo n.º 6
0
 def render(self, name, value, attrs=None, *args, **kwargs):
     output = super(LinkedSelect, self).render(name, value, attrs=attrs, *args, **kwargs)
     model = self.choices.field.queryset.model
     to_field_name = self.choices.field.to_field_name or 'id'
     try:
         kwargs = {to_field_name:value}
         obj = model.objects.get(**kwargs)
         view_url = utils.get_admin_change_url(obj)
         output += mark_safe('&nbsp;<a href="%s" target="_blank">view</a>&nbsp;' % (view_url,))
     except model.DoesNotExist:
         pass
     return output
Exemplo n.º 7
0
 def admin_url(self, obj=None):
     from utils import get_admin_change_url
     if not obj or not obj.id:
         return ''
     #obj = obj.get_edited_object()#This doesn't support multiple databases.
     obj = self.get_edited_object(obj)
     if not obj:
         return ''
     if hasattr(obj, 'get_admin_url'):
         url = obj.get_admin_url()
     else:
         url = get_admin_change_url(obj)
     return mark_safe('<a href="%s">%s</a>' % (url, url), )
Exemplo n.º 8
0
 def format(self, v, plaintext=False):
     try:
         assert self.template_type in ('button', 'raw'), \
             'Invalid template type: %s' % (self.template_type)
         url = utils.get_admin_change_url(v)
         label = self.label_template.format(name=str(v))
         if self.template_type == 'button':
             return ('<a href="%s" target="%s"><input type="button" ' + \
                     'value="%s" /></a>') % (url, self.target, label)
         else:
             return '<a href="%s" target="%s">%s</a>' \
                 % (url, self.target, label)
     except Exception, e:
         return str(e)
Exemplo n.º 9
0
 def format(self, v, plaintext=False):
     try:
         assert self.template_type in ('button', 'raw'), \
             'Invalid template type: %s' % (self.template_type)
         url = utils.get_admin_change_url(v)
         label = self.label_template.format(name=str(v))
         if self.template_type == 'button':
             return ('<a href="%s" target="%s"><input type="button" ' + \
                     'value="%s" /></a>') % (url, self.target, label)
         else:
             return '<a href="%s" target="%s">%s</a>' \
                 % (url, self.target, label)
     except Exception, e:
         return str(e)
Exemplo n.º 10
0
 def admin_url(self, obj=None):
     from utils import get_admin_change_url
     if not obj or not obj.id:
         return ''
     #obj = obj.get_edited_object()#This doesn't support multiple databases.
     obj = self.get_edited_object(obj)
     if not obj:
         return ''
     if hasattr(obj, 'get_admin_url'):
         url = obj.get_admin_url()
     else:
         url = get_admin_change_url(obj)
     return mark_safe(
         '<a href="%s">%s</a>' % (url, url),
     )