-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better connect contract to tenders and examptions
- Loading branch information
Showing
3 changed files
with
74 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
datapackage_pipelines_budgetkey/pipelines/procurement/spending/resolve_manof_ref.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
import logging | ||
import json | ||
|
||
from datapackage_pipelines.wrapper import process | ||
|
||
from sqlalchemy import create_engine | ||
from sqlalchemy.exc import ProgrammingError, OperationalError | ||
|
||
TK = 'tender_key' | ||
db_table = 'procurement_tenders_processed' | ||
connection_string = os.environ['DPP_DB_ENGINE'] | ||
engine = create_engine(connection_string) | ||
|
||
key_fields = ('publication_id', 'tender_type', 'tender_id') | ||
to_select = ','.join(key_fields) | ||
|
||
all_tenders = set() | ||
for result in engine.execute(f'select {to_select} from {db_table}'): | ||
all_tenders.add(tuple(str(result[k]) for k in key_fields)) | ||
|
||
logging.info('Collected %d tenders and exemptions', len(all_tenders)) | ||
|
||
def modify_datapackage(dp, *_): | ||
dp['resources'][0]['schema']['fields'].extend([dict( | ||
name = TK, | ||
type = 'string' | ||
) for k in key_fields]) | ||
return dp | ||
|
||
def process_row(row, *_): | ||
mf = row['manof_ref'] | ||
if mf: | ||
mf = mf.strip() | ||
if mf: | ||
for t in all_tenders: | ||
if (t[0] and t[0] in mf) or (t[2] and t[2] in mf): | ||
row[TK] = json.dumps(list(t)) | ||
break | ||
if TK not in row: | ||
row[TK] = None | ||
logging.info('Failed to find reference for "%s"', mf) | ||
else: | ||
row[TK] = None | ||
return row | ||
|
||
if __name__ == '__main__': | ||
process(modify_datapackage=modify_datapackage, | ||
process_row=process_row) |