Commit 2444d374 authored by Aymeric Barantal's avatar Aymeric Barantal

flatten output for campaigns/<id>/contacts

parent 27371eea
...@@ -56,3 +56,6 @@ class CampaignContact(models.Model): ...@@ -56,3 +56,6 @@ class CampaignContact(models.Model):
def __unicode__(self): def __unicode__(self):
return 'Contact %s on %s' % (self.contact, self.campaign.title) return 'Contact %s on %s' % (self.contact, self.campaign.title)
def all_groups(self):
return [x.name for x in self.contact.groups.all()]
from picampaign.campaign.models import (Campaign, CampaignContact, Contact, from picampaign.campaign.models import (Campaign, CampaignContact,
Argumentary) Argumentary)
from rest_framework import serializers from rest_framework import serializers
class ContactSerializer(serializers.ModelSerializer):
class Meta:
model = Contact
fields = ('id', 'first_name', 'last_name', 'phone')
class ArgumentarySerializer(serializers.ModelSerializer): class ArgumentarySerializer(serializers.ModelSerializer):
class Meta: class Meta:
...@@ -26,8 +19,11 @@ class CampaignSerializer(serializers.HyperlinkedModelSerializer): ...@@ -26,8 +19,11 @@ class CampaignSerializer(serializers.HyperlinkedModelSerializer):
class CampaignContactSerializer(serializers.HyperlinkedModelSerializer): class CampaignContactSerializer(serializers.HyperlinkedModelSerializer):
contact = ContactSerializer(many=False) first_name = serializers.Field(source='contact.first_name')
last_name = serializers.Field(source='contact.last_name')
phone = serializers.Field(source='contact.phone')
groups = serializers.Field(source='all_groups')
class Meta: class Meta:
model = CampaignContact model = CampaignContact
fields = ('id', 'weight', 'contact') fields = ('id', 'weight', 'first_name', 'last_name', 'phone', 'groups')
...@@ -20,7 +20,8 @@ class Group(models.Model): ...@@ -20,7 +20,8 @@ class Group(models.Model):
name = models.CharField(max_length=64) name = models.CharField(max_length=64)
type = models.CharField(max_length=255, blank=True) type = models.CharField(max_length=255, blank=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')
def __unicode__(self): def __unicode__(self):
return self.name 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