0001-standardize-phonenumber-format-23443.patch
corbo/migrations/0011_auto_20180426_1334.py | ||
---|---|---|
1 |
# -*- coding: utf-8 -*- |
|
2 |
from __future__ import unicode_literals |
|
3 | ||
4 |
import re |
|
5 | ||
6 |
from django.db import migrations |
|
7 | ||
8 | ||
9 |
def format_sms_identifiers(apps, schema_editor): |
|
10 |
Subscription = apps.get_model('corbo', 'Subscription') |
|
11 |
uri = 'sms:' |
|
12 |
for subscription in Subscription.objects.filter(identifier__startswith='sms:'): |
|
13 |
_, phonenumber = subscription.identifier.split(':') |
|
14 |
phonenumber = re.sub('[-.\s]', '', phonenumber) |
|
15 |
subscription.identifier = uri + phonenumber |
|
16 |
subscription.save() |
|
17 | ||
18 | ||
19 |
class Migration(migrations.Migration): |
|
20 | ||
21 |
dependencies = [ |
|
22 |
('corbo', '0010_broadcast_delivery_count'), |
|
23 |
] |
|
24 | ||
25 |
operations = [ |
|
26 |
migrations.RunPython(format_sms_identifiers, format_sms_identifiers), |
|
27 |
] |
corbo/models.py | ||
---|---|---|
1 | 1 |
import os |
2 | 2 |
import io |
3 |
import re |
|
3 | 4 |
import hashlib |
4 | 5 |
from time import mktime |
5 | 6 |
from datetime import datetime |
... | ... | |
11 | 12 |
from django.conf import settings |
12 | 13 |
from django.db import models |
13 | 14 |
from django.core.files.storage import DefaultStorage |
15 |
from django.core.exceptions import ValidationError |
|
14 | 16 |
from django.utils.translation import ugettext_lazy as _ |
15 | 17 | |
16 | 18 |
from ckeditor.fields import RichTextField |
... | ... | |
177 | 179 |
self.deliver_time = timezone.now() |
178 | 180 |
self.save() |
179 | 181 | |
180 | ||
181 | 182 |
class Meta: |
182 | 183 |
verbose_name = _('sent') |
183 | 184 |
ordering = ('-deliver_time',) |
... | ... | |
198 | 199 | |
199 | 200 |
class Meta: |
200 | 201 |
unique_together = ('category', 'identifier', 'uuid') |
202 | ||
203 |
def clean(self): |
|
204 |
if 'sms:' in self.identifier: |
|
205 |
uri, phonenumber = self.identifier.split(':') |
|
206 |
phonenumber = re.sub('[-.\s]', '', phonenumber) |
|
207 |
if not re.search('^\+?\d{,20}$', phonenumber): |
|
208 |
raise ValidationError({'identifier': _('Invalid phone number')}) |
|
209 |
self.identifier = '%s:%s' % (uri, phonenumber) |
|
210 | ||
211 |
def save(self, *args, **kwargs): |
|
212 |
self.clean() |
|
213 |
return super(Subscription, self).save(*args, **kwargs) |
tests/test_api.py | ||
---|---|---|
70 | 70 | |
71 | 71 |
for identifier, name in channel_choices: |
72 | 72 |
for category in categories: |
73 |
uri = '%s:%s' % (identifier, uuid)
|
|
73 |
uri = '%s:%s' % (identifier, '00339988776655')
|
|
74 | 74 |
Subscription.objects.create(identifier=uri, uuid=uuid, category=category) |
75 | 75 |
resp = app.get(reverse('subscriptions'), params={'uuid': uuid}) |
76 | 76 |
assert len(resp.json['data']) == 2 |
tests/test_data_migrations.py | ||
---|---|---|
1 |
import uuid |
|
2 | ||
3 |
import pytest |
|
4 | ||
5 |
from django.db import connection |
|
6 |
from django.db.migrations.executor import MigrationExecutor |
|
7 | ||
8 |
pytestmark = pytest.mark.django_db |
|
9 | ||
10 | ||
11 |
def test_subscription_sms_identifier_format_migration(): |
|
12 |
executor = MigrationExecutor(connection) |
|
13 |
app = 'corbo' |
|
14 |
migrate_from = [(app, '0009_auto_20170120_1533')] |
|
15 |
migrate_to = [(app, '0011_auto_20180426_1334')] |
|
16 |
executor.migrate(migrate_from) |
|
17 |
executor.loader.build_graph() |
|
18 |
old_apps = executor.loader.project_state(migrate_from).apps |
|
19 |
Category = old_apps.get_model('corbo', 'Category') |
|
20 |
Subscription = old_apps.get_model('corbo', 'Subscription') |
|
21 |
category = Category.objects.create(name='Science', slug='science') |
|
22 |
for identifier in ('sms:06 10 20 30 40', 'sms:+33 6 10 20 30 40', 'sms:0033610203040', 'mailto:john@doe.com'): |
|
23 |
Subscription.objects.create(uuid=uuid.uuid4().hex, category=category, identifier=identifier) |
|
24 |
executor.migrate(migrate_to) |
|
25 |
executor.loader.build_graph() |
|
26 |
apps = executor.loader.project_state(migrate_from).apps |
|
27 |
Subscription = apps.get_model('corbo', 'Subscription') |
|
28 |
assert Subscription.objects.count() == 4 |
|
29 |
valid_sms_identifier = ['sms:0610203040', 'sms:+33610203040', 'sms:0033610203040'] |
|
30 |
for subscription in Subscription.objects.filter(identifier__startswith='sms:'): |
|
31 |
assert subscription.identifier in valid_sms_identifier |
|
0 |
- |