import_representatives.py 16 KB
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1
2
3
4
5
6
7
8
9
10
11
12
13
# coding: utf-8

import logging
import sys
from datetime import datetime

import django.dispatch
import ijson
import django
from django.apps import apps
from django.db import transaction
from django.utils import timezone

14
from representatives.models import (Country, Mandate, Email, Address, WebSite,
15
16
                                    Representative, Constituency, Phone, Group,
                                    Chamber)
Nicolas Joyard's avatar
Nicolas Joyard committed
17
18
19
20
21
22
23
24
25
26

logger = logging.getLogger(__name__)

representative_pre_import = django.dispatch.Signal(
    providing_args=['representative_data'])


def _get_rep_district_name(json):
    num = json.get('num_circo')
    nom = json.get('nom_circo')
27
28
29
30
31
32

    if num == 'nd':
        return nom
    else:
        ordinal = u'ère' if num == 1 else u'ème'
        return '%s (%d%s circonscription)' % (nom, num, ordinal)
Nicolas Joyard's avatar
Nicolas Joyard committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54


def _get_rep_parl_groups(json):
    return [{
        'name': g['responsabilite']['organisme'],
        'role': g['responsabilite']['fonction'],
        'start': json['mandat_debut']
    } for g in json['groupes_parlementaires']]


def _get_rep_comittees(json):
    return [{
        'name': g['responsabilite']['organisme'],
        'role': g['responsabilite']['fonction'],
        'start': json['mandat_debut']
    } for g in json['responsabilites']
        if g['responsabilite']['organisme'].startswith('Commission')]


#
# Variant configuration
# - mail_domain is used to distinguish official vs personal emails
55
# - off_* fields are used for the official address of meps
Nicolas Joyard's avatar
Nicolas Joyard committed
56
57
58
59
# - mandates defines how mandates are created from the rep json
#
# Mandates are defined as follows
# - 'kind' indicates the group kind, a constant string
60
# - 'chamber' tells whether the group belongs to the chamber
Nicolas Joyard's avatar
Nicolas Joyard committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# - 'from', if present, must be a function that takes the rep json and returns
#   an array of dicts; one group will be created from each item in the dict.
#   When 'from' is not present, only one group wil be created using the rep
#   json (IOW, 'from' defaults to lambda repjson: [repjson])
# - 'name', 'abbr', 'role', 'start', 'end' are strings that are interpolated
#   against the rep json or items returned by 'from'.
# - 'name_path', 'abbr_path', etc. can replace 'name', 'abbr'... by specifying
#   a slash-separated dictionnary path where the value is to be found in the
#   rep json or item returned by 'from'
# - 'name_fn', 'abbr_fn', etc. can also replace 'name', 'abbr'... by a
#   function that takes the input item (rep json or item returned by 'from')
#   and returns the value
#
FranceDataVariants = {
75
76
    "AN": {
        "chamber": u"Assemblée nationale",
77
        "remote_id_field": "url_an",
Nicolas Joyard's avatar
Nicolas Joyard committed
78
        "mail_domain": "@assemblee-nationale.fr",
79
80
81
82
83
        "off_city": "Paris",
        "off_street": u"Rue de l'Université",
        "off_number": "126",
        "off_code": "75355",
        "off_name": u"Assemblée nationale",
Nicolas Joyard's avatar
Nicolas Joyard committed
84
85
86
        "mandates": [
            {
                "kind": "group",
87
                "chamber": True,
Nicolas Joyard's avatar
Nicolas Joyard committed
88
89
                "abbr": "%(groupe_sigle)s",
                "name_path": "groupe/organisme",
90
                "role_path": "groupe/fonction",
Nicolas Joyard's avatar
Nicolas Joyard committed
91
92
93
94
95
96
97
98
99
100
                "start": "%(mandat_debut)s"
            },
            {
                "kind": "department",
                "abbr": "%(num_deptmt)s",
                "name": "%(nom_circo)s",
                "start": "%(mandat_debut)s"
            },
            {
                "kind": "district",
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
                "abbr": "%(num_deptmt)s-%(num_circo)s",
                "name_fn": _get_rep_district_name,
                "start": "%(mandat_debut)s"
            },
            {
                "kind": "parl-group",
                "chamber": True,
                "from": _get_rep_parl_groups,
                "abbr": "%(name)s",
                "name": "%(name)s",
                "role": "%(role)s",
                "start": "%(start)s"
            },
            {
                "kind": "comittee",
116
                "chamber": True,
117
118
119
120
121
122
123
124
125
126
127
                "from": _get_rep_comittees,
                "abbr": "%(name)s",
                "name": "%(name)s",
                "role": "%(role)s",
                "start": "%(start)s"
            }
        ]
    },

    "SEN": {
        "chamber": u"Sénat",
128
        "remote_id_field": "url_institution",
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
        "mail_domain": "@senat.fr",
        "off_city": "Paris",
        "off_street": u"Rue de Vaugirard",
        "off_number": "15",
        "off_code": "75291",
        "off_name": u"Palais du Luxembourg",
        "mandates": [
            {
                "kind": "group",
                "chamber": True,
                "abbr": "%(groupe_sigle)s",
                "name_path": "groupe/organisme",
                "role_path": "groupe/fonction",
                "start": "%(mandat_debut)s"
            },
            {
                "kind": "department",
                "abbr": "%(num_deptmt)s",
                "name": "%(nom_circo)s",
                "start": "%(mandat_debut)s"
            },
            {
                "kind": "district",
                "abbr": "%(num_deptmt)s-%(num_circo)s",
Nicolas Joyard's avatar
Nicolas Joyard committed
153
154
155
156
157
                "name_fn": _get_rep_district_name,
                "start": "%(mandat_debut)s"
            },
            {
                "kind": "parl-group",
158
                "chamber": True,
Nicolas Joyard's avatar
Nicolas Joyard committed
159
160
161
162
163
164
165
166
                "from": _get_rep_parl_groups,
                "abbr": "%(name)s",
                "name": "%(name)s",
                "role": "%(role)s",
                "start": "%(start)s"
            },
            {
                "kind": "comittee",
167
                "chamber": True,
Nicolas Joyard's avatar
Nicolas Joyard committed
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
                "from": _get_rep_comittees,
                "abbr": "%(name)s",
                "name": "%(name)s",
                "role": "%(role)s",
                "start": "%(start)s"
            }
        ]
    }
}


