Commit 997eb80b authored by okhin's avatar okhin

Fix #6: Only one phone number returned, ever

parent b04deb2b
Pipeline #109 passed with stage
in 49 seconds
# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-08-09 15:36
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('campaign', '0003_auto_20160807_1603'),
]
operations = [
migrations.AddField(
model_name='campaign',
name='phone_filter',
field=models.CharField(blank=True, max_length=20, null=True),
),
]
......@@ -8,7 +8,6 @@ from picampaign.contact.models import Contact
LANGUAGES = map(lambda k: (k[0], _(k[1])), settings.LANGUAGES)
class Campaign(models.Model):
"""Campaign model, describe what have to be achieved"""
id = models.AutoField(primary_key=True)
......@@ -20,6 +19,7 @@ class Campaign(models.Model):
default_lang = models.CharField(max_length=5,
choices=LANGUAGES,
verbose_name=_('language'))
phone_filter = models.CharField(max_length=20, null=True, blank=True)
def __str__(self):
return self.title
......@@ -37,7 +37,6 @@ class Argumentary(models.Model):
args = {'lang': self.lang, 'title': self.campaign.title}
return _('Argumentary in %(lang)s for %(title)s') % args
class CampaignContact(models.Model):
"""List contact related to a campaign with a given weight"""
id = models.AutoField(primary_key=True)
......
......@@ -11,6 +11,11 @@ class ArgumentarySerializer(serializers.ModelSerializer):
model = Argumentary
fields = ('lang', 'text')
class PhoneSerializer(serializers.ModelSerializer):
class Meta:
model = Phone
fields = ('phone',)
class CampaignSerializer(serializers.HyperlinkedModelSerializer):
......@@ -18,18 +23,12 @@ class CampaignSerializer(serializers.HyperlinkedModelSerializer):
model = Campaign
fields = ('id', 'title', 'description', 'start_date', 'end_date')
class PhoneSerializer(serializers.ModelSerializer):
class Meta:
model = Phone
fields = ('id', 'phone', 'valid')
class CampaignContactSerializer(serializers.HyperlinkedModelSerializer):
full_name = serializers.ReadOnlyField(source='contact.full_name')
first_name = serializers.ReadOnlyField(source='contact.first_name')
last_name = serializers.ReadOnlyField(source='contact.last_name')
phones = PhoneSerializer(many=True, source='contact.phones')
phone = PhoneSerializer(many=True, source='contact.phones')
groups = GroupSerializer(many=True, source='contact.groups')
contact_id = serializers.ReadOnlyField(source='contact.id')
photo = serializers.ReadOnlyField(source='contact.get_photo_url')
......@@ -37,4 +36,25 @@ class CampaignContactSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = CampaignContact
fields = ('id', 'weight', 'contact_id',
'full_name', 'first_name', 'last_name', 'phones', 'groups', 'photo')
'full_name', 'first_name', 'last_name',
'phone', 'groups', 'photo')
def to_representation(self, instance):
"""
We want to validate the phone number.
"""
# First, let's get the campaign
data = super(CampaignContactSerializer, self).to_representation(instance)
campaign = instance.campaign
# We should have a filter parameter for the campaign
filter = campaign.phone_filter
if filter == '' or filter is None:
# No filter, we just return the first phone number
data['phone'] = data['phone'][0]['phone']
return data
else:
for phone in data['phone']:
if phone['phone'].startswith(filter):
data['phone'] = phone['phone']
return data
return data
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