phone: handle a new max call duration settings (#15979)

This commit is contained in:
Thomas NOËL 2017-05-31 23:45:52 +02:00
parent c1e0c32a29
commit 8bbb458eae
3 changed files with 56 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import pytest
from django.core.urlresolvers import reverse
from django.test import override_settings
from django.utils.timezone import now, timedelta
from welco.sources.phone import models
@ -221,3 +222,44 @@ def test_take_release_line(user, client):
assert models.PhoneLine.objects.count() == 1
assert models.PhoneLine.objects.filter(
users=user, callee='102').count() == 0
def test_call_expiration(user, client):
assert models.PhoneCall.objects.count() == 0
# create a call
payload = {'event': 'start', 'caller': '003369999999', '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(stop__isnull=True).count() == 1
# get list of calls for line 102
models.PhoneLine.take(callee='102', user=user)
client.login(username='toto', password='toto')
response = client.get(reverse('phone-current-calls'))
assert response.status_code == 200
payload = json.loads(response.content)
assert payload['err'] == 0
assert len(payload['data']['calls']) == 1
# start call 10 minutes ago
models.PhoneCall.objects.filter(stop__isnull=True).update(
start=now()-timedelta(minutes=10))
# get list of calls without expiration
response = client.get(reverse('phone-current-calls'))
assert response.status_code == 200
payload = json.loads(response.content)
assert payload['err'] == 0
assert len(payload['data']['calls']) == 1 # still here
# get list of calls with an expiration of 2 minutes (< 10 minutes)
with override_settings(PHONE_MAX_CALL_DURATION=2):
response = client.get(reverse('phone-current-calls'))
assert response.status_code == 200
payload = json.loads(response.content)
assert payload['err'] == 0
assert len(payload['data']['calls']) == 0 # call is expired
assert models.PhoneCall.objects.filter(stop__isnull=True).count() == 0 # active calls
assert models.PhoneCall.objects.filter(stop__isnull=False).count() == 1 # stopped calls

View File

@ -200,6 +200,7 @@ COUNTER_LINKS = [
# phone system
PHONE_ONE_CALL_PER_CALLEE = True
PHONE_MAX_CALL_DURATION = 0 # in minutes, 0 stands for infinity
# enable/disable specific features
# ex: FLAVOURS = ['alfortville']

View File

@ -14,10 +14,14 @@
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import logging
from django.conf import settings
from django.contrib.contenttypes.fields import GenericRelation
from django.core.urlresolvers import reverse
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.timezone import now, timedelta
from welco.qualif.models import Association
@ -54,6 +58,15 @@ class PhoneCall(models.Model):
@classmethod
def get_current_calls(cls, user):
if settings.PHONE_MAX_CALL_DURATION:
logger = logging.getLogger(__name__)
start_after = now() - timedelta(minutes=settings.PHONE_MAX_CALL_DURATION)
for call in cls.objects.filter(callee__in=PhoneLine.get_callees(user),
stop__isnull=True,
start__lt=start_after):
logger.info('stop expired call from %s to %s', call.caller, call.callee)
call.stop = now()
call.save()
return cls.objects.filter(callee__in=PhoneLine.get_callees(user),
stop__isnull=True).order_by('start')