def _get_mdef_item(mdef, item, json, default=None):
    if item in mdef:
        return mdef[item] % json

    if '%s_path' % item in mdef:
        return _get_path(json, mdef['%s_path' % item])

    if '%s_fn' % item in mdef:
        return mdef['%s_fn' % item](json)

    return default


def _parse_date(date):
    return datetime.strptime(date, "%Y-%m-%d").date()


196
197
def _create_mandate(representative, group, constituency, role='',
                    begin_date=None, end_date=None):
Nicolas Joyard's avatar
Nicolas Joyard committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    mandate, _ = Mandate.objects.get_or_create(
        representative=representative,
        group=group,
        constituency=constituency,
        role=role,
        begin_date=begin_date,
        end_date=end_date
    )

    if _:
        logger.debug('Created mandate %s', mandate.pk)


def _get_path(dict_, path):
    '''
    Get value at specific path in dictionary. Path is specified by slash-
    separated string, eg _get_path(foo, 'bar/baz') returns foo['bar']['baz']
    '''
    cur = dict_
    for part in path.split('/'):
        cur = cur[part]
    return cur


class GenericImporter(object):

    def pre_import(self):
        self.import_start_datetime = timezone.now()

    def touch_model(self, model, **data):
        '''
        This method create or look up a model with the given data
        it saves the given model if it exists, updating its
        updated field
        '''
233

Nicolas Joyard's avatar
Nicolas Joyard committed
234
235
236
237
238
239
240
241
242
243
        instance, created = model.objects.get_or_create(**data)

        if not created:
            if instance.updated < self.import_start_datetime:
                instance.save()     # Updates updated field

        return (instance, created)


