diff --git a/tests/test_source_phone.py b/tests/test_source_phone.py index 0bbc124..b855407 100644 --- a/tests/test_source_phone.py +++ b/tests/test_source_phone.py @@ -19,6 +19,7 @@ import json import pytest from django.core.urlresolvers import reverse +from django.test import override_settings from welco.sources.phone import models @@ -95,6 +96,43 @@ def test_call_start_stop(client): data=json.dumps(payload['data']), stop__isnull=False).count() == 2 +def test_one_call_per_callee(user, client): + assert models.PhoneCall.objects.count() == 0 + payload = {'event': 'start', 'caller': '0033699999999', 'callee': '102'} + response = client.post(reverse('phone-call-event'), json.dumps(payload), + content_type='application/json') + assert response.status_code == 200 + assert models.PhoneCall.objects.filter(callee='102', stop__isnull=True).count() == 1 # active + assert models.PhoneCall.objects.filter(callee='102', stop__isnull=False).count() == 0 # inactive + + # new caller, same callee: stops the last call, start a new one + payload['caller'] = '00337123456789' + response = client.post(reverse('phone-call-event'), json.dumps(payload), + content_type='application/json') + assert response.status_code == 200 + assert models.PhoneCall.objects.count() == 2 + assert models.PhoneCall.objects.filter( + caller='00337123456789', callee='102', stop__isnull=True).count() == 1 + assert models.PhoneCall.objects.filter( + caller='0033699999999', callee='102', stop__isnull=False).count() == 1 + + with override_settings(PHONE_ONE_CALL_PER_CALLEE=False): + # accept multiple call: start a new one, don't stop anything + payload['caller'] = '00221774261500' + response = client.post(reverse('phone-call-event'), json.dumps(payload), + content_type='application/json') + assert response.status_code == 200 + assert models.PhoneCall.objects.count() == 3 + assert models.PhoneCall.objects.filter(callee='102', stop__isnull=True).count() == 2 + assert models.PhoneCall.objects.filter(callee='102', stop__isnull=False).count() == 1 + # same caller: stop his last call, add a new one + response = client.post(reverse('phone-call-event'), json.dumps(payload), + content_type='application/json') + assert response.status_code == 200 + assert models.PhoneCall.objects.count() == 4 + assert models.PhoneCall.objects.filter(callee='102', stop__isnull=True).count() == 2 + assert models.PhoneCall.objects.filter(callee='102', stop__isnull=False).count() == 2 + def test_current_calls(user, client): # create some calls for number in range(0, 10): diff --git a/welco/settings.py b/welco/settings.py index 033f32a..e0438cd 100644 --- a/welco/settings.py +++ b/welco/settings.py @@ -198,6 +198,9 @@ COUNTER_LINKS = [ {'label': 'Wikipedia', 'url': 'https://fr.wikipedia.org'} ] +# phone system +PHONE_ONE_CALL_PER_CALLEE = True + # enable/disable specific features # ex: FLAVOURS = ['alfortville'] FLAVOURS = [] diff --git a/welco/sources/phone/views.py b/welco/sources/phone/views.py index 955f8ef..0211bc4 100644 --- a/welco/sources/phone/views.py +++ b/welco/sources/phone/views.py @@ -18,6 +18,7 @@ import json import logging from django import template +from django.conf import settings from django.contrib.contenttypes.models import ContentType from django.template import RequestContext from django.views.decorators.csrf import csrf_exempt @@ -97,10 +98,16 @@ def call_event(request): return HttpResponseBadRequest(json.dumps({'err': 1, 'msg': unicode(e)}), content_type='application/json') - # terminate all existing calls between these two endpoints - PhoneCall.objects.filter(caller=payload['caller'], - callee=payload['callee'], stop__isnull=True) \ - .update(stop=now()) + # janitoring: stop active calls to the callee + if settings.PHONE_ONE_CALL_PER_CALLEE: + logger.info('stop all calls to %s', payload['callee']) + PhoneCall.objects.filter(callee=payload['callee'], + stop__isnull=True).update(stop=now()) + else: + logger.info('stop call from %s to %s', payload['caller'], payload['callee']) + PhoneCall.objects.filter(caller=payload['caller'], + callee=payload['callee'], + stop__isnull=True).update(stop=now()) if payload['event'] == 'start': # start a new call kwargs = { @@ -111,8 +118,6 @@ def call_event(request): kwargs['data'] = json.dumps(payload['data']) PhoneCall.objects.create(**kwargs) logger.info('start call from %s to %s', payload['caller'], payload['callee']) - else: - logger.info('stop call from %s to %s', payload['caller'], payload['callee']) return HttpResponse(json.dumps({'err': 0}), content_type='application/json')