Projet

Général

Profil

0001-add-phone-calls-connector-29829.patch

Thomas Noël, 21 janvier 2019 15:22

Télécharger (20,4 ko)

Voir les différences:

Subject: [PATCH] add phone calls connector (#29829)

 passerelle/apps/phonecalls/__init__.py        |   0
 .../phonecalls/migrations/0001_initial.py     |  54 +++++
 .../apps/phonecalls/migrations/__init__.py    |   0
 passerelle/apps/phonecalls/models.py          | 146 ++++++++++++
 passerelle/settings.py                        |   1 +
 tests/test_phonecalls.py                      | 211 ++++++++++++++++++
 6 files changed, 412 insertions(+)
 create mode 100644 passerelle/apps/phonecalls/__init__.py
 create mode 100644 passerelle/apps/phonecalls/migrations/0001_initial.py
 create mode 100644 passerelle/apps/phonecalls/migrations/__init__.py
 create mode 100644 passerelle/apps/phonecalls/models.py
 create mode 100644 tests/test_phonecalls.py
passerelle/apps/phonecalls/migrations/0001_initial.py
1
# -*- coding: utf-8 -*-
2
# Generated by Django 1.11.17 on 2019-01-21 14:14
3
from __future__ import unicode_literals
4

  
5
from django.db import migrations, models
6
import django.db.models.deletion
7
import jsonfield.fields
8

  
9

  
10
class Migration(migrations.Migration):
11

  
12
    initial = True
13

  
14
    dependencies = [
15
        ('base', '0010_loggingparameters_trace_emails'),
16
    ]
17

  
18
    operations = [
19
        migrations.CreateModel(
20
            name='Call',
21
            fields=[
22
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
23
                ('callee', models.CharField(max_length=64)),
24
                ('caller', models.CharField(max_length=64)),
25
                ('start_timestamp', models.DateTimeField(auto_now_add=True)),
26
                ('end_timestamp', models.DateTimeField(default=None, null=True)),
27
                ('details', jsonfield.fields.JSONField(default={})),
28
            ],
29
            options={
30
                'ordering': ['-start_timestamp'],
31
                'verbose_name': 'Phone Call',
32
            },
33
        ),
34
        migrations.CreateModel(
35
            name='PhoneCalls',
36
            fields=[
37
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
38
                ('title', models.CharField(max_length=50, verbose_name='Title')),
39
                ('description', models.TextField(verbose_name='Description')),
40
                ('slug', models.SlugField(unique=True, verbose_name='Identifier')),
41
                ('max_call_duration', models.PositiveIntegerField(default=120, help_text='Each hour, too long calls are closed.', verbose_name='Maximum duration of a call, in minutes.')),
42
                ('data_retention_period', models.PositiveIntegerField(default=60, help_text='Each day, old calls are removed.', verbose_name='Data retention period, in days.')),
43
                ('users', models.ManyToManyField(blank=True, to='base.ApiUser')),
44
            ],
45
            options={
46
                'verbose_name': 'Phone Calls',
47
            },
48
        ),
49
        migrations.AddField(
50
            model_name='call',
51
            name='resource',
52
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='phonecalls.PhoneCalls'),
53
        ),
54
    ]
passerelle/apps/phonecalls/models.py
1
# passerelle - uniform access to multiple data sources and services
2
# Copyright (C) 2019  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16

  
17
from django.db import models
18
from django.utils.timezone import now, timedelta, make_naive
19
from django.utils.translation import ugettext_lazy as _
20
from jsonfield import JSONField
21

  
22
from passerelle.base.models import BaseResource
23
from passerelle.utils.api import endpoint
24

  
25

  
26
class PhoneCalls(BaseResource):
27
    category = _('Telephony')
28

  
29
    max_call_duration = models.PositiveIntegerField(
30
            _('Maximum duration of a call, in minutes.'),
31
            help_text=_('Each hour, too long calls are closed.'),
32
            default=120)
33
    data_retention_period = models.PositiveIntegerField(
34
            _('Data retention period, in days.'),
35
            help_text=_('Each day, old calls are removed.'),
36
            default=60)
37

  
38
    class Meta:
39
        verbose_name = _('Phone Calls')
40

  
41
    @endpoint(name='call-start',
42
              perm='can_access',
43
              parameters={
44
                  'callee': {'description': _('Callee number'),
45
                             'example_value': '142'},
46
                  'caller': {'description': _('Caller number'),
47
                             'example_value': '0143350135'},
48
              })
49
    def call_start(self, request, callee, caller, **kwargs):
50
        existing_call = Call.objects.filter(resource=self,
51
                                            callee=callee, caller=caller,
52
                                            end_timestamp=None).last()
53
        if existing_call:
54
            existing_call.details = kwargs
