Projet

Général

Profil

0001-standardize-phonenumber-format-23443.patch

Josué Kouka, 27 avril 2018 11:34

Télécharger (5,2 ko)

Voir les différences:

Subject: [PATCH] standardize phonenumber format (#23443)

 corbo/migrations/0011_auto_20180426_1334.py | 28 +++++++++++++++++++++++++
 corbo/models.py                             | 14 ++++++++++++-
 tests/test_api.py                           |  2 +-
 tests/test_data_migrations.py               | 32 +++++++++++++++++++++++++++++
 4 files changed, 74 insertions(+), 2 deletions(-)
 create mode 100644 corbo/migrations/0011_auto_20180426_1334.py
 create mode 100644 tests/test_data_migrations.py
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('\(\d+\)', '', phonenumber)
15
        phonenumber = re.sub('[-.\s]', '', phonenumber)
16
        subscription.identifier = uri + phonenumber
17
        subscription.save()
18

  
19

  
20
class Migration(migrations.Migration):
21

  
22
    dependencies = [
23
        ('corbo', '0010_broadcast_delivery_count'),
24
    ]
25

  
26
    operations = [
27
        migrations.RunPython(format_sms_identifiers, format_sms_identifiers),
28
    ]
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('\(\d+\)', '', phonenumber)
207
            phonenumber = re.sub('[-.\s]', '', phonenumber)
208
            self.identifier = '%s:%s' % (uri, phonenumber)
209

  
210
    def save(self, *args, **kwargs):
211
        self.clean()
212
        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',
23
                       'mailto:john@doe.com', 'sms:+33 (0) 6 10 20 30 40'):
24
        Subscription.objects.create(uuid=uuid.uuid4().hex, category=category, identifier=identifier)
25
    executor.migrate(migrate_to)
26
    executor.loader.build_graph()
27
    apps = executor.loader.project_state(migrate_from).apps
28
    Subscription = apps.get_model('corbo', 'Subscription')
29
    assert Subscription.objects.count() == 5
30
    valid_sms_identifier = ['sms:0610203040', 'sms:+33610203040', 'sms:0033610203040']
31
    for subscription in Subscription.objects.filter(identifier__startswith='sms:'):
32
        assert subscription.identifier in valid_sms_identifier
0
-