Commit ff1fb676 authored by Aymeric Barantal's avatar Aymeric Barantal

filter organizations admin models for related user organizations if not super_user

parent c959aff1
......@@ -2,6 +2,34 @@ from django.contrib import admin
from picampaign.organization.models import (Organization, Group,
FeedbackCategory)
admin.site.register(Organization)
admin.site.register(Group)
admin.site.register(FeedbackCategory)
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)
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)
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)
admin.site.register(Organization, OrganizationAdmin)
admin.site.register(Group, GroupAdmin)
admin.site.register(FeedbackCategory, CategoryAdmin)
......@@ -7,7 +7,8 @@ class Organization(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64)
sip_key = models.CharField(max_length=255)
users = models.ManyToManyField(User, null=True, blank=True)
users = models.ManyToManyField(User, null=True, blank=True,
related_name='organizations')
def __unicode__(self):
return self.name
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment