Programming in almost language

This is the site where you may share your knowledge and experience to eachother..

  • Categories

  • LinkedIn

  • Tweet Me

  • My footsteps

Customize admin permission

Posted by Praveen Kumar on May 18, 2009

Here, i am writing all the possible customize admin function.
If a user belong to a specific city then he/she could add the events for that city except others
class

EventAdmin(admin.ModelAdmin):

model = events

# Attributes

list_display = (‘title’, ‘event_type’,’city’,’join_name’)

list_display_links = (‘title’, ‘event_type’,’city’)

list_filter = [‘title’,’event_type’,’city’,’sites’]

list_select_related = True

search_fields = [‘title’]

save_on_top = True

raw_id_fields = (‘event_type’,’city’)

list_per_page = 2

prepopulated_fields = {‘title’: (‘summary’,’description’)}

fieldsets = (
(‘Add Event’, {‘fields’: (‘title’,’event_type’,’city’,’visibility’,’highlighted’,’sites’), ‘classes’: (‘wide’,)}),
(‘Additional Information’, {‘fields’ (‘summary’,’description’,’start_date’,’end_date’,’start_Time’,’end_Time’,’admission’), ‘classes’: (‘collapse’, ‘wide’)}),
(‘Address Information’, {‘fields’: (‘contact_detail’,’booking_url’,’venue’,’address’,’phone’,’mobile’,’image’,’image_caption’), ‘classes’: (‘collapse’, ‘wide’)}),
)

#A cool thing there is the ‘site_list’ column. By default, we can’t use a ManyToManyField

def site_list(self):

if self.sites:

sites = [site.name for site in self.sites.all()]
string = “, “.join(sites)
return string

# To give the edit permission city wise

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

#if obj and request.user.groups.name == ‘Chicago’:

#city=request.user.get_profile().res_city

if obj:

return obj.city == request.user.get_profile().res_city

return super(EventAdmin, self).has_change_permission(request, obj)

# To give the add permission city wise

def has_add_permission(self, request):

#if obj and request.user.groups.name == ‘Chicago’:

#city=request.user.get_profile().res_city

if request.user.get_profile().res_city:

return super(EventAdmin, self).has_add_permission(request)

# To return the records city wise

def queryset(self, request):

#return self.model._default_manager.filter(city=’Chicago’)

return self.model._default_manager.filter(city=request.user.get_profile().res_city)( OR )

def queryset(self, request):

qs = super(EventAdmin, self).queryset(request)

if request.user.is_superuser:

qs = qs.filter(city=request.user.get_profile().res_city)

return qs

#return self.model._default_manager.filter(city=request.user.get_profile().res_city)

admin.site.register(events,EventAdmin)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: