-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
executable file
·40 lines (29 loc) · 1.35 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from django.contrib import admin
from django.http import Http404
class SubdomainAdmin(admin.ModelAdmin):
exclude = ('subdomain',)
def queryset(self,request):
qs = super(SubdomainAdmin, self).queryset(request)
return get_queryset(request,qs)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
the_model = db_field.related.parent_model
if hasattr(the_model,'subdomain'):
kwargs['queryset'] = the_model.objects.filter(subdomain=request.subdomain)
return super(SubdomainAdmin,self).formfield_for_foreignkey(db_field, request, **kwargs)
def save_model(self, request, obj, form, change):
if not change:
obj.subdomain = request.subdomain
obj.save()
def get_queryset(request,qs):
"""
Filter the objects displayed in the change_list to only
display those for the currently signed in user.
"""
#Super user going to asimpleerp.com/admin displays all entries
if request.mainsite and request.user.is_superuser:
return qs
#Super user or subdomain user going to their subdomain shows their entries
elif request.subdomain and (request.user.is_superuser or request.subdomain.user == request.user):
return qs.filter(subdomain=request.subdomain)
else:
raise Http404