import this
Meu nome é Igor Santos
Eu sou "programador",
Entusiasta Python e
apaixonado por desenvolvimento ágil
class Contact(object):
def __init__(self):
self.github = "igr-santos"
self.email = "igr.exe@gmail.com"
self.website = "http://pythonclub.com.br"
existem vários outros em DjangoPackages ...
from django.contrib import admin
from myapp.models import Author, Entry
class AuthorInline(admin.TabularInline):
model = Author
class EntryAdmin(admin.ModelAdmin):
inlines = [
AuthorInline,
]
fields = ('title', 'content', 'pub_date', 'published')
date_hierarchy = 'pub_date'
radio_fields = {'publish': admin.VERTICAL}
list_display = ('title', 'pub_date', 'published')
list_filter = ('pub_date', 'published')
list_display_links = ('title', )
search_fields = ('title', 'pub_date')
class Media:
js = (
'js/my_custom.js',
)
css = {
'': ('css/my_custom.css', )
}
admin.site.register(Entry, EntryAdmin)
class EntryAdmin(admin.ModelAdmin):
...
def get_readonly_fields(self, request, obj=None):
"""Regra n1"""
if request.user.is_superuser:
return []
fields = ['title', 'content', 'pub_date', 'published']
if obj and obj.author.filter(user=request.user).exists() and not obj.published:
fields = ['pub_date']
fields += self.readonly_fields
return fields
def get_queryset(self, request):
"""Regra 2"""
queryset = super(EntryAdmin, self).get_queryset(request)
if (request.user.is_superuser or
request.user.has_perm('entry.view_all')):
return queryset
elif request.user.has_perm('entry.view_my_entry'):
queryset = queryset.filter(author__in=[request.user, ])
return queryset
return queryset
...
...
class ErrorMixin(object):
def concat_error_template(self):
pass
class EntryAdmin(ErrorMixin, admin.ModelAdmin):
...
def get_users_for_complete_query(self):
pass
def get_category_by_instance(self, obj):
pass
def create_relationships(self):
pass
def remove_date_hierarchy(self):
pass
def fix_status(self):
pass
...
class EntryAdmin(ErrorMixin, admin.ModelAdmin):
...
def get_urls(self):
urls = super(EntryAdmin, self).get_urls()
my_urls = patterns(
'',
(r'(?P<pk>\d+)/chat/$',
self.admin_site.admin_view(self.chat_view)),
)
return my_urls + urls
def chat_view(self, request, pk):
instance = self.model.objects.get(pk=pk)
context = {
'title': u'Chat',
'object': instance,
'opts': self.model._meta,
}
form = ChatForm()
if request.method == 'POST':
form = ChatForm(request.POST)
if form.is_valid():
chat = form.save(commit=False)
chat.sender = request.user
chat.entry = instance
chat.save()
context['messages'] = Message.objects.filter(entry=pk)
context['form'] = form
return render(request, 'admin/myapp/chat.html', context)
...
Na minha humilde opnião . . .
Que a vida pode ser fácil no começo, mas depois fica muito dificil.
o Django Admin não foi feito para ser seu projeto, e sim para fazer parte do seu projeto.
Que a força "para não quebrar a cara com o Django Admin" esteja com você!