0001-sfr_dmc-implemented-SFR-DMC-sms-connector-20973.patch
passerelle/apps/sfr_dmc/migrations/0001_initial.py | ||
---|---|---|
1 |
# Generated by Django 2.2.24 on 2022-02-09 11:10 |
|
2 | ||
3 |
import django.contrib.postgres.fields |
|
4 |
from django.db import migrations, models |
|
5 | ||
6 |
import passerelle.sms.models |
|
7 | ||
8 | ||
9 |
class Migration(migrations.Migration): |
|
10 | ||
11 |
initial = True |
|
12 | ||
13 |
dependencies = [ |
|
14 |
('base', '0029_auto_20210202_1627'), |
|
15 |
] |
|
16 | ||
17 |
operations = [ |
|
18 |
migrations.CreateModel( |
|
19 |
name='SfrDmcGateway', |
|
20 |
fields=[ |
|
21 |
( |
|
22 |
'id', |
|
23 |
models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'), |
|
24 |
), |
|
25 |
('title', models.CharField(max_length=50, verbose_name='Title')), |
|
26 |
('slug', models.SlugField(unique=True, verbose_name='Identifier')), |
|
27 |
('description', models.TextField(verbose_name='Description')), |
|
28 |
( |
|
29 |
'default_country_code', |
|
30 |
models.CharField(default='33', max_length=3, verbose_name='Default country code'), |
|
31 |
), |
|
32 |
( |
|
33 |
'default_trunk_prefix', |
|
34 |
models.CharField(default='0', max_length=2, verbose_name='Default trunk prefix'), |
|
35 |
), |
|
36 |
( |
|
37 |
'max_message_length', |
|
38 |
models.IntegerField( |
|
39 |
default=2000, |
|
40 |
help_text='Messages over this limit will be truncated.', |
|
41 |
verbose_name='Maximum message length', |
|
42 |
), |
|
43 |
), |
|
44 |
( |
|
45 |
'authorized', |
|
46 |
django.contrib.postgres.fields.ArrayField( |
|
47 |
base_field=models.CharField( |
|
48 |
choices=[ |
|
49 |
('fr-metro', 'France mainland (+33 [67])'), |
|
50 |
('fr-domtom', 'France DOM/TOM (+262, etc.)'), |
|
51 |
('be', 'Belgian (+32 4[5-9]) '), |
|
52 |
('all', 'All'), |
|
53 |
], |
|
54 |
max_length=32, |
|
55 |
null=True, |
|
56 |
), |
|
57 |
default=passerelle.sms.models.authorized_default, |
|
58 |
size=None, |
|
59 |
verbose_name='Authorized Countries', |
|
60 |
), |
|
61 |
), |
|
62 |
( |
|
63 |
'allow_premium_rate', |
|
64 |
models.BooleanField( |
|
65 |
default=False, |
|
66 |
help_text='This option is only applyed to France mainland', |
|
67 |
verbose_name='Allow premium rate numbers', |
|
68 |
), |
|
69 |
), |
|
70 |
('service_id', models.CharField(max_length=15, verbose_name='Service ID')), |
|
71 |
('service_password', models.CharField(max_length=15, verbose_name='Service Password')), |
|
72 |
('space_id', models.IntegerField(verbose_name='Space ID')), |
|
73 |
( |
|
74 |
'users', |
|
75 |
models.ManyToManyField( |
|
76 |
blank=True, |
|
77 |
related_name='_sfrdmcgateway_users_+', |
|
78 |
related_query_name='+', |
|
79 |
to='base.ApiUser', |
|
80 |
), |
|
81 |
), |
|
82 |
], |
|
83 |
options={ |
|
84 |
'verbose_name': 'SFR DMC', |
|
85 |
}, |
|
86 |
), |
|
87 |
] |
passerelle/apps/sfr_dmc/models.py | ||
---|---|---|
1 |
# passerelle - uniform access to multiple data sources and services |
|
2 |
# Copyright (C) 2022 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 |
import json |
|
17 | ||
18 |
from django.db import models |
|
19 |
from django.utils.translation import ugettext_lazy as _ |
|
20 | ||
21 |
from passerelle.sms.models import SMSResource |
|
22 |
from passerelle.utils.jsonresponse import APIError |
|
23 | ||
24 | ||
25 |
class SfrDmcGateway(SMSResource): |
|
26 |
service_id = models.CharField(verbose_name=_('Service ID'), max_length=15) |
|
27 |
service_password = models.CharField(verbose_name=_('Service Password'), max_length=15) |
|
28 |
space_id = models.IntegerField(verbose_name=_('Space ID')) |
|
29 | ||
30 |
TEST_DEFAULTS = { |
|
31 |
'create_kwargs': { |
|
32 |
'service_id': '1234', |
|
33 |
'service_password': 'krascuky', |
|
34 |
'space_id': '1234', |
|
35 |
}, |
|
36 |
'test_vectors': [ |
|
37 |
{ |
|
38 |
'response': '', |
|
39 |
'result': { |
|
40 |
'err': 1, |
|
41 |
'err_desc': 'SFR DMC error: some destinations failed', |
|
42 |
'data': [ |
|
43 |
['0033688888888', 'SFR DMC error: Bad JSON response'], |
|
44 |
['0033677777777', 'SFR DMC error: Bad JSON response'], |
|
45 |
], |
|
46 |
}, |
|
47 |
}, |
|
48 |
{ |
|
49 |
'response': {'success': False, 'no_error_detail_field': 'bad_json'}, |
|
50 |
'result': { |
|
51 |
'err': 1, |
|
52 |
'err_desc': 'SFR DMC error: some destinations failed', |
|
53 |
'data': [ |
|
54 |
['0033688888888', 'SFR DMC error: Bad JSON response'], |
|
55 |
['0033677777777', 'SFR DMC error: Bad JSON response'], |
|
56 |
], |
|
57 |
}, |
|
58 |
}, |
|
59 |
{ |
|
60 |
'response': {'success': False, 'errorDetail': 'sfr error description'}, |
|
61 |
'result': { |
|
62 |
'err': 1, |
|
63 |
'err_desc': 'SFR DMC error: some destinations failed', |
|
64 |
'data': [ |
|
65 |
[ |
|
66 |
'0033688888888', |
|
67 |
{'result': 'SFR DMC error: sfr error description', 'sms_id': 1234}, |
|
68 |
], |
|
69 |
[ |
|
70 |
'0033677777777', |
|
71 |
{'result': 'SFR DMC error: sfr error description', 'sms_id': 1234}, |
|
72 |
], |
|
73 |
], |
|
74 |
}, |
|
75 |
}, |
|
76 |
{ |
|
77 |
'response': {'success': True, 'response': ['1234', '5678']}, |
|
78 |
'result': { |
|
79 |
'err': 0, |
|
80 |
'data': [ |
|
81 |
['0033688888888', {'result': 'Envoi terminé', 'sms_id': 1234}], |
|
82 |
['0033677777777', {'result': 'Envoi terminé', 'sms_id': 1234}], |
|
83 |
], |
|
84 |
}, |
|
85 |
}, |
|
86 |
], |
|
87 |
} |
|
88 |
URL = 'https://www.dmc.sfr-sh.fr/DmcWS/1.5.7' |
|
89 | ||
90 |
class Meta: |
|
91 |
verbose_name = _('SFR DMC') |
|
92 | ||
93 |
def send_msg(self, text, sender, destinations, **kwargs): |
|
94 |
"""Send a SMS using the SFR DMC provider""" |
|
95 |
errors = [] |
|
96 | ||
97 |
# SFR API limitation, we slice destinations in chunks of 20 items |
|
98 |
batch_size = 20 |
|
99 |
batches = [destinations[i : i + batch_size] for i in range(0, len(destinations), batch_size)] |
|
100 | ||
101 |
for batch in batches: |
|
102 | ||
103 |
def add_error(message, destination=None): |
|
104 |
if destination is None: |
|
105 |
for dest in batch: |
|
106 |
errors.append((dest, f'SFR DMC Error : {message}')) |
|
107 |
else: |
|
108 |
errors.append((destination, f'SFR DMC Error : {message}')) |
|
109 | ||
110 |
local_destinations = [] |
|
111 |
# The API seems to only accept number without country codes. |
|
112 |
for dest in destinations: |
|
113 |
if dest[0:4] != '0033': |
|
114 |
add_error('SFR DMC API doesn\'t support country code in phone number.') |
|
115 |
continue |
|
116 |
local_destinations.append(f'0{dest[4:]}') |
|
117 | ||
118 |
response = self.requests.get( |
|
119 |
f'{self.URL}/JsonService/MessagesUnitairesWS/batchSingleCall', |
|
120 |
params={ |
|
121 |
'authenticate': json.dumps( |
|
122 |
{ |
|
123 |
'serviceId': self.service_id, |
|
124 |
'servicePassword': self.service_password, |
|
125 |
'spaceId': self.space_id, |
|
126 |
} |
|
127 |
), |
|
128 |
'messageUnitaires': json.dumps( |
|
129 |
[ |
|
130 |
{'media': 'SMS', 'textMsg': text, 'from': sender, 'to': dest} |
|
131 |
for dest in local_destinations |
|
132 |
] |
|
133 |
), |
|
134 |
}, |
|
135 |
) |
|
136 | ||
137 |
try: |
|
138 |
json_data = response.json() |
|
139 | ||
140 |
if 'success' not in json_data: |
|
141 |
add_error('Bad JSON response') |
|
142 |
elif not json_data['success']: |
|
143 |
if not 'errorDetail' in json_data: |
|
144 |
add_error('Bad JSON response') |
|
145 |
else: |
|
146 |
add_error(json_data['errorDetail']) |
|
147 | ||
148 |
except ValueError: |
|
149 |
add_error('Bad JSON response') |
|
150 | ||
151 |
if errors: |
|
152 |
raise APIError('SFR DMC error: some destinations failed', data=errors) |
|
153 | ||
154 |
return None |
passerelle/settings.py | ||
---|---|---|
147 | 147 |
'passerelle.apps.gdc', |
148 | 148 |
'passerelle.apps.gesbac', |
149 | 149 |
'passerelle.apps.jsondatastore', |
150 |
'passerelle.apps.sp_fr', |
|
151 | 150 |
'passerelle.apps.maelis', |
152 | 151 |
'passerelle.apps.mdel', |
153 | 152 |
'passerelle.apps.mdel_ddpacs', |
... | ... | |
162 | 161 |
'passerelle.apps.photon', |
163 | 162 |
'passerelle.apps.plone_restapi', |
164 | 163 |
'passerelle.apps.sector', |
164 |
'passerelle.apps.sfr_dmc', |
|
165 | 165 |
'passerelle.apps.solis', |
166 |
'passerelle.apps.sp_fr', |
|
166 | 167 |
'passerelle.apps.twilio', |
167 | 168 |
'passerelle.apps.vivaticket', |
168 | 169 |
# backoffice templates and static |
169 |
- |