admin.py 3.28 KB
Newer Older
Aymeric Barantal's avatar
Aymeric Barantal committed
1
from django.contrib import admin
2
from picampaign.organization.models import (Organization, Group, GroupType,
3
                                            FeedbackCategory)
Aymeric Barantal's avatar
Aymeric Barantal committed
4

5 6 7 8 9 10 11 12 13 14

class OrganizationAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(OrganizationAdmin, 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(id__in=user_orgs)


15 16 17 18 19 20 21 22 23
class GroupTypeAdmin(admin.ModelAdmin):

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

24 25 26 27 28 29 30 31 32
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "organization":
            orgs = request.user.organizations.all()
            query = {'id__in': orgs}
            kwargs["queryset"] = Organization.objects.filter(**query)
        return super(GroupTypeAdmin, self).formfield_for_foreignkey(db_field,
                                                                    request,
                                                                    **kwargs)

33

34 35 36 37 38 39 40 41
class GroupAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(GroupAdmin, 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)

42 43 44 45 46 47 48 49 50 51 52 53 54
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "organization":
            orgs = request.user.organizations.all()
            query = {'id__in': orgs}
            kwargs["queryset"] = Organization.objects.filter(**query)
        elif db_field.name == "type":
            orgs = request.user.organizations.all()
            query = {'organization__in': orgs}
            kwargs["queryset"] = GroupType.objects.filter(**query)
        return super(GroupAdmin, self).formfield_for_foreignkey(db_field,
                                                                request,
                                                                **kwargs)

55 56 57 58 59 60 61 62 63

class CategoryAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(CategoryAdmin, 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)

64 65 66 67 68 69 70 71 72
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if db_field.name == "organization":
            orgs = request.user.organizations.all()
            query = {'id__in': orgs}
            kwargs["queryset"] = Organization.objects.filter(**query)
        return super(CategoryAdmin, self).formfield_for_foreignkey(db_field,
                                                                   request,
                                                                   **kwargs)

73 74

admin.site.register(Organization, OrganizationAdmin)
75
admin.site.register(GroupType, GroupTypeAdmin)
76 77
admin.site.register(Group, GroupAdmin)
admin.site.register(FeedbackCategory, CategoryAdmin)