Skip to content

Commit

Permalink
Revert "agregado de parametros de origen y destino de timezones, corr…
Browse files Browse the repository at this point in the history
…eciones en los tests para parametros opcionales"

This reverts commit 51fda26
  • Loading branch information
Alexis Wolfsdorf committed Sep 12, 2019
1 parent 51fda26 commit b17d9ca
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 129 deletions.
29 changes: 10 additions & 19 deletions pydatajson/ckan_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pytz
from dateutil import parser

from pydatajson.constants import DEFAULT_TIMEZONE
from pydatajson import constants
from .helpers import title_to_name
from . import custom_exceptions as ce

Expand All @@ -25,8 +25,7 @@ def append_attribute_to_extra(package, dataset, attribute, serialize=False):


def map_dataset_to_package(catalog, dataset, owner_org, catalog_id=None,
demote_superThemes=True, demote_themes=True,
origin_tz=DEFAULT_TIMEZONE, dst_tz=DEFAULT_TIMEZONE):
demote_superThemes=True, demote_themes=True):
package = dict()
package['extras'] = []

Expand All @@ -49,7 +48,7 @@ def map_dataset_to_package(catalog, dataset, owner_org, catalog_id=None,

distributions = dataset['distribution']
package['resources'] = map_distributions_to_resources(
distributions, catalog_id, origin_tz=origin_tz, dst_tz=dst_tz)
distributions, catalog_id)

super_themes = dataset['superTheme']
append_attribute_to_extra(package, dataset, 'superTheme', serialize=True)
Expand Down Expand Up @@ -118,22 +117,18 @@ def _get_theme_label(catalog, theme):
return label


def convert_iso_string_to_default_timezone(date_string, origin_tz=DEFAULT_TIMEZONE, dst_tz=DEFAULT_TIMEZONE):
def convert_iso_string_to_default_timezone(date_string):
date_time = parser.parse(date_string)

dest_timezone = pytz.timezone(dst_tz)
if date_time.tzinfo is not None:
date_time = date_time.astimezone(dest_timezone)
else:
origin_timezone = pytz.timezone(origin_tz)
date_time = date_time.replace(tzinfo=origin_timezone)
date_time = date_time.astimezone(dest_timezone)
timezone = pytz.timezone(constants.DEFAULT_TIMEZONE)
date_time = date_time.astimezone(timezone)
date_time = date_time.replace(tzinfo=None)

date_time = date_time.replace(tzinfo=None)
return date_time.isoformat()


