admin.py 2.54 KB
Newer Older
Aymeric Barantal's avatar
Aymeric Barantal committed
1
from django.contrib import admin
2 3
from picampaign.campaign.models import Campaign, CampaignContact, Argumentary
from picampaign.organization.models import Organization
okhin's avatar
okhin committed
4
from picampaign.importer.models import Importer
Aymeric Barantal's avatar
Aymeric Barantal committed
5

6 7 8 9

class InlineContact(admin.TabularInline):
    model = CampaignContact

okhin's avatar
okhin committed
10 11
class InlineImporter(admin.TabularInline):
    model = Importer
12 13

class CampaignAdmin(admin.ModelAdmin):
14
    inlines = [InlineContact, InlineImporter]
15

16 17 18 19 20 21 22
    def get_queryset(self, request):
        qs = super(CampaignAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        user_orgs = [x.id for x in request.user.organizations.all()]
        return qs.filter(organization__in=user_orgs)

23 24 25 26 27 28 29 30
    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)
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
class CampaignContactAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(CampaignContactAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        user_orgs = [x.id for x in request.user.organizations.all()]
        return qs.filter(campaign__organization__in=user_orgs)


class ArgumentaryAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(ArgumentaryAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        user_orgs = [x.id for x in request.user.organizations.all()]
        return qs.filter(campaign__organization__in=user_orgs)

48 49 50 51 52 53 54 55
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "campaign":
            orgs = request.user.organizations.all()
            query = {'organization__in': orgs}
            kwargs["queryset"] = Campaign.objects.filter(**query)
        return super(ArgumentaryAdmin, self).formfield_for_foreignkey(db_field,
                                                                      request,
                                                                      **kwargs)
56
admin.site.register(Campaign, CampaignAdmin)
57 58
admin.site.register(CampaignContact, CampaignContactAdmin)
admin.site.register(Argumentary, ArgumentaryAdmin)