import_recommendations.py 3.91 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# coding: utf-8

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

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

logger = logging.getLogger(__name__)

dossier_mappings = {
    "Resolution on Anti-Counterfeiting Trade Agreement (ACTA)":
        "Resolution on the Anti-Counterfeiting Trade Agreement (ACTA)",
    "Rapport Bono on cultural industries in Europe":
        "Cultural industries in Europe",
    "Rapport Gallo on enforcement of intellectual property rights in the internal market":
        "Enforcement of intellectual property rights in the internal market",
    "Rapport Lambrinidis on strengthening security and fundamental freedoms on the Internet":
        "Strengthening security and fundamental freedoms on the Internet",
    "Criminal measures aimed at ensuring the enforcement of intellectual property rights (IPRED 2), 1st reading":
        "Criminal measures aimed at ensuring the enforcement of intellectual property rights"
}


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)
43
                self.dossier_cache[title] = dossier
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
            except Dossier.DoesNotExist:
                dossier = None

        return dossier

    def get_proposal(self, dossier, kind):
        try:
            return Proposal.objects.get(dossier=dossier, kind__iexact=kind)
        except Proposal.DoesNotExist:
            return None

    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

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

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

        return True


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

    Usage:
93
        cat recommendations.csv | memopol_import_recommendations
94
95

    The input CSV file should be generated by the following query:
96
97
        SELECT CONCAT(r.description, '|', r.weight, '|', r.recommendation, '|',
            r.part, '|', p.title, '|', p.ponderation)
98
99
100
101
102
103
104
105
106
107
108
109
110
        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

111
    reader = csv.DictReader(stream or sys.stdin, delimiter='|', fieldnames=[
112
113
114
115
        'description',
        'weight',
        'recommendation',
        'part',
116
117
        'title',
        'ponderation'
118
119
120
121
122
123
124
125
126
127
    ], 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))