55
            existing_call.save()
56
            return {'data': existing_call.json()}
57
        new_call = Call(resource=self, callee=callee, caller=caller, details=kwargs)
58
        new_call.save()
59
        return {'data': new_call.json()}
60

  
61
    @endpoint(name='call-stop',
62
              perm='can_access',
63
              parameters={
64
                  'callee': {'description': _('Callee number'),
65
                             'example_value': '142'},
66
                  'caller': {'description': _('Caller number'),
67
                             'example_value': '0143350135'},
68
              })
69
    def call_stop(self, request, callee, caller, **kwargs):
70
        # close all current callee/caller calls
71
        data = []
72
        for current_call in Call.objects.filter(resource=self,
73
                                                callee=callee, caller=caller,
74
                                                end_timestamp=None):
75
            current_call.end_timestamp = now()
76
            current_call.save()
77
            data.append(current_call.json())
78
        return {'data': data}
79

  
80
    @endpoint(name='calls',
81
              perm='can_access',
82
              parameters={
83
                  'callee': {'description': _('Callee number'),
84
                             'example_value': '142'},
85
                  'limit': {'description': _('Maximal number of results')},
86
              })
87
    def calls(self, request, callee=None, caller=None, limit=30):
88
        calls = Call.objects.filter(resource=self)
89
        if callee:
90
            calls = calls.filter(callee=callee)
91
        if caller:
92
            calls = calls.filter(caller=caller)
93

  
94
        def json_list(calls):
95
            return [call.json() for call in calls[:limit]]
96
        return {
97
            'data': {
98
                'current': json_list(calls.filter(end_timestamp__isnull=True)),
99
                'past': json_list(calls.filter(end_timestamp__isnull=False)),
100
            }
101
        }
102

  
103
    def hourly(self):
104
        super(PhoneCalls, self).hourly()
105
        # close unfinished long calls
106
        maximal_time = now() - timedelta(minutes=self.max_call_duration)
107
        Call.objects.filter(resource=self, end_timestamp=None,
108
                            start_timestamp__lt=maximal_time).update(end_timestamp=now())
109

  
110
    def daily(self):
111
        super(PhoneCalls, self).daily()
112
        # remove finished old calls
113
        maximal_time = now() - timedelta(days=self.data_retention_period)
114
        Call.objects.filter(resource=self, end_timestamp__isnull=False,
115
                            end_timestamp__lt=maximal_time).delete()
116

  
117

  
118
class Call(models.Model):
119
    resource = models.ForeignKey(PhoneCalls)
120
    callee = models.CharField(blank=False, max_length=64)
121
    caller = models.CharField(blank=False, max_length=64)
122
    start_timestamp = models.DateTimeField(auto_now_add=True)
123
    end_timestamp = models.DateTimeField(null=True, default=None)
124
    details = JSONField(default={})
125

  
126
    class Meta:
127
        verbose_name = _('Phone Call')
128
        ordering = ['-start_timestamp']
129

  
130
    def json(self):
131
        # We use make_naive to send localtime, because this API will be used
132
        # by javascript, which will not be comfortable with UTC datetimes
133
        if self.end_timestamp:
134
            is_current = False
135
            end_timestamp = make_naive(self.end_timestamp)
136
        else:
137
            is_current = True
138
            end_timestamp = None
139
        return {
140
            'caller': self.caller,
141
            'callee': self.callee,
142
            'start': make_naive(self.start_timestamp),
143
            'end': end_timestamp,
144
            'is_current': is_current,
145
            'details': self.details,
146
        }
passerelle/settings.py
138 138
    'passerelle.apps.ovh',
139 139
    'passerelle.apps.oxyd',
140 140
    'passerelle.apps.pastell',
141
    'passerelle.apps.phonecalls',
141 142
    'passerelle.apps.solis',
142 143
    'passerelle.apps.arpege_ecp',
143 144
    'passerelle.apps.vivaticket',
tests/test_phonecalls.py
1
import pytest
2
import utils
3

  
4
from django.contrib.contenttypes.models import ContentType
5
from django.utils.timezone import now, timedelta
6

  
7
from passerelle.apps.phonecalls.models import PhoneCalls, Call
8
from passerelle.base.models import ApiUser, AccessRight
9

  
10

  
11
@pytest.fixture
12
def phonecalls(db):
13
    phonecalls = PhoneCalls.objects.create(slug='test')
14
    apikey = ApiUser.objects.create(username='all', keytype='API', key='123')
15
    obj_type = ContentType.objects.get_for_model(phonecalls)
16
    AccessRight.objects.create(codename='can_access', apiuser=apikey,
17
                               resource_type=obj_type,
18
                               resource_pk=phonecalls.pk)
19
    return phonecalls
