Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Muevo funciones de normalizacion de indicadores a archivo propio, y l…
…as migro
- Loading branch information
1 parent
6eed0c7
commit ae4ebba
Showing
4 changed files
with
276 additions
and
213 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
monitoreo/apps/dashboard/migrations/0056_normalize_indicator_values_import.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,22 @@ | ||
from __future__ import unicode_literals | ||
|
||
from django.db import migrations | ||
|
||
from monitoreo.apps.dashboard.normalization_utils import normalize_node_indicator_values, \ | ||
normalize_network_indicator_values, normalize_federator_indicator_values | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('dashboard', '0055_auto_20191030_1544'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(normalize_node_indicator_values, | ||
migrations.RunPython.noop), | ||
migrations.RunPython(normalize_network_indicator_values, | ||
migrations.RunPython.noop), | ||
migrations.RunPython(normalize_federator_indicator_values, | ||
migrations.RunPython.noop), | ||
] |
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,41 @@ | ||
from __future__ import unicode_literals | ||
|
||
import json | ||
import logging | ||
|
||
from pydatajson.helpers import fields_to_uppercase | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
def normalize_values(apps, model): | ||
queryset = model.objects.filter(indicador_tipo__nombre__in=[ | ||
'distribuciones_formatos_cant', 'datasets_frecuencia_cant']) | ||
|
||
for indicator in queryset: | ||
try: | ||
value = json.loads(indicator.indicador_valor) | ||
if isinstance(value, dict): | ||
normalized_value = fields_to_uppercase(value) | ||
if normalized_value != value: | ||
# No quiero hacer un save si los valores no cambian | ||
indicator.indicador_valor = json.dumps(normalized_value) | ||
indicator.save() | ||
except json.JSONDecodeError: | ||
msg = f'error parseando el indicador:{indicator.pk}' | ||
LOGGER.warning(msg) | ||
|
||
|
||
def normalize_node_indicator_values(apps, _schema_editor): | ||
indicador = apps.get_model('dashboard', 'Indicador') | ||
normalize_values(apps, indicador) | ||
|
||
|
||
def normalize_network_indicator_values(apps, _schema_editor): | ||
indicador_red = apps.get_model('dashboard', 'IndicadorRed') | ||
normalize_values(apps, indicador_red) | ||
|
||
|
||
def normalize_federator_indicator_values(apps, _schema_editor): | ||
indicador_federador = apps.get_model('dashboard', 'IndicadorFederador') | ||
normalize_values(apps, indicador_federador) |
Oops, something went wrong.