darklow / django-suit

Modern theme for Django admin interface
http://djangosuit.com/
Other
2.33k stars 704 forks source link

get suit_form_tabs #235

Open jab3z opened 10 years ago

jab3z commented 10 years ago

Django admin provides a method for each one of it's provided options for editing before displaying them in template. I really would need a similar method on suit_form_tabs.

Thank you.

darklow commented 10 years ago

Could you please describe more detailed, i couldn't understand what was your idea.

jab3z commented 10 years ago

get_inline_instances(request, obj=None) is returning the inlines I need to display in the view. In my case, I will return more or less inline instances depending what user is requesting them. And for organizing inlines in tabs I will have to define the tabs inside suit_form_tabs tuple, yes? Now, what if I defined a tuple within suit_form_tabs but there will be no consistent inline for it? I will get an empty tab. How I can avoid this? If the tab is empty, it shouldn't be displayed.

maximereinhart commented 9 years ago

Hi, jab3z do you have a solution ? Cause I need it too. My user needs permission for see differents tabs so I use def get_fieldsets(self, request, obj=None), but like you I get an empty tab. So I would like change suit_form_tabs too before display it.

jab3z commented 9 years ago

HI Max,

Sorry for my late answer. I don't remember exactly how I fixed it because it was long time ago, but I think I overriden admin.ModelAdmin method get_inline_instances.

nchampsavoir commented 9 years ago

Hi,

Here is one way to do it, though it feels a little hack-ish:

import threading

_thread_locals = threading.local()

class MyAdmin(admin.ModelAdmin):

def get_form(self, request, obj=None):

_thread_locals.obj = obj

_thread_locals.request = request

return super(MyAdmin, self).get_form(request, obj)

@property

def suit_form_tabs(self):

return self.get_suit_form_tabs(_thread_locals.request,

_thread_locals.obj)

def get_suit_form_tabs(self, request, obj):

# Your code goes here...

if obj is None:

  return ('Tab1', 'Tab2')

else:

  return ('Tab3', 'Tab4')

Hope that helps, Cheers.

2014-12-30 18:51 GMT+04:00 jab3z notifications@github.com:

HI Max,

Sorry for my late answer. I don't remember exactly how I fixed it because it was long time ago, but I think I overriden admin.ModelAdmin method get_inline_instances.

— Reply to this email directly or view it on GitHub https://github.com/darklow/django-suit/issues/235#issuecomment-68362057.

darklow commented 9 years ago

Here is a less hack-ish method:

class MyAdmin(admin.ModelAdmin):
    def get_form(self, request, obj=None, **kwargs):
        self._request = request
        return super(MyAdmin, self).get_form(request, obj, **kwargs)

    @property
    def suit_form_tabs(self):
        tabs = [('general', 'General')]
        # Add your own condition here and change tabs 
        # based on `self._request.user` or any other params
        if 'test' in self._request.GET:
            tabs.append(('advanced', 'Advanced'))
        return tabs
maximereinhart commented 9 years ago

Ok thx, works for me !

ilubnon commented 9 years ago

You can pass a "context" for a suit_form_includes?

 suit_form_includes = (
       ('includetest.html', '', 'tab_name'), #context
 )  

in includetest.html...

<h1> Test </h1>
<h1> Name: {{context}} </h1>
darklow commented 9 years ago

Context should be there already, try {{ original }} or {{ original.pk }} in include.html to access instance of active object

ilubnon commented 9 years ago

Hello, thanks for the reply ... But? nothing renders the context.

In model.py

class TestInclude(models.Model):
  name = models.CharField(max_length=60, verbose_name=u'Nome', null=False, blank=False)

In admin.py

class TestIncludeAdmin(admin.ModelAdmin):
  list_display = ('name', )
  list_filter = ['name',]
  search_fields = ['name', ]  
  fieldsets = [
          (None, {
              'classes': ('suit-tab', 'suit-tab-general',),
              'fields': ['name', ]
          }),
      ]

  suit_form_tabs = (('general', 'Geral'), ('notification', 'Notificações'))

  suit_form_includes = (
        ('<path>/include.html', '', 'notification'),
    )  

In include.html

{% load i18n admin_static suit_tags %}
{% load url from future %}
{% load staticfiles %}

<h1> Testando... </h1>
<h1> Contexto: {{ name }} </h1>
darklow commented 9 years ago

Have you tried {{ original.name }}? suit_form_includes feature does nothing to context. It is just a shortcut for include into change_form.html so you can access whatever context is available there. https://github.com/darklow/django-suit/blob/develop/suit/templates/admin/change_form.html#L156