admin.py 3.64 KB
Newer Older
1
from django.contrib import admin
okhin's avatar
okhin committed
2
from modeltranslation.admin import TranslationAdmin, TranslationStackedInline
3

4 5
from picampaign.campaign.models import Campaign, CampaignContact, Argumentary
from picampaign.organization.models import Organization
okhin's avatar
okhin committed
6
from picampaign.importer.models import Importer
7
from picampaign.campaign.translation import CampaignTranslationOptions, ArgumentaryTranslationOptions
8

9 10 11 12

class InlineContact(admin.TabularInline):
    model = CampaignContact

okhin's avatar
okhin committed
13 14
class InlineImporter(admin.TabularInline):
    model = Importer
15

okhin's avatar
okhin committed
16
class InlineTranslationArgumentary(TranslationStackedInline):
17 18 19 20
    model = Argumentary

class CampaignAdmin(TranslationAdmin):
    inlines = [InlineTranslationArgumentary, InlineContact, InlineImporter]
okhin's avatar
okhin committed
21 22 23
    list_display = ('title', 'organization', 'start_date', 'end_date', 'phone_filter',)
    list_filter = ('organization', 'start_date', 'end_date',)
    list_select_related = ('organization',)
24
    search_fields = ['title', 'organization__name']
25

26 27 28 29 30 31 32
    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)

33 34
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "organization":
35
            if request.user.is_superuser:
okhin's avatar
okhin committed
36
                orgs = Organization.objects.all()
37
            else:
okhin's avatar
okhin committed
38 39 40 41
                orgs = request.user.organizations.all()
            query = {'id__in': [x.id
                                for x in orgs]}
            kwargs["queryset"] = Organization.objects.filter(**query)
42 43 44
        return super(CampaignAdmin, self).formfield_for_foreignkey(db_field,
                                                                   request,
                                                                   **kwargs)
45
class CampaignContactAdmin(admin.ModelAdmin):
okhin's avatar
okhin committed
46 47 48
    list_display = ('contact', 'campaign', 'weight')
    list_select_related = ('contact', 'campaign',)
    list_filter = ('campaign',)
49
    search_fields = ['campaign__title', 'contact__first_name', 'contact__last_name']
okhin's avatar
okhin committed
50

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


59
class ArgumentaryAdmin(TranslationAdmin):
okhin's avatar
okhin committed
60 61 62 63
    list_display = ('title', 'campaign',)
    list_select_related = ('campaign',)
    list_filter = ('campaign',)

64 65 66 67 68 69 70
    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)

71 72
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "campaign":
okhin's avatar
okhin committed
73 74 75 76
            if request.user.is_superuser:
                orgs = Organization.objects.all()
            else:
                orgs = request.user.organizations.all()
77 78 79 80 81
            query = {'organization__in': orgs}
            kwargs["queryset"] = Campaign.objects.filter(**query)
        return super(ArgumentaryAdmin, self).formfield_for_foreignkey(db_field,
                                                                      request,
                                                                      **kwargs)
82
admin.site.register(Campaign, CampaignAdmin)
83 84
admin.site.register(CampaignContact, CampaignContactAdmin)
admin.site.register(Argumentary, ArgumentaryAdmin)