admin.py 2.42 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
Aymeric Barantal's avatar
Aymeric Barantal committed
4

5 6 7 8 9 10 11 12

class InlineContact(admin.TabularInline):
    model = CampaignContact


class CampaignAdmin(admin.ModelAdmin):
    inlines = [InlineContact]

13 14 15 16 17 18 19
    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)

20 21 22 23 24 25 26 27 28
    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)

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

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)

49 50 51 52 53 54 55 56 57
    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)

58 59

admin.site.register(Campaign, CampaignAdmin)
60 61
admin.site.register(CampaignContact, CampaignContactAdmin)
admin.site.register(Argumentary, ArgumentaryAdmin)