def map_distributions_to_resources(distributions, catalog_id=None, origin_tz=DEFAULT_TIMEZONE, dst_tz=DEFAULT_TIMEZONE):
def map_distributions_to_resources(distributions, catalog_id=None):
resources = []
for distribution in distributions:
resource = dict()
Expand All @@ -143,18 +138,14 @@ def map_distributions_to_resources(distributions, catalog_id=None, origin_tz=DEF
resource['name'] = distribution['title']
resource['url'] = distribution['downloadURL']
resource['created'] = \
convert_iso_string_to_default_timezone(distribution['issued'],
origin_tz=origin_tz,
dst_tz=dst_tz)
convert_iso_string_to_default_timezone(distribution['issued'])
# Recomendados y opcionales
resource['description'] = distribution.get('description')
resource['format'] = distribution.get('format')
last_modified = distribution.get('modified')
if last_modified:
resource['last_modified'] = \
convert_iso_string_to_default_timezone(last_modified,
origin_tz=origin_tz,
dst_tz=dst_tz)
convert_iso_string_to_default_timezone(last_modified)
resource['mimetype'] = distribution.get('mediaType')
resource['size'] = distribution.get('byteSize')
resource['accessURL'] = distribution.get('accessURL')
Expand Down
99 changes: 20 additions & 79 deletions pydatajson/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import logging
from ckanapi.errors import NotFound, CKANAPIError

from pydatajson.constants import REQUESTS_TIMEOUT, DEFAULT_TIMEZONE
from pydatajson.constants import REQUESTS_TIMEOUT
from pydatajson.custom_exceptions import NumericDistributionIdentifierError
from .ckan_utils import map_dataset_to_package, map_theme_to_group
from pydatajson.custom_remote_ckan import CustomRemoteCKAN as RemoteCKAN
Expand All @@ -21,8 +21,7 @@
def push_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
portal_url, apikey, catalog_id=None,
demote_superThemes=True, demote_themes=True,
download_strategy=None, generate_new_access_url=None,
origin_tz=DEFAULT_TIMEZONE, dst_tz=DEFAULT_TIMEZONE):
download_strategy=None, generate_new_access_url=None):
"""Escribe la metadata de un dataset en el portal pasado por parámetro.
Args:
Expand All @@ -33,7 +32,7 @@ def push_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
portal_url (str): La URL del portal CKAN de destino.
apikey (str): La apikey de un usuario con los permisos que le
permitan crear o actualizar el dataset.
catalog_id (str or None): El prefijo con el que va a preceder el id del
catalog_id (str): El prefijo con el que va a preceder el id del
dataset en catálogo destino.
demote_superThemes(bool): Si está en true, los ids de los super
themes del dataset, se propagan como grupo.
Expand All @@ -47,11 +46,6 @@ def push_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
distribuciones cuyo accessURL se regenerar en el portal de
destino. Para el resto, el portal debe mantiene el valor pasado
en el DataJson.
origin_tz(str): Timezone de origen, un string (EJ: Africa/Bamako) el
cual identifica el timezone del emisor del DataJson.
dst_tz(str): Timezone de destino, un string
(EJ: Antarctica/Palmer) el cual identifica el timezone del
receptor del DataJson, comunmente el timezone del servidor.
Returns:
str: El id del dataset en el catálogo de destino.
"""
Expand All @@ -60,14 +54,8 @@ def push_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
verify_ssl=catalog.verify_ssl,
requests_timeout=catalog.requests_timeout)

package = map_dataset_to_package(catalog,
dataset,
owner_org,
catalog_id=catalog_id,
demote_superThemes=demote_superThemes,
demote_themes=demote_themes,
origin_tz=origin_tz,
dst_tz=dst_tz)
package = map_dataset_to_package(catalog, dataset, owner_org, catalog_id,
demote_superThemes, demote_themes)

# Get license id
if dataset.get('license'):
Expand Down Expand Up @@ -269,8 +257,7 @@ def push_theme_to_ckan(catalog, portal_url, apikey,

def restore_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
portal_url, apikey, download_strategy=None,
generate_new_access_url=None,
origin_tz=DEFAULT_TIMEZONE, dst_tz=DEFAULT_TIMEZONE):
generate_new_access_url=None):
"""Restaura la metadata de un dataset en el portal pasado por parámetro.
Args:
Expand All @@ -289,11 +276,6 @@ def restore_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
distribuciones cuyo accessURL se regenerar en el portal de
destino. Para el resto, el portal debe mantiene el valor
pasado en el DataJson.
origin_tz(str): Timezone de origen, un string (EJ: Africa/Bamako) el
cual identifica el timezone del emisor del DataJson.
dst_tz(str): Timezone de destino, un string
(EJ: Antarctica/Palmer) el cual identifica el timezone del
receptor del DataJson, comunmente el timezone del servidor.
Returns:
str: El id del dataset restaurado.
"""
Expand All @@ -312,17 +294,15 @@ def restore_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
'este es numerico. Por favor, cambielo e intente de '
'nuevo'.format(distribution["identifier"]))

return push_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
portal_url, apikey, catalog_id=None, demote_superThemes=False,
demote_themes=False, download_strategy=download_strategy,
generate_new_access_url=generate_new_access_url,
origin_tz=origin_tz, dst_tz=dst_tz)
return push_dataset_to_ckan(catalog, owner_org,
dataset_origin_identifier, portal_url,
apikey, None, False, False, download_strategy,
generate_new_access_url)


