import_recommendations.py 3.36 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
# coding: utf-8

import csv
import django
from django.apps import apps
import logging
import sys

from representatives_recommendations.models import Recommendation
from representatives_votes.models import Dossier, Proposal

12
from .import_data import dossier_mappings, resolutions
13

Nicolas Joyard's avatar
Nicolas Joyard committed
14
logger = logging.getLogger(__name__)
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30


class RecommendationImporter:
    def __init__(self):
        self.dossier_cache = {}

    def get_dossier(self, title):
        alt_title = dossier_mappings.get(title, None)
        if alt_title is not None:
            title = alt_title

        dossier = self.dossier_cache.get(title, None)

        if dossier is None:
            try:
                dossier = Dossier.objects.get(title__iexact=title)
31
                self.dossier_cache[title] = dossier
32
33
34
35
36
37
            except Dossier.DoesNotExist:
                dossier = None

        return dossier

    def get_proposal(self, dossier, kind):
38
39
        kinds = [kind]

40
        try:
41
42
43
44
45
46
47
48
49
50
51
52
            resolutions.index(kind.lower())
            kinds.extend(resolutions)
        except ValueError:
            pass

        for k in kinds:
            try:
                return Proposal.objects.get(dossier=dossier, kind__iexact=k)
            except Proposal.DoesNotExist:
                continue

        return None
53
54
55
56
57
58
59
60
61
62
63
64
65

    def import_row(self, row):
        dossier = self.get_dossier(row['title'])
        if dossier is None:
            logger.warn('Could not find dossier "%s"' % row['title'])
            return False

        proposal = self.get_proposal(dossier, row['part'])
        if proposal is None:
            logger.warn('Could not find proposal "%s" for dossier "%s"' % (
                row['part'], row['title']))
            return False

66
67
        weight = int(row['weight']) * int(row['ponderation'])

68
69
70
71
72
73
74
        try:
            recom = Recommendation.objects.get(proposal=proposal)
        except Recommendation.DoesNotExist:
            recom = Recommendation(
                proposal=proposal,
                recommendation=row['recommendation'],
                title=row['description'],
75
                weight=weight
76
77
78
            )
            recom.save()
            logger.info('Created recommendation with weight %s for %s: %s' % (
79
                weight,
80
81
82
83
84
85
86
87
88
89
90
91
                row['title'],
                row['part']
            ))

        return True


def main(stream=None):
    """
    Imports recommendations from an old memopol instance.

    Usage:
92
        cat recommendations.csv | memopol_import_recommendations
93
94

    The input CSV file should be generated by the following query:
95
96
        SELECT CONCAT(r.description, '|', r.weight, '|', r.recommendation, '|',
            r.part, '|', p.title, '|', p.ponderation)
97
98
99
100
101
102
103
104
105
106
107
108
109
        FROM votes_recommendation r
            LEFT JOIN votes_proposal p ON r.proposal_id = p.id
        WHERE p.institution = 'EU'

    """

    if not apps.ready:
        django.setup()

    importer = RecommendationImporter()
    rejected = []
    imported = 0

110
    reader = csv.DictReader(stream or sys.stdin, delimiter='|', fieldnames=[
111
112
113
114
        'description',
        'weight',
        'recommendation',
        'part',
115
116
        'title',
        'ponderation'
117
118
119
120
121
122
123
124
125
    ], quoting=csv.QUOTE_NONE)

    for row in reader:
        if not importer.import_row(row):
            rejected.append(row)
        else:
            imported = imported + 1

    logger.info('%d rows imported, %d rows rejected', imported, len(rejected))