admin.py 4.02 KB
Newer Older
Aymeric Barantal's avatar
Aymeric Barantal committed
1
from django.contrib import admin
Okhin's avatar
Okhin committed
2 3
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponseRedirect
Aymeric Barantal's avatar
Aymeric Barantal committed
4

okhin's avatar
okhin committed
5
from picampaign.organization.models import FeedbackCategory
6
from picampaign.feedback.models import Feedback
Aymeric Barantal's avatar
Aymeric Barantal committed
7

okhin's avatar
okhin committed
8 9 10 11 12 13
class FeedbackFilterByCategory(admin.SimpleListFilter):
    title = 'category'
    parameter_name = 'category'

    def lookups(self, request, model_admin):
        if request.user.is_superuser:
Okhin's avatar
Okhin committed
14 15 16
            feedbacks = Feedback.objects.all().values_list(
                    'category__id',
                    'category__name')
okhin's avatar
okhin committed
17 18
        else:
            user_orgs = [x.id for x in request.user.organizations.all()]
Okhin's avatar
Okhin committed
19
            feedbacks = Feedback.objects.filter(
20
                    category__organization__in=user_orgs).values_list(
Okhin's avatar
Okhin committed
21 22
                            'category_id',
                            'category_name')
okhin's avatar
okhin committed
23 24 25 26 27 28 29 30

        return ((f[0], f[1],) for f in sorted(set(feedbacks), key=lambda f: f[1]))

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(category__id=self.value())
        return queryset

Okhin's avatar
Okhin committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

class FeedbackFilterByOrganization(admin.SimpleListFilter):
    title = 'organization'
    parameter_name = 'organization'

    def lookups(self, request, model_admin):
        if request.user.is_superuser:
            feedbacks = Feedback.objects.all().values_list(
                    'category__organization__id',
                    'category__organization__name')
        else:
            user_orgs = [x.id for x in request.user.organizations.all()]
            feedbacks = Feedback.objects.filter(
                    category__organization__in=user_orgs).values_list(
                        'category__organization__id',
                        'category__organization__name')
        return ((f[0], f[1],) for f in sorted(set(feedbacks), key=lambda f: f[1]))

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(category__organization__id=self.value())
        return queryset


class FeedbackFilterByCampaign(admin.SimpleListFilter):
    title = 'campaign'
    parameter_name = 'campaign'

    def lookups(self, request, model_admin):
        if request.user.is_superuser:
            feedbacks = Feedback.objects.all().values_list(
                    'callee__campaign__id',
                    'callee__campaign__title')
        else:
            user_orgs = [x.id for x in request.user.organizations.all()]
            feedbacks = Feedback.objects.filter(
                    category__organization__in=user_orgs).values_list(
                        'callee__campaign__id',
                        'callee__campaign__title')
        return ((f[0], f[1],) for f in sorted(set(feedbacks), key=lambda f: f[1]))

    def queryset(self, request, queryset):
        if self.value():
            return queryset.filter(callee__campaign__id=self.value())
        return queryset


okhin's avatar
okhin committed
78
class FeedbackAdmin(admin.ModelAdmin):
Okhin's avatar
Okhin committed
79 80 81 82 83 84 85 86 87 88 89
    date_hierarchy = 'date'
    list_display = ('callee', 'category',
            'date', 'organization',)
    list_filter = (FeedbackFilterByCategory,
            FeedbackFilterByOrganization,
            FeedbackFilterByCampaign)
    search_fields = ['category__organization__name', 'category__name',
            'callee__contact__first_name',
            'callee__contact__last_name',
            'callee__campaign__title']
    actions = ['export_csv']
okhin's avatar
okhin committed
90 91 92 93 94 95 96 97

    def get_queryset(self, request):
        qs = super(FeedbackAdmin, 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(category__organization__in=user_orgs)

Okhin's avatar
Okhin committed
98 99 100 101 102 103 104
    def export_csv(self, request, queryset):
        selected = request.POST.getlist(admin.ACTION_CHECKBOX_NAME)
        ct = ContentType.objects.get_for_model(queryset.model)
        return HttpResponseRedirect("/export/?ct={}&ids={}".format(ct.pk, ",".join(selected)))

    export_csv.short_description = "Export feedbacks as CSV"

okhin's avatar
okhin committed
105
admin.site.register(Feedback, FeedbackAdmin)