Commit 3f000279 authored by Aymeric Barantal's avatar Aymeric Barantal

filter available organization and campaign in admin forms, second part

parent b19fcb0c
...@@ -50,7 +50,6 @@ class ArgumentaryAdmin(admin.ModelAdmin): ...@@ -50,7 +50,6 @@ class ArgumentaryAdmin(admin.ModelAdmin):
if db_field.name == "campaign": if db_field.name == "campaign":
orgs = request.user.organizations.all() orgs = request.user.organizations.all()
query = {'organization__in': orgs} query = {'organization__in': orgs}
print "##### QUERY %r" % query
kwargs["queryset"] = Campaign.objects.filter(**query) kwargs["queryset"] = Campaign.objects.filter(**query)
return super(ArgumentaryAdmin, self).formfield_for_foreignkey(db_field, return super(ArgumentaryAdmin, self).formfield_for_foreignkey(db_field,
request, request,
......
...@@ -21,6 +21,15 @@ class GroupTypeAdmin(admin.ModelAdmin): ...@@ -21,6 +21,15 @@ class GroupTypeAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()] user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs) return qs.filter(organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
orgs = request.user.organizations.all()
query = {'id__in': orgs}
kwargs["queryset"] = Organization.objects.filter(**query)
return super(GroupTypeAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
class GroupAdmin(admin.ModelAdmin): class GroupAdmin(admin.ModelAdmin):
def get_queryset(self, request): def get_queryset(self, request):
...@@ -30,6 +39,19 @@ class GroupAdmin(admin.ModelAdmin): ...@@ -30,6 +39,19 @@ class GroupAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()] user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs) return qs.filter(organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
orgs = request.user.organizations.all()
query = {'id__in': orgs}
kwargs["queryset"] = Organization.objects.filter(**query)
elif db_field.name == "type":
orgs = request.user.organizations.all()
query = {'organization__in': orgs}
kwargs["queryset"] = GroupType.objects.filter(**query)
return super(GroupAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
class CategoryAdmin(admin.ModelAdmin): class CategoryAdmin(admin.ModelAdmin):
def get_queryset(self, request): def get_queryset(self, request):
...@@ -39,6 +61,15 @@ class CategoryAdmin(admin.ModelAdmin): ...@@ -39,6 +61,15 @@ class CategoryAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()] user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs) return qs.filter(organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
orgs = request.user.organizations.all()
query = {'id__in': orgs}
kwargs["queryset"] = Organization.objects.filter(**query)
return super(CategoryAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
admin.site.register(Organization, OrganizationAdmin) admin.site.register(Organization, OrganizationAdmin)
admin.site.register(GroupType, GroupTypeAdmin) admin.site.register(GroupType, GroupTypeAdmin)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment