-
Notifications
You must be signed in to change notification settings - Fork 8
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 #258 from datosgobar/255-refactor-validations
Refactor de validaciones
- Loading branch information
Showing
8 changed files
with
355 additions
and
357 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
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,22 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
from pydatajson import custom_exceptions | ||
|
||
from pydatajson.response_formatters.dict_formatter import DictFormatter | ||
from pydatajson.response_formatters.list_formatter import ListFormatter | ||
from pydatajson.response_formatters.tables_formatter import TablesFormatter | ||
|
||
|
||
def format_response(validation, export_path, response_format): | ||
formats = { | ||
'table': TablesFormatter(validation, export_path), | ||
'dict': DictFormatter(validation), | ||
'list': ListFormatter(validation), | ||
} | ||
try: | ||
return formats[response_format].format() | ||
except KeyError: | ||
msg = "No se reconoce el formato {}".format(response_format) | ||
raise custom_exceptions.FormatNameError(msg) |
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,12 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
from pydatajson.response_formatters.validation_response_formatter import \ | ||
ValidationResponseFormatter | ||
|
||
|
||
class DictFormatter(ValidationResponseFormatter): | ||
|
||
def format(self): | ||
return self.response |
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,58 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
from pydatajson.response_formatters.validation_response_formatter import \ | ||
ValidationResponseFormatter | ||
|
||
|
||
class ListFormatter(ValidationResponseFormatter): | ||
|
||
def format(self): | ||
rows_catalog = [] | ||
validation_result = { | ||
"catalog_title": self.response["error"]["catalog"]["title"], | ||
"catalog_status": self.response["error"]["catalog"]["status"], | ||
} | ||
for error in self.response["error"]["catalog"]["errors"]: | ||
catalog_result = dict(validation_result) | ||
catalog_result.update({ | ||
"catalog_error_message": error["message"], | ||
"catalog_error_location": ", ".join(error["path"]), | ||
}) | ||
rows_catalog.append(catalog_result) | ||
|
||
if len(self.response["error"]["catalog"]["errors"]) == 0: | ||
catalog_result = dict(validation_result) | ||
catalog_result.update({ | ||
"catalog_error_message": None, | ||
"catalog_error_location": None | ||
}) | ||
rows_catalog.append(catalog_result) | ||
|
||
# crea una lista de dicts para volcarse en una tabla (dataset) | ||
rows_dataset = [] | ||
for dataset in self.response["error"]["dataset"]: | ||
validation_result = { | ||
"dataset_title": dataset["title"], | ||
"dataset_identifier": dataset["identifier"], | ||
"dataset_list_index": dataset["list_index"], | ||
"dataset_status": dataset["status"] | ||
} | ||
for error in dataset["errors"]: | ||
dataset_result = dict(validation_result) | ||
dataset_result.update({ | ||
"dataset_error_message": error["message"], | ||
"dataset_error_location": error["path"][-1] | ||
}) | ||
rows_dataset.append(dataset_result) | ||
|
||
if len(dataset["errors"]) == 0: | ||
dataset_result = dict(validation_result) | ||
dataset_result.update({ | ||
"dataset_error_message": None, | ||
"dataset_error_location": None | ||
}) | ||
rows_dataset.append(dataset_result) | ||
|
||
return {"catalog": rows_catalog, "dataset": rows_dataset} |
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,53 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
|
||
from openpyxl.styles import Alignment, Font | ||
|
||
from pydatajson import writers | ||
from pydatajson.response_formatters.list_formatter import ListFormatter | ||
from pydatajson.response_formatters.validation_response_formatter import\ | ||
ValidationResponseFormatter | ||
|
||
|
||
class TablesFormatter(ValidationResponseFormatter): | ||
|
||
def __init__(self, response, export_path): | ||
super(TablesFormatter, self).__init__(response) | ||
self.export_path = export_path | ||
|
||
def format(self): | ||
validation_lists = ListFormatter(self.response).format() | ||
|
||
column_styles = { | ||
"catalog": { | ||
"catalog_status": {"width": 20}, | ||
"catalog_error_location": {"width": 40}, | ||
"catalog_error_message": {"width": 40}, | ||
"catalog_title": {"width": 20}, | ||
}, | ||
"dataset": { | ||
"dataset_error_location": {"width": 20}, | ||
"dataset_identifier": {"width": 40}, | ||
"dataset_status": {"width": 20}, | ||
"dataset_title": {"width": 40}, | ||
"dataset_list_index": {"width": 20}, | ||
"dataset_error_message": {"width": 40}, | ||
} | ||
} | ||
cell_styles = { | ||
"catalog": [ | ||
{"alignment": Alignment(vertical="center")}, | ||
{"row": 1, "font": Font(bold=True)}, | ||
], | ||
"dataset": [ | ||
{"alignment": Alignment(vertical="center")}, | ||
{"row": 1, "font": Font(bold=True)}, | ||
] | ||
} | ||
|
||
# crea tablas en un sólo excel o varios CSVs | ||
writers.write_tables( | ||
tables=validation_lists, path=self.export_path, | ||
column_styles=column_styles, cell_styles=cell_styles | ||
) |
14 changes: 14 additions & 0 deletions
14
pydatajson/response_formatters/validation_response_formatter.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,14 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
from __future__ import unicode_literals | ||
import abc | ||
|
||
|
||
class ValidationResponseFormatter(object): | ||
|
||
def __init__(self, response): | ||
self.response = response | ||
|
||
@abc.abstractmethod | ||
def format(self): | ||
raise NotImplementedError |
Oops, something went wrong.