def harvest_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
portal_url, apikey, catalog_id,
download_strategy=None,
origin_tz=DEFAULT_TIMEZONE, dst_tz=DEFAULT_TIMEZONE):
download_strategy=None):
"""Federa la metadata de un dataset en el portal pasado por parámetro.
Args:
Expand All @@ -338,25 +318,18 @@ def harvest_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
bool. Sobre las distribuciones que evalúa True, descarga el
recurso en el downloadURL y lo sube al portal de destino.
Por default no sube ninguna distribución.
origin_tz(str): Timezone de origen, un string (EJ: Africa/Bamako) el
cual identifica el timezone del emisor del DataJson.
dst_tz(str): Timezone de destino, un string
(EJ: Antarctica/Palmer) el cual identifica el timezone del
receptor del DataJson, comunmente el timezone del servidor.
Returns:
str: El id del dataset restaurado.
"""

return push_dataset_to_ckan(catalog, owner_org, dataset_origin_identifier,
portal_url, apikey, catalog_id=catalog_id,
download_strategy=download_strategy,
origin_tz=origin_tz, dst_tz=dst_tz)
download_strategy=download_strategy)


def harvest_catalog_to_ckan(catalog, portal_url, apikey, catalog_id,
dataset_list=None, owner_org=None,
download_strategy=None,
origin_tz=DEFAULT_TIMEZONE, dst_tz=DEFAULT_TIMEZONE):
download_strategy=None):
"""Federa los datasets de un catálogo al portal pasado por parámetro.
Args:
Expand All @@ -374,11 +347,6 @@ def harvest_catalog_to_ckan(catalog, portal_url, apikey, catalog_id,
bool. Sobre las distribuciones que evalúa True, descarga el
recurso en el downloadURL y lo sube al portal de destino.
Por default no sube ninguna distribución.
origin_tz(str): Timezone de origen, un string (EJ: Africa/Bamako) el
cual identifica el timezone del emisor del DataJson.
dst_tz(str): Timezone de destino, un string
(EJ: Antarctica/Palmer) el cual identifica el timezone del
receptor del DataJson, comunmente el timezone del servidor.
Returns:
str: El id del dataset en el catálogo de destino.
"""
Expand All @@ -397,9 +365,7 @@ def harvest_catalog_to_ckan(catalog, portal_url, apikey, catalog_id,
harvested_id = harvest_dataset_to_ckan(catalog, owner_org,
dataset_id, portal_url,
apikey, catalog_id,
download_strategy,
origin_tz=origin_tz,
dst_tz=dst_tz)
download_strategy)
harvested.append(harvested_id)
except Exception as e:
msg = "Error federando catalogo: %s, dataset: %s al portal: %s\n"\
Expand Down Expand Up @@ -586,9 +552,7 @@ def remove_organizations_from_ckan(portal_url, apikey, organization_list):

def restore_organizations_to_ckan(catalog, organizations, portal_url, apikey,
download_strategy=None,
generate_new_access_url=None,
origin_tz=DEFAULT_TIMEZONE,
dst_tz=DEFAULT_TIMEZONE):
generate_new_access_url=None):
"""Restaura los datasets indicados para c/organización de un catálogo al
portal pasado. Si hay temas presentes en el DataJson que no están en el
portal de CKAN, los genera. Las organizaciones ya deben estar creadas.
Expand All @@ -608,11 +572,6 @@ def restore_organizations_to_ckan(catalog, organizations, portal_url, apikey,
distribuciones cuyo accessURL se regenerar en el portal de
destino. Para el resto, el portal debe mantiene el valor
pasado en el DataJson.
origin_tz(str): Timezone de origen, un string (EJ: Africa/Bamako) el
cual identifica el timezone del emisor del DataJson.
dst_tz(str): Timezone de destino, un string
(EJ: Antarctica/Palmer) el cual identifica el timezone del
receptor del DataJson, comunmente el timezone del servidor.
Returns:
list(str): La lista de ids de datasets subidos.
"""
Expand All @@ -622,9 +581,7 @@ def restore_organizations_to_ckan(catalog, organizations, portal_url, apikey,
catalog, org,
portal_url,
apikey,
dataset_list=organizations[org],
origin_tz=origin_tz,
dst_tz=dst_tz
dataset_list=organizations[org]
)
pushed_datasets[org] = org_pushed_datasets

