Commit f754014f authored by okhin's avatar okhin

Merge branch 'org_admin' into 'master'

Org admin

When a user connect on admin site he will see only objects related to organizations
he belong to.

See merge request !4
parents 8d155b55 2bf9817b
...@@ -10,7 +10,34 @@ class InlineContact(admin.TabularInline): ...@@ -10,7 +10,34 @@ class InlineContact(admin.TabularInline):
class CampaignAdmin(admin.ModelAdmin): class CampaignAdmin(admin.ModelAdmin):
inlines = [InlineContact] inlines = [InlineContact]
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)
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)
admin.site.register(Campaign, CampaignAdmin) admin.site.register(Campaign, CampaignAdmin)
admin.site.register(CampaignContact) admin.site.register(CampaignContact, CampaignContactAdmin)
admin.site.register(Argumentary) admin.site.register(Argumentary, ArgumentaryAdmin)
...@@ -14,7 +14,7 @@ class Campaign(models.Model): ...@@ -14,7 +14,7 @@ class Campaign(models.Model):
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
title = models.CharField(max_length=255) title = models.CharField(max_length=255)
description = models.CharField(max_length=512, blank=True) description = models.CharField(max_length=512, blank=True)
organization = models.ForeignKey(Organization) organization = models.ForeignKey(Organization, related_name='campaigns')
start_date = models.DateField() start_date = models.DateField()
end_date = models.DateField() end_date = models.DateField()
default_lang = models.CharField(max_length=5, default_lang = models.CharField(max_length=5,
......
from picampaign.campaign.models import (Campaign, CampaignContact, from picampaign.campaign.models import (Campaign, CampaignContact,
Argumentary) Argumentary)
from picampaign.organization.serializers import GroupSerializer
from rest_framework import serializers from rest_framework import serializers
...@@ -22,7 +23,7 @@ class CampaignContactSerializer(serializers.HyperlinkedModelSerializer): ...@@ -22,7 +23,7 @@ class CampaignContactSerializer(serializers.HyperlinkedModelSerializer):
first_name = serializers.Field(source='contact.first_name') first_name = serializers.Field(source='contact.first_name')
last_name = serializers.Field(source='contact.last_name') last_name = serializers.Field(source='contact.last_name')
phone = serializers.Field(source='contact.phone') phone = serializers.Field(source='contact.phone')
groups = serializers.Field(source='all_groups') groups = GroupSerializer(many=True, source='contact.groups')
contact_id = serializers.Field(source='contact.id') contact_id = serializers.Field(source='contact.id')
class Meta: class Meta:
......
...@@ -9,6 +9,7 @@ class Contact(models.Model): ...@@ -9,6 +9,7 @@ class Contact(models.Model):
phone = models.CharField(max_length=32) phone = models.CharField(max_length=32)
twitter = models.CharField(max_length=64, blank=True) twitter = models.CharField(max_length=64, blank=True)
mail = models.CharField(max_length=255, blank=True) mail = models.CharField(max_length=255, blank=True)
photo = models.ImageField(upload_to='contacts/photos', blank=True)
def __unicode__(self): def __unicode__(self):
return '%s %s' % (self.first_name, self.last_name) return '%s %s' % (self.first_name, self.last_name)
from django.contrib import admin from django.contrib import admin
from picampaign.organization.models import (Organization, Group, from picampaign.organization.models import (Organization, Group, GroupType,
FeedbackCategory) FeedbackCategory)
admin.site.register(Organization)
admin.site.register(Group) class OrganizationAdmin(admin.ModelAdmin):
admin.site.register(FeedbackCategory) 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 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)
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(GroupType, GroupTypeAdmin)
admin.site.register(Group, GroupAdmin)
admin.site.register(FeedbackCategory, CategoryAdmin)
...@@ -7,7 +7,17 @@ class Organization(models.Model): ...@@ -7,7 +7,17 @@ class Organization(models.Model):
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64) name = models.CharField(max_length=64)
sip_key = models.CharField(max_length=255) 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
class GroupType(models.Model):
id = models.AutoField(primary_key=True)
organization = models.ForeignKey(Organization)
name = models.CharField(max_length=64)
def __unicode__(self): def __unicode__(self):
return self.name return self.name
...@@ -18,7 +28,7 @@ class Group(models.Model): ...@@ -18,7 +28,7 @@ class Group(models.Model):
id = models.AutoField(primary_key=True) id = models.AutoField(primary_key=True)
organization = models.ForeignKey(Organization) organization = models.ForeignKey(Organization)
name = models.CharField(max_length=64) name = models.CharField(max_length=64)
type = models.CharField(max_length=255, blank=True) type = models.ForeignKey(GroupType, blank=True, null=True)
media = models.CharField(max_length=255, blank=True) media = models.CharField(max_length=255, blank=True)
contacts = models.ManyToManyField(Contact, null=True, blank=True, contacts = models.ManyToManyField(Contact, null=True, blank=True,
related_name='groups') related_name='groups')
......
from picampaign.organization.models import FeedbackCategory from picampaign.organization.models import FeedbackCategory, Group
from rest_framework import serializers from rest_framework import serializers
...@@ -7,3 +7,11 @@ class CategorySerializer(serializers.ModelSerializer): ...@@ -7,3 +7,11 @@ class CategorySerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = FeedbackCategory model = FeedbackCategory
fields = ('id', 'name') fields = ('id', 'name')
class GroupSerializer(serializers.ModelSerializer):
type = serializers.Field(source='type.name')
class Meta:
model = Group
fields = ('type', 'name', 'media')
from rest_framework import viewsets from rest_framework import viewsets
from rest_framework.response import Response from rest_framework.response import Response
from picampaign.organization.models import FeedbackCategory from picampaign.organization.models import FeedbackCategory, Organization
from picampaign.organization.serializers import CategorySerializer from picampaign.organization.serializers import CategorySerializer
...@@ -10,8 +10,8 @@ class CategoryViewSet(viewsets.ViewSet): ...@@ -10,8 +10,8 @@ class CategoryViewSet(viewsets.ViewSet):
queryset = FeedbackCategory.objects.all() queryset = FeedbackCategory.objects.all()
serializer_class = CategorySerializer serializer_class = CategorySerializer
def list(self, request, campaign_pk=None): def list(self, request, campaign_pk):
# XXX need an organization in request to filter upon orga = Organization.objects.filter(campaigns__id=campaign_pk)[0]
categories = self.queryset.all() categories = self.queryset.filter(organization_id=orga.id)
serializer = self.serializer_class(categories, many=True) serializer = self.serializer_class(categories, many=True)
return Response(serializer.data) return Response(serializer.data)
...@@ -9,6 +9,7 @@ requires = [ ...@@ -9,6 +9,7 @@ requires = [
'django', 'django',
'djangorestframework', 'djangorestframework',
'drf-nested-routers', 'drf-nested-routers',
'pillow',
] ]
setup(name='picampaign', setup(name='picampaign',
......
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