This repository has been archived by the owner on Feb 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #242 from datasciencebr/cuducos-rows
Use rows to serialize reimbursements
- Loading branch information
Showing
9 changed files
with
133 additions
and
95 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from datetime import date | ||
|
||
from rows import fields | ||
|
||
|
||
class IntegerField(fields.IntegerField): | ||
|
||
@classmethod | ||
def deserialize(cls, value, *args, **kwargs): | ||
try: # Rows cannot convert values such as '2011.0' to integer | ||
value = int(float(value)) | ||
except: | ||
pass | ||
return super(IntegerField, cls).deserialize(value) | ||
|
||
|
||
class DateAsStringField(fields.DateField): | ||
INPUT_FORMAT = '%Y-%m-%dT%H:%M:%S' | ||
OUTPUT_FORMAT = '%Y-%m-%d' | ||
|
||
@classmethod | ||
def deserialize(cls, value, *args, **kwargs): | ||
value = super(DateAsStringField, cls).deserialize(value) | ||
if value: # useful when serializing it to Celery | ||
return value.strftime(cls.OUTPUT_FORMAT) |
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
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 |
---|---|---|
@@ -1,62 +1,25 @@ | ||
from celery import shared_task | ||
|
||
from jarbas.core.models import Reimbursement | ||
from jarbas.core.management.commands import LoadCommand | ||
|
||
|
||
@shared_task | ||
def create_or_update_reimbursement(data): | ||
def create_or_update_reimbursement(row): | ||
""" | ||
:param data: (dict) reimbursement data (keys and data types must mach | ||
Reimbursement model) | ||
:param row: (dict) key/values maching Reimbursement model | ||
""" | ||
serialized = serialize_reimbursement(data) | ||
kwargs = dict(document_id=serialized['document_id'], defaults=serialized) | ||
data = transform_row(row) | ||
kwargs = dict(document_id=data['document_id'], defaults=data) | ||
Reimbursement.objects.update_or_create(**kwargs) | ||
|
||
|
||
def serialize_reimbursement(data): | ||
"""Read the dict generated by DictReader and fix types""" | ||
|
||
missing = ('probability', 'suspicions') | ||
for key in missing: | ||
data[key] = None | ||
|
||
def transform_row(row): | ||
"""Read the dict generated by rows and fix some keys""" | ||
rename = ( | ||
('subquota_number', 'subquota_id'), | ||
('reimbursement_value_total', 'total_reimbursement_value') | ||
) | ||
for old, new in rename: | ||
data[new] = data.pop(old) | ||
|
||
integers = ( | ||
'applicant_id', | ||
'batch_number', | ||
'congressperson_document', | ||
'congressperson_id', | ||
'document_id', | ||
'document_type', | ||
'installment', | ||
'month', | ||
'subquota_group_id', | ||
'subquota_id', | ||
'term', | ||
'term_id', | ||
'year' | ||
) | ||
for key in integers: | ||
data[key] = LoadCommand.to_number(data[key], int) | ||
|
||
floats = ( | ||
'document_value', | ||
'remark_value', | ||
'total_net_value', | ||
'total_reimbursement_value' | ||
) | ||
for key in floats: | ||
data[key] = LoadCommand.to_number(data[key]) | ||
|
||
issue_date = LoadCommand.to_date(data['issue_date']) | ||
data['issue_date'] = issue_date.strftime('%Y-%m-%d') | ||
row[new] = row.pop(old) | ||
|
||
return data | ||
return row |
Binary file not shown.
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
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
-r requirements.txt | ||
django-test-without-migrations==0.6 | ||
ipdb==0.10.3 | ||
mixer==5.6.6 |
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 |
---|---|---|
|
@@ -19,3 +19,4 @@ python-memcached==1.58 | |
python-twitter==3.3 | ||
reprint==0.3.0 # pyup: ignore | ||
requests==2.18.4 | ||
rows==0.3.1 |