This repository has been archived by the owner on Jul 28, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mapper.py
209 lines (148 loc) · 7.22 KB
/
mapper.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# -*- coding: utf-8 -*-
import json
import csv
import time
import random
import re
import urllib2
import sys
from datetime import datetime
from datetime import date
from app import app
import pytz
from slugify import slugify
import hashlib
def field_mapper_pol_mtl(row, source, my_release):
eastern = pytz.timezone('US/Eastern')
contract_date = eastern.localize(datetime.strptime(row[7], "%Y-%m-%d"))
if (row[2] in app.config["SERVICE_AGGREGATOR"]):
buyer = app.config["SERVICE_AGGREGATOR"][row[2]]
else:
buyer = row[2]
# TODO: USE NO DOSSIER OR NO DECISION?
key = hashlib.sha1(bytes(row[0] + buyer + row[4] + row[7] + row[8])).hexdigest()
my_release["ocid"] = app.config["OCID_PREFIX"] + key
my_release["id"] = key
#TODO: IL FAUT SUREMENT AJOUTER UN TIMEZONE
my_release["date"] = contract_date.isoformat()
my_release["buyer"]["name"] = buyer
my_release["buyer"]["identifier"]["id"] = slugify(buyer, to_lower=True)
my_release["buyer"]["subOrganisationOf"]["name"] = row[3]
#TODO: FAIT MAPPING SERVICE => ACTIVITE
my_release["subject"] = ["Autres"]
if (buyer in app.config["SERVICE_TO_ACTIVITY"]):
my_release["subject"] = app.config["SERVICE_TO_ACTIVITY"][buyer]
#TODO : Pass the procuring entity as a paramter of the mapper?
my_release["tender"]["procuringEntity"]["name"] = source.name
my_release["tender"]["procuringEntity"]["identifier"]["id"] = slugify(source.name, to_lower=True)
my_release["awards"][0]["id"] = row[4]
my_release["awards"][0]["date"] = contract_date.isoformat()
my_release["awards"][0]["repartition"] = row[1]
my_release["awards"][0]["value"]["amount"] = float(row[8].replace(',','.'))
my_release["awards"][0]["suppliers"][0]["name"] = row[0]
my_release["awards"][0]["suppliers"][0]["identifier"]["id"] = slugify(row[0], to_lower=True)
my_release["awards"][0]["items"][0]["id"] = row[6]
#TODO : Pass the procuring entity as a paramter of the mapper?
my_release["awards"][0]["items"][0]["description"] = row[5]
return my_release
def field_mapper_fonc_mtl(row, source, my_release):
eastern = pytz.timezone('US/Eastern')
contract_date = eastern.localize(datetime.strptime(row[2], "%Y-%m-%d"))
if (row[5] in app.config["SERVICE_AGGREGATOR"]):
buyer = app.config["SERVICE_AGGREGATOR"][row[5]]
else:
buyer = row[5]
# TODO: USE NO DOSSIER OR NO DECISION?
key = hashlib.sha1(bytes(row[0] + row[1] + row[2] + row[4] + buyer + row[7])).hexdigest()
my_release["ocid"] = app.config["OCID_PREFIX"] + key
my_release["id"] = key
#TODO: IL FAUT SUREMENT AJOUTER UN TIMEZONE
my_release["date"] = contract_date.isoformat()
my_release["buyer"]["name"] = buyer
my_release["buyer"]["identifier"]["id"] = slugify(buyer, to_lower=True)
#TODO: FAIT MAPPING SERVICE => ACTIVITE
my_release["subject"] = ["Autres"]
if (buyer in app.config["SERVICE_TO_ACTIVITY"]):
my_release["subject"] = app.config["SERVICE_TO_ACTIVITY"][buyer]
#TODO : Pass the procuring entity as a paramter of the mapper?
my_release["tender"]["procuringEntity"]["name"] = source.name
my_release["tender"]["procuringEntity"]["identifier"]["id"] = slugify(source.name, to_lower=True)
my_release["awards"][0]["id"] = row[1]
my_release["awards"][0]["date"] = contract_date.isoformat()
my_release["awards"][0]["value"]["amount"] = float(row[7].replace(',','.'))
my_release["awards"][0]["suppliers"][0]["name"] = row[0]
my_release["awards"][0]["suppliers"][0]["identifier"]["id"] = slugify(row[0], to_lower=True)
my_release["awards"][0]["items"][0]["id"] = ""
#TODO : Pass the procuring entity as a paramter of the mapper?
description = row[4] + "."
if len(row[6]) > 0:
description += " " + row[6] + ". "
if len(row[3]) > 0:
description += "APPROUVÉ PAR : " + row[3] + ". "
my_release["awards"][0]["items"][0]["description"] = description
del description
return my_release
def field_mapper_subvention_mtl(row, source, my_release):
eastern = pytz.timezone('US/Eastern')
contract_date = eastern.localize(datetime.strptime(row[7], "%Y-%m-%d"))
# TODO: USE NO DOSSIER OR NO DECISION?
if (row[2] in app.config["SERVICE_AGGREGATOR"]):
buyer = app.config["SERVICE_AGGREGATOR"][row[2]]
else:
buyer = row[2]
key = hashlib.sha1(bytes(row[0] + buyer + row[4] + row[7] + row[8])).hexdigest()
my_release["ocid"] = app.config["OCID_PREFIX"] + key
my_release["id"] = key
my_release["tender"]["procurementMethodRationale"] = source.type
#TODO: IL FAUT SUREMENT AJOUTER UN TIMEZONE
my_release["date"] = contract_date.isoformat()
my_release["buyer"]["name"] = buyer
my_release["buyer"]["identifier"]["id"] = slugify(buyer, to_lower=True)
my_release["buyer"]["subOrganisationOf"]["name"] = row[3]
#TODO: FAIT MAPPING SERVICE => ACTIVITE
my_release["subject"] = ["Autres"]
if (buyer in app.config["SERVICE_TO_ACTIVITY"]):
my_release["subject"] = app.config["SERVICE_TO_ACTIVITY"][buyer]
#TODO : Pass the procuring entity as a paramter of the mapper?
my_release["tender"]["procuringEntity"]["name"] = source.name
my_release["tender"]["procuringEntity"]["identifier"]["id"] = slugify(source.name, to_lower=True)
my_release["awards"][0]["id"] = row[4]
my_release["awards"][0]["date"] = contract_date.isoformat()
my_release["awards"][0]["repartition"] = row[1]
my_release["awards"][0]["value"]["amount"] = float(row[11].replace(',','.'))
my_release["awards"][0]["suppliers"][0]["name"] = row[0]
my_release["awards"][0]["suppliers"][0]["identifier"]["id"] = slugify(row[0], to_lower=True)
my_release["awards"][0]["items"][0]["id"] = row[6]
#TODO : Pass the procuring entity as a paramter of the mapper?
my_release["awards"][0]["items"][0]["description"] = row[5]
return my_release
class Mapper():
def __init__(self, source, options={}):
if re.match("^http", source.url) == None:
self.cr = csv.reader(open(source.url))
else:
self.cr = csv.reader(urllib2.urlopen(source.url))
self.template = json.load(open('templates/release.json'))
self.mapper_type = source.mapper
self.mapper = getattr(sys.modules[__name__], self.mapper_type)
self.source = source
self.output = {}
self.csv_skip = 1
if hasattr(source, "skip_lines") and source.skip_lines != None:
self.csv_skip = source.skip_lines
def to_ocds(self):
i = 0
#errors = []
for row in self.cr:
if i >= self.csv_skip:
try:
release = self.mapper(row, self.source, self.template)
if int(release["awards"][0]["date"][0:4]) < 2011 or int(release["awards"][0]["date"][0:4]) > date.today().year:
yield (None, ["INVALID_YEAR", i ])
else:
yield (release, None)
except IndexError as e:
yield (None, ["MISSING_FIELD", i ])
except (ValueError, IndexError) as e:
yield (None, ["INVALID_FIELD", i ])
i = i+1