20

  
21

  
22
def test_phonecalls_start_stop(app, phonecalls):
23
    start_endpoint = utils.generic_endpoint_url('phonecalls', 'call-start',
24
                                                slug=phonecalls.slug)
25
    assert start_endpoint == '/phonecalls/test/call-start'
26
    stop_endpoint = utils.generic_endpoint_url('phonecalls', 'call-stop',
27
                                               slug=phonecalls.slug)
28
    assert stop_endpoint == '/phonecalls/test/call-stop'
29
    calls_endpoint = utils.generic_endpoint_url('phonecalls', 'calls',
30
                                                slug=phonecalls.slug)
31
    assert calls_endpoint == '/phonecalls/test/calls'
32

  
33
    resp = app.get(start_endpoint, status=403)
34
    assert resp.json['err'] == 1
35
    assert resp.json['err_class'] == 'django.core.exceptions.PermissionDenied'
36
    resp = app.get(stop_endpoint, status=403)
37
    assert resp.json['err'] == 1
38
    assert resp.json['err_class'] == 'django.core.exceptions.PermissionDenied'
39
    resp = app.get(calls_endpoint, status=403)
40
    assert resp.json['err'] == 1
41
    assert resp.json['err_class'] == 'django.core.exceptions.PermissionDenied'
42

  
43
    resp = app.get(start_endpoint, params={'apikey': '123'}, status=400)
44
    assert resp.json['err'] == 1
45
    assert resp.json['err_class'] == 'passerelle.views.WrongParameter'
46
    assert 'missing parameters' in resp.json['err_desc']
47
    resp = app.get(stop_endpoint, params={'apikey': '123'}, status=400)
48
    assert resp.json['err'] == 1
49
    assert resp.json['err_class'] == 'passerelle.views.WrongParameter'
50
    assert 'missing parameters' in resp.json['err_desc']
51

  
52
    Call.objects.all().delete()
53

  
54
    resp = app.get(start_endpoint, status=200, params={'apikey': '123',
55
                                                       'callee': '42',
56
                                                       'caller': '0612345678'})
57
    assert resp.json['err'] == 0
58
    assert resp.json['data']['callee'] == '42'
59
    assert resp.json['data']['caller'] == '0612345678'
60
    assert 'start' in resp.json['data']
61
    assert resp.json['data']['end'] is None
62
    assert resp.json['data']['is_current'] is True
63
    assert resp.json['data']['details'] == {}
64
    assert Call.objects.count() == 1
65
    call = Call.objects.first()
66
    assert call.callee == '42'
67
    assert call.caller == '0612345678'
68
    assert call.end_timestamp is None
69
    assert call.details == {}
70
    json_start = resp.json['data']['start']
71
    start = call.start_timestamp
72

  
73
    # same call, do nothing
74
    resp = app.get(start_endpoint, status=200, params={'apikey': '123',
75
                                                       'callee': '42',
76
                                                       'caller': '0612345678'})
77
    assert resp.json['err'] == 0
78
    assert resp.json['data']['callee'] == '42'
79
    assert resp.json['data']['caller'] == '0612345678'
80
    assert resp.json['data']['start'] == json_start
81
    assert resp.json['data']['end'] is None
82
    assert resp.json['data']['is_current'] is True
83
    assert Call.objects.count() == 1
84
    call = Call.objects.first()
85
    assert call.callee == '42'
86
    assert call.caller == '0612345678'
87
    assert call.end_timestamp is None
88
    assert call.start_timestamp == start
89
    assert call.details == {}
90
    resp = app.get(start_endpoint, status=200, params={'apikey': '123',
91
                                                       'callee': '42',
92
                                                       'caller': '0612345678',
93
                                                       'foo': 'bar'})  # add details
94
    assert resp.json['err'] == 0
95
    assert resp.json['data']['callee'] == '42'
96
    assert resp.json['data']['caller'] == '0612345678'
97
    assert resp.json['data']['start'] == json_start
98
    assert resp.json['data']['end'] is None
99
    assert resp.json['data']['is_current'] is True
100
    assert resp.json['data']['details'] == {'foo': 'bar'}
101
    assert Call.objects.count() == 1
102
    call = Call.objects.first()
103
    assert call.callee == '42'
104
    assert call.caller == '0612345678'
105
    assert call.end_timestamp is None
106
    assert call.start_timestamp == start
107
    assert call.details == {'foo': 'bar'}
108

  
109
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123'})
110
    assert resp.json['err'] == 0
111
    assert len(resp.json['data']['current']) == 1
112
    assert len(resp.json['data']['past']) == 0
113
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123', 'callee': '42'})
114
    assert resp.json['err'] == 0
115
    assert len(resp.json['data']['current']) == 1
116
    assert len(resp.json['data']['past']) == 0
