Commit b19fcb0c authored by Aymeric Barantal's avatar Aymeric Barantal

filter available organization and campaign in admin forms

parent 82b2f88b
from django.contrib import admin
from picampaign.campaign.models import (Campaign, CampaignContact,
Argumentary)
from picampaign.campaign.models import Campaign, CampaignContact, Argumentary
from picampaign.organization.models import Organization
class InlineContact(admin.TabularInline):
......@@ -17,6 +17,15 @@ class CampaignAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "organization":
query = {'id__in': [x.id
for x in request.user.organizations.all()]}
kwargs["queryset"] = Organization.objects.filter(**query)
return super(CampaignAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
class CampaignContactAdmin(admin.ModelAdmin):
......@@ -37,6 +46,16 @@ class ArgumentaryAdmin(admin.ModelAdmin):
user_orgs = [x.id for x in request.user.organizations.all()]
return qs.filter(campaign__organization__in=user_orgs)
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == "campaign":
orgs = request.user.organizations.all()
query = {'organization__in': orgs}
print "##### QUERY %r" % query
kwargs["queryset"] = Campaign.objects.filter(**query)
return super(ArgumentaryAdmin, self).formfield_for_foreignkey(db_field,
request,
**kwargs)
admin.site.register(Campaign, CampaignAdmin)
admin.site.register(CampaignContact, CampaignContactAdmin)
......
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