class FranceDataImporter(GenericImporter):
244
    url = 'http://francedata.future/data/parlementaires.json'
Nicolas Joyard's avatar
Nicolas Joyard committed
245
246
247
248
249

    def parse_date(self, date):
        return _parse_date(date)

    def __init__(self, variant):
250
        self.france = Country.objects.get(name="France")
Nicolas Joyard's avatar
Nicolas Joyard committed
251
        self.variant = FranceDataVariants[variant]
252
253
254
255
256
257
258
        self.chamber, _ = Chamber.objects.get_or_create(
            name=self.variant['chamber'], country=self.france)
        self.ch_constituency, _ = Constituency.objects.get_or_create(
            name=self.variant['chamber'], country=self.france)
        self.ch_group, _ = Group.objects.get_or_create(
            name=self.variant['chamber'], kind='chamber', abbreviation=variant,
            chamber=self.chamber)
Nicolas Joyard's avatar
Nicolas Joyard committed
259
260
261
262
263
264
265
266
267

    @transaction.atomic
    def manage_rep(self, rep_json):
        '''
        Import a rep as a representative from the json dict fetched from
        FranceData (which comes from nosdeputes.fr)
        '''
        remote_id = rep_json[self.variant['remote_id_field']]

268
269
270
        if rep_json['num_circo'] == 'non disponible':
            rep_json['num_circo'] = 'nd'

Nicolas Joyard's avatar
Nicolas Joyard committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
        if not remote_id:
            logger.warning('Skipping MEP without UID %s %s',
                           rep_json['nom'],
                           rep_json[self.variant['remote_id_field']])
            return

        # Some versions of memopol will connect to this and skip inactive reps.
        responses = representative_pre_import.send(sender=self,
                representative_data=rep_json)

        for receiver, response in responses:
            if response is False:
                logger.debug(
                    'Skipping MEP %s', rep_json['nom'])
                return

        try:
            representative = Representative.objects.get(remote_id=remote_id)
        except Representative.DoesNotExist:
            representative = Representative(remote_id=remote_id)

        # Save representative attributes
        self.import_representative_details(representative, rep_json)

        representative.save()

        self.add_mandates(representative, rep_json)

        self.add_contacts(representative, rep_json)

        logger.debug('Imported MEP %s', unicode(representative))

        return representative

    def import_representative_details(self, representative, rep_json):
        representative.active = True

        if rep_json.get("date_naissance"):
            representative.birth_date = _parse_date(rep_json["date_naissance"])
        if rep_json.get("lieu_naissance"):
            representative.birth_place = rep_json["lieu_naissance"]

        representative.photo = rep_json['photo_url']
        representative.full_name = rep_json["nom"]

        gender_convertion_dict = {
            u"F": 1,
            u"H": 2
        }
        if 'sexe' in rep_json:
            representative.gender = gender_convertion_dict.get(rep_json[
                                                               'sexe'], 0)
        else:
            representative.gender = 0

        representative.slug = rep_json['slug']

    def add_mandates(self, representative, rep_json):
        '''
        Create mandates from rep data based on variant configuration
        '''

333
334
335
336
337
338
339
340
341
342
343
344
345
        # Mandate in country group for party constituency
        if rep_json.get('parti_ratt_financier'):
            constituency, _ = Constituency.objects.get_or_create(
                name=rep_json.get('parti_ratt_financier'), country=self.france)

            group, _ = self.touch_model(model=Group,
                                        abbreviation=self.france.code,
                                        kind='country',
                                        name=self.france.name)

            _create_mandate(representative, group, constituency, 'membre')

        # Configurable mandates
Nicolas Joyard's avatar
Nicolas Joyard committed
346
        for mdef in self.variant['mandates']:
347
348
349
350
351
            if mdef.get('chamber', False):
                chamber = self.chamber
            else:
                chamber = None