117
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123', 'callee': '43'})
118
    assert resp.json['err'] == 0
119
    assert len(resp.json['data']['current']) == 0
120
    assert len(resp.json['data']['past']) == 0
121

  
122
    resp = app.get(start_endpoint, status=200, params={'apikey': '123',
123
                                                       'callee': '43',
124
                                                       'caller': '0687654321'})
125
    assert resp.json['err'] == 0
126
    assert resp.json['data']['callee'] == '43'
127
    assert resp.json['data']['caller'] == '0687654321'
128
    assert Call.objects.count() == 2
129
    assert Call.objects.filter(end_timestamp__isnull=True).count() == 2
130

  
131
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123', 'callee': '42'})
132
    assert resp.json['err'] == 0
133
    assert len(resp.json['data']['current']) == 1
134
    assert len(resp.json['data']['past']) == 0
135
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123', 'callee': '43'})
136
    assert resp.json['err'] == 0
137
    assert len(resp.json['data']['current']) == 1
138
    assert len(resp.json['data']['past']) == 0
139

  
140
    resp = app.get(stop_endpoint, status=200, params={'apikey': '123',
141
                                                      'callee': '43',
142
                                                      'caller': '0687654321'})
143
    assert resp.json['err'] == 0
144
    assert len(resp.json['data']) == 1
145
    assert resp.json['data'][0]['callee'] == '43'
146
    assert resp.json['data'][0]['caller'] == '0687654321'
147
    assert resp.json['data'][0]['start'] is not None
148
    assert resp.json['data'][0]['end'] is not None
149
    assert resp.json['data'][0]['is_current'] is False
150
    assert Call.objects.count() == 2
151
    assert Call.objects.filter(end_timestamp__isnull=True).count() == 1
152
    assert Call.objects.filter(end_timestamp__isnull=False).count() == 1
153

  
154
    # calls by callee
155
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123', 'callee': '42'})
156
    assert resp.json['err'] == 0
157
    assert len(resp.json['data']['current']) == 1
158
    assert len(resp.json['data']['past']) == 0
159
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123', 'callee': '43'})
160
    assert resp.json['err'] == 0
161
    assert len(resp.json['data']['current']) == 0
162
    assert len(resp.json['data']['past']) == 1
163
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123'})
164
    assert resp.json['err'] == 0
165
    assert len(resp.json['data']['current']) == 1
166
    assert len(resp.json['data']['past']) == 1
167
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123', 'callee': 'foo'})
168
    assert resp.json['err'] == 0
169
    assert len(resp.json['data']['current']) == 0
170
    assert len(resp.json['data']['past']) == 0
171

  
172
    # calls by caller
173
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123',
174
                                                       'caller': '0612345678'})
175
    assert resp.json['err'] == 0
176
    assert len(resp.json['data']['current']) == 1
177
    assert len(resp.json['data']['past']) == 0
178
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123',
179
                                                       'caller': '0687654321'})
180
    assert resp.json['err'] == 0
181
    assert len(resp.json['data']['current']) == 0
182
    assert len(resp.json['data']['past']) == 1
183
    resp = app.get(calls_endpoint, status=200, params={'apikey': '123',
184
                                                       'caller': 'foo'})
185
    assert resp.json['err'] == 0
186
    assert len(resp.json['data']['current']) == 0
187
    assert len(resp.json['data']['past']) == 0
188

  
189
    # create a "too long" current call (> 120 minutes == phonecalls.max_call_duration)
190
    assert Call.objects.count() == 2
191
    assert Call.objects.filter(end_timestamp__isnull=True).count() == 1
192
    assert Call.objects.filter(end_timestamp__isnull=False).count() == 1
193
    current_call = Call.objects.filter(end_timestamp__isnull=True).first()
194
    current_call.start_timestamp = now() - timedelta(minutes=200)
195
    current_call.save()
196
    # close too long calls
197
    phonecalls.hourly()
198
    assert Call.objects.count() == 2
199
    assert Call.objects.filter(end_timestamp__isnull=True).count() == 0
200
    assert Call.objects.filter(end_timestamp__isnull=False).count() == 2
201

  
202
    # create a "too old" call (> 60 days == phonecalls.data_retention_period)
203
    old_call = Call.objects.first()
204
    old_call.start_timestamp = old_call.start_timestamp - timedelta(days=100)
205
    old_call.end_timestamp = old_call.end_timestamp - timedelta(days=100)
206
    old_call.save()
207
    # remove old calls
208
    phonecalls.daily()
209
    assert Call.objects.count() == 1
210
    assert Call.objects.filter(end_timestamp__isnull=True).count() == 0
211
    assert Call.objects.filter(end_timestamp__isnull=False).count() == 1
0
-