Expand All @@ -633,9 +590,7 @@ def restore_organizations_to_ckan(catalog, organizations, portal_url, apikey,

def restore_organization_to_ckan(catalog, owner_org, portal_url, apikey,
dataset_list=None, download_strategy=None,
generate_new_access_url=None,
origin_tz=DEFAULT_TIMEZONE,
dst_tz=DEFAULT_TIMEZONE):
generate_new_access_url=None):
"""Restaura los datasets de la organización de un catálogo al portal pasado
por parámetro. Si hay temas presentes en el DataJson que no están en el
portal de CKAN, los genera.
Expand All @@ -656,11 +611,6 @@ def restore_organization_to_ckan(catalog, owner_org, portal_url, apikey,
distribuciones cuyo accessURL se regenerar en el portal de
destino. Para el resto, el portal debe mantiene el valor
pasado en el DataJson.
origin_tz(str): Timezone de origen, un string (EJ: Africa/Bamako) el
cual identifica el timezone del emisor del DataJson.
dst_tz(str): Timezone de destino, un string
(EJ: Antarctica/Palmer) el cual identifica el timezone del
receptor del DataJson, comunmente el timezone del servidor.
Returns:
list(str): La lista de ids de datasets subidos.
"""
Expand All @@ -678,9 +628,7 @@ def restore_organization_to_ckan(catalog, owner_org, portal_url, apikey,
restored_id = restore_dataset_to_ckan(catalog, owner_org,
dataset_id, portal_url,
apikey, download_strategy,
generate_new_access_url,
origin_tz=origin_tz,
dst_tz=dst_tz)
generate_new_access_url)
restored.append(restored_id)
except (CKANAPIError, KeyError, AttributeError,
NumericDistributionIdentifierError) as e:
Expand All @@ -691,8 +639,7 @@ def restore_organization_to_ckan(catalog, owner_org, portal_url, apikey,

def restore_catalog_to_ckan(catalog, origin_portal_url, destination_portal_url,
apikey, download_strategy=None,
generate_new_access_url=None,
origin_tz=DEFAULT_TIMEZONE, dst_tz=DEFAULT_TIMEZONE):
generate_new_access_url=None):
"""Restaura los datasets de un catálogo original al portal pasado
por parámetro. Si hay temas presentes en el DataJson que no están en
el portal de CKAN, los genera.
Expand All @@ -713,11 +660,6 @@ def restore_catalog_to_ckan(catalog, origin_portal_url, destination_portal_url,
distribuciones cuyo accessURL se regenerar en el portal de
destino. Para el resto, el portal debe mantiene el valor
pasado en el DataJson.
origin_tz(str): Timezone de origen, un string (EJ: Africa/Bamako) el
cual identifica el timezone del emisor del DataJson.
dst_tz(str): Timezone de destino, un string
(EJ: Antarctica/Palmer) el cual identifica el timezone del
receptor del DataJson, comunmente el timezone del servidor.
Returns:
dict: Diccionario con key organización y value la lista de ids
de datasets subidos a esa organización
Expand Down Expand Up @@ -747,8 +689,7 @@ def restore_catalog_to_ckan(catalog, origin_portal_url, destination_portal_url,
pushed_datasets = restore_organization_to_ckan(
catalog, org, destination_portal_url, apikey,
dataset_list=datasets, download_strategy=download_strategy,
generate_new_access_url=generate_new_access_url,
origin_tz=origin_tz, dst_tz=dst_tz
generate_new_access_url=generate_new_access_url
)
res[org] = pushed_datasets

Expand Down

0 comments on commit b17d9ca

Please sign in to comment.