Nicolas Joyard's avatar
Nicolas Joyard committed
352
353
354
355
356
357
358
359
360
361
362
363
            if 'from' in mdef:
                elems = mdef['from'](rep_json)
            else:
                elems = [rep_json]

            for elem in elems:
                name = _get_mdef_item(mdef, 'name', elem, '')
                abbr = _get_mdef_item(mdef, 'abbr', elem, '')

                group, _ = self.touch_model(model=Group,
                                            abbreviation=abbr,
                                            kind=mdef['kind'],
364
                                            chamber=chamber,
Nicolas Joyard's avatar
Nicolas Joyard committed
365
366
367
368
369
370
371
372
373
374
                                            name=name)

                role = _get_mdef_item(mdef, 'role', elem, 'membre')
                start = _get_mdef_item(mdef, 'start', elem, None)
                if start is not None:
                    start = _parse_date(start)
                end = _get_mdef_item(mdef, 'end', elem, None)
                if end is not None:
                    end = _parse_date(end)

375
376
                _create_mandate(representative, group, self.ch_constituency,
                                role, start, end)
Nicolas Joyard's avatar
Nicolas Joyard committed
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411

                logger.debug(
                    '%s => %s: %s of "%s" (%s) %s-%s' % (rep_json['slug'],
                    mdef['kind'], role, name, abbr, start, end))

    def add_contacts(self, representative, rep_json):
        # Websites
        websites = rep_json.get('sites_web', [])
        for site in websites:
            if not site['site'].startswith('http://twitter.com/'):
                self.touch_model(model=WebSite,
                                 url=site['site'],
                                 representative=representative
                                 )

        # Twitter
        if rep_json.get('twitter'):
            tid = rep_json.get('twitter')
            self.touch_model(model=WebSite,
                             representative=representative,
                             kind='twitter',
                             url='http://twitter.com/%s' % tid
                             )

        # E-mails
        emails = rep_json.get('emails', [])
        for email in emails:
            mail = email['email']
            self.touch_model(
                model=Email,
                representative=representative,
                kind=('official' if mail.endswith(self.variant['mail_domain'])
                    else 'other'),
                email=mail)

412
413
414
415
416
417
418
419
420
421
422
423
424
        # Official address
        off_name = self.variant['off_name']
        official_addr, _ = self.touch_model(model=Address,
                                            representative=representative,
                                            country=self.france,
                                            city=self.variant['off_city'],
                                            street=self.variant['off_street'],
                                            number=self.variant['off_number'],
                                            postcode=self.variant['off_code'],
                                            kind='official',
                                            name=off_name
                                            )

Nicolas Joyard's avatar
Nicolas Joyard committed
425
426
        # Addresses & phone numbers
        addresses = rep_json.get('adresses', [])
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
        for item in addresses:
            addr = None
            if 'geo' in item:
                props = item['geo'].get('properties', {})
                name = ''

                if item['adresse'].lower().startswith('permanence'):
                    name = 'Permanence'

                addr, _ = self.touch_model(model=Address,
                                           representative=representative,
                                           country=self.france,
                                           city=props.get('city', ''),
                                           street=props.get('street', ''),
                                           number=props.get('housenumber', ''),
                                           postcode=props.get('postcode', ''),
                                           kind='',
                                           name=name
                                           )
            elif item['adresse'].lower().startswith(off_name.lower()):
                addr = official_addr

            if 'tel' in item:
                self.touch_model(model=Phone, address=addr,
Nicolas Joyard's avatar
Nicolas Joyard committed
451
                                 representative=representative,
452
                                 kind='', number=item['tel']
Nicolas Joyard's avatar
Nicolas Joyard committed
453
454
455
456
457
458
459
                                 )


def main(stream=None):
    if not apps.ready:
        django.setup()

460
461
462
463
464
    an_importer = FranceDataImporter('AN')
    GenericImporter.pre_import(an_importer)

    sen_importer = FranceDataImporter('SEN')
    GenericImporter.pre_import(sen_importer)
Nicolas Joyard's avatar
Nicolas Joyard committed
465
466
467

    for data in ijson.items(stream or sys.stdin, ''):
        for rep in data:
468
469
470
471
            if rep['chambre'] == 'AN':
                an_importer.manage_rep(rep)
            elif rep['chambre'] == 'SEN':
                sen_importer.manage_rep(rep)