From 168f8a4e03c61081d55a9ff07546b852ef23c16e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Arroyo=20Torrens?= Date: Fri, 17 Jan 2020 11:57:36 +0100 Subject: [PATCH 1/5] Add Map.delete_publication static method. Remove delete_publication method --- cartoframes/viz/kuviz.py | 24 ++++++++++++++++-------- cartoframes/viz/map.py | 21 ++++++++++++++++----- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/cartoframes/viz/kuviz.py b/cartoframes/viz/kuviz.py index 8ed0cf0cd..65426c3c6 100644 --- a/cartoframes/viz/kuviz.py +++ b/cartoframes/viz/kuviz.py @@ -32,6 +32,22 @@ def all(credentials=None): kuvizs = kmanager.all() return [kuviz_to_dict(kuviz) for kuviz in kuvizs] + @staticmethod + def delete(name, credentials=None): + auth_client = _create_auth_client(credentials) + kmanager = _get_kuviz_manager(auth_client) + kuvizs = kmanager.all() + kuviz = next((kuviz for kuviz in kuvizs if kuviz.name == name), None) + + if kuviz is None: + raise PublishError('Publication "{}" not found.'.format(name)) + + try: + kuviz.delete() + log.info('Success! Publication "{0}" deleted'.format(name)) + except Exception as e: + manage_kuviz_exception(e, name) + def get_layers(self): return self._layers @@ -62,14 +78,6 @@ def update(self, data, name, password, if_exists='fail'): return kuviz_to_dict(self.kuviz) - def delete(self): - if self.kuviz: - self.kuviz.delete() - log.warning("Publication '{n}' ({id}) deleted".format(n=self.kuviz.name, id=self.kuviz.id)) - self.kuviz = None - return True - return False - def _sync_layers(self, layers, table_name=None): for idx, layer in enumerate(layers): if layer.source.is_local(): diff --git a/cartoframes/viz/map.py b/cartoframes/viz/map.py index cf1f0d776..908d47ae0 100644 --- a/cartoframes/viz/map.py +++ b/cartoframes/viz/map.py @@ -7,7 +7,7 @@ from .html import HTMLMap from .basemaps import Basemaps from .kuviz import KuvizPublisher -from ..utils.utils import get_center +from ..utils.utils import get_center, get_credentials from ..utils.metrics import send_metrics WORLD_BOUNDS = [[-180, -90], [180, 90]] @@ -215,10 +215,6 @@ def publish(self, name, password, if_exists='fail', table_name=None, credentials html = self._get_publication_html(name) return self._publisher.publish(html, name, password, if_exists) - def delete_publication(self): - """Delete the published map visualization.""" - return self._publisher.delete() - def update_publication(self, name, password, if_exists='fail'): """Update the published map visualization. @@ -248,6 +244,21 @@ def all_publications(credentials=None): """ return KuvizPublisher.all(credentials) + @staticmethod + def delete_publication(name, credentials=None): + """Delete a map visualization published by id. + + Args: + name (str): name of the publication to be deleted. + credentials (:py:class:`Credentials `, optional): + A Credentials instance. If not provided, the credentials will be automatically + obtained from the default credentials if available. + + """ + _credentials = get_credentials(credentials) + + return KuvizPublisher.delete(name, _credentials) + def _get_publication_html(self, name): html_map = HTMLMap('templates/viz/main.html.j2') html_map.set_content( From 198103fba12cf8e7f8c67daa58ceaa88635af9aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Arroyo=20Torrens?= Date: Fri, 17 Jan 2020 11:59:05 +0100 Subject: [PATCH 2/5] Remove unused tests --- tests/unit/viz/test_kuviz.py | 19 ------------------- tests/unit/viz/test_map.py | 11 ----------- 2 files changed, 30 deletions(-) diff --git a/tests/unit/viz/test_kuviz.py b/tests/unit/viz/test_kuviz.py index f36a2a120..422961df3 100644 --- a/tests/unit/viz/test_kuviz.py +++ b/tests/unit/viz/test_kuviz.py @@ -184,22 +184,3 @@ def test_kuviz_publisher_update(self, mocker): assert kuviz_publisher.kuviz == kuviz assert result_update == kuviz_to_dict(kuviz) assert result_publish != kuviz_to_dict(kuviz) - - def test_kuviz_publisher_delete(self, mocker): - setup_mocks(mocker, self.credentials) - - kuviz = CartoKuvizMock('fake_kuviz') - mocker.patch('cartoframes.viz.kuviz._create_kuviz', return_value=kuviz) - - vmap = Map(Layer('fake_table', credentials=self.credentials)) - - html = 'fake_html' - kuviz_name = 'fake_name' - - kuviz_publisher = KuvizPublisher(None) - kuviz_publisher.set_layers(vmap.layers, kuviz_name, 'fake_table_name') - kuviz_publisher.publish(html, kuviz_name, None) - - kuviz_publisher.delete() - - assert kuviz_publisher.kuviz is None diff --git a/tests/unit/viz/test_map.py b/tests/unit/viz/test_map.py index 3fb684707..22018e596 100644 --- a/tests/unit/viz/test_map.py +++ b/tests/unit/viz/test_map.py @@ -284,17 +284,6 @@ def test_map_publish_with_password(self, mocker): kuviz_dict = map.publish(name, '1234', credentials=self.credentials) self.assert_kuviz_dict(kuviz_dict, name, 'password') - def test_map_publish_deletion(self, mocker): - setup_mocks(mocker) - - map = Map(Layer(Source('fake_table', credentials=self.credentials))) - - name = 'cf_publish' - map.publish(name, None, credentials=self.credentials) - response = map.delete_publication() - - assert response is True - def test_map_publish_update_name(self, mocker): setup_mocks(mocker) From 7463b77d8ce9d89b802fae9c9c3604c8113ec9ad Mon Sep 17 00:00:00 2001 From: Josema Camacho Date: Thu, 9 Jul 2020 17:28:11 +0200 Subject: [PATCH 3/5] all_publications and delete_publication to the viz module --- cartoframes/viz/__init__.py | 7 +++- cartoframes/viz/kuviz.py | 66 ++++++++++++++++++++++++------------- cartoframes/viz/map.py | 29 ---------------- 3 files changed, 49 insertions(+), 53 deletions(-) diff --git a/cartoframes/viz/__init__.py b/cartoframes/viz/__init__.py index 83bad4b73..89d60e84d 100644 --- a/cartoframes/viz/__init__.py +++ b/cartoframes/viz/__init__.py @@ -38,6 +38,8 @@ from .popups import popup_element from .popups import default_popup_element +from .kuviz import all_publications +from .kuviz import delete_publication __all__ = [ 'Map', @@ -77,5 +79,8 @@ 'default_widget', 'popup_element', - 'default_popup_element' + 'default_popup_element', + + 'all_publications', + 'delete_publication' ] diff --git a/cartoframes/viz/kuviz.py b/cartoframes/viz/kuviz.py index 3bddf1d06..f23d2d8c9 100644 --- a/cartoframes/viz/kuviz.py +++ b/cartoframes/viz/kuviz.py @@ -6,6 +6,7 @@ from ..data.clients.auth_api_client import AuthAPIClient from ..exceptions import PublishError from ..utils.logger import log +from ..utils.utils import get_credentials filterwarnings('ignore', category=FutureWarning, module='carto') @@ -19,29 +20,6 @@ def __init__(self, credentials=None): self._auth_client = _create_auth_client(credentials) self._auth_api_client = _create_auth_api_client(credentials) - @staticmethod - def all(credentials=None): - auth_client = _create_auth_client(credentials) - kmanager = _get_kuviz_manager(auth_client) - kuvizs = kmanager.all() - return [kuviz_to_dict(kuviz) for kuviz in kuvizs] - - @staticmethod - def delete(name, credentials=None): - auth_client = _create_auth_client(credentials) - kmanager = _get_kuviz_manager(auth_client) - kuvizs = kmanager.all() - kuviz = next((kuviz for kuviz in kuvizs if kuviz.name == name), None) - - if kuviz is None: - raise PublishError('Publication "{}" not found.'.format(name)) - - try: - kuviz.delete() - log.info('Success! Publication "{0}" deleted'.format(name)) - except Exception as e: - manage_kuviz_exception(e, name) - def get_layers(self): return self._layers @@ -151,3 +129,45 @@ def manage_kuviz_exception(error, name): "Upgrade your account or delete some of your previous maps to be able to create new ones.") raise error + + +def all_publications(credentials=None): + """Get all map visualizations published by the current user. + + Args: + credentials (:py:class:`Credentials `, optional): + A Credentials instance. If not provided, the credentials will be automatically + obtained from the default credentials if available. + + """ + _credentials = get_credentials(credentials) + auth_client = _create_auth_client(_credentials) + kmanager = _get_kuviz_manager(auth_client) + kuvizs = kmanager.all() + return [kuviz_to_dict(kuviz) for kuviz in kuvizs] + + +def delete_publication(name, credentials=None): + """Delete a map visualization published by id. + + Args: + name (str): name of the publication to be deleted. + credentials (:py:class:`Credentials `, optional): + A Credentials instance. If not provided, the credentials will be automatically + obtained from the default credentials if available. + + """ + _credentials = get_credentials(credentials) + auth_client = _create_auth_client(_credentials) + kmanager = _get_kuviz_manager(auth_client) + kuvizs = kmanager.all() + kuviz = next((kuviz for kuviz in kuvizs if kuviz.name == name), None) + + if kuviz is None: + raise PublishError('Publication "{}" not found.'.format(name)) + + try: + kuviz.delete() + log.info('Success! Publication "{0}" deleted'.format(name)) + except Exception as e: + manage_kuviz_exception(e, name) diff --git a/cartoframes/viz/map.py b/cartoframes/viz/map.py index 6f86bb04a..eba469d75 100644 --- a/cartoframes/viz/map.py +++ b/cartoframes/viz/map.py @@ -236,35 +236,6 @@ def update_publication(self, name, password, if_exists='fail'): html = self._get_publication_html(name) return self._publisher.update(html, name, password, if_exists) - @staticmethod - def all_publications(credentials=None): - """Get all map visualization published by the current user. - - Args: - credentials (:py:class:`Credentials `, optional): - A Credentials instance. If not provided, the credentials will be automatically - obtained from the default credentials if available. - - """ - _credentials = get_credentials(credentials) - - return KuvizPublisher.all(_credentials) - - @staticmethod - def delete_publication(name, credentials=None): - """Delete a map visualization published by id. - - Args: - name (str): name of the publication to be deleted. - credentials (:py:class:`Credentials `, optional): - A Credentials instance. If not provided, the credentials will be automatically - obtained from the default credentials if available. - - """ - _credentials = get_credentials(credentials) - - return KuvizPublisher.delete(name, _credentials) - def _get_publication_html(self, name): html_map = HTMLMap('templates/viz/main.html.j2') html_map.set_content( From a717af993a57c270f0b1b9cc49ca10dc47d293d7 Mon Sep 17 00:00:00 2001 From: Josema Camacho Date: Thu, 9 Jul 2020 18:29:22 +0200 Subject: [PATCH 4/5] Added unit tests for all_publications and delete_publication --- tests/unit/viz/test_kuviz.py | 49 +++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/tests/unit/viz/test_kuviz.py b/tests/unit/viz/test_kuviz.py index 5d4411214..e928c27c1 100644 --- a/tests/unit/viz/test_kuviz.py +++ b/tests/unit/viz/test_kuviz.py @@ -1,9 +1,11 @@ # -*- coding: utf-8 -*- +from collections import namedtuple + import pytest from cartoframes.auth import Credentials -from cartoframes.viz import Map, Layer +from cartoframes.viz import all_publications, delete_publication, Layer, Map from cartoframes.viz.source import Source from cartoframes.viz.kuviz import KuvizPublisher, DEFAULT_PUBLIC, kuviz_to_dict from cartoframes.io.managers.context_manager import ContextManager @@ -162,3 +164,48 @@ def test_kuviz_publisher_update(self, mocker): assert kuviz_publisher.kuviz == kuviz assert result_update == kuviz_to_dict(kuviz) assert result_publish != kuviz_to_dict(kuviz) + + def test_kuviz_all_publications(self, mocker): + setup_mocks(mocker, self.credentials) + + html = 'fake_html' + kuviz_name = 'fake_name' + kuviz = CartoKuvizMock(kuviz_name) + + KuvizManagerNamedtuple = namedtuple('KuvizManager', ['all']) + kuviz_manager_namedtuple = KuvizManagerNamedtuple(lambda: [kuviz]) + + mocker.patch('cartoframes.viz.kuviz._create_kuviz', return_value=kuviz) + mocker.patch('cartoframes.viz.kuviz._get_kuviz_manager', return_value=kuviz_manager_namedtuple) + + vmap = Map(Layer('fake_table', credentials=self.credentials)) + + kuviz_publisher = KuvizPublisher(None) + kuviz_publisher.set_layers(vmap.layers) + kuviz_publisher.publish(html, kuviz_name, None) + + assert all_publications(credentials=self.credentials)[0]['name'] == kuviz_name + + def test_kuviz_delete_publication(self, mocker): + setup_mocks(mocker, self.credentials) + + html = 'fake_html' + kuviz_name = 'fake_name' + kuviz = CartoKuvizMock(kuviz_name) + + KuvizManagerNamedtuple = namedtuple('KuvizManager', ['all']) + kuviz_manager_namedtuple = KuvizManagerNamedtuple(lambda: [kuviz]) + + mocker.patch('cartoframes.viz.kuviz._create_kuviz', return_value=kuviz) + mocker.patch('cartoframes.viz.kuviz._get_kuviz_manager', return_value=kuviz_manager_namedtuple) + mock = mocker.patch('tests.unit.mocks.kuviz_mock.CartoKuvizMock.delete') + + vmap = Map(Layer('fake_table', credentials=self.credentials)) + + kuviz_publisher = KuvizPublisher(None) + kuviz_publisher.set_layers(vmap.layers) + kuviz_publisher.publish(html, kuviz_name, None) + + delete_publication(kuviz_name, credentials=self.credentials) + + mock.assert_called_once_with() From f966e4de62764d6f5755e8d315c7f87bf8bfcb7c Mon Sep 17 00:00:00 2001 From: antoniocarlon Date: Tue, 13 Oct 2020 12:07:35 +0200 Subject: [PATCH 5/5] Trigger tests --- tests/README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/README.md b/tests/README.md index 83d4ec788..0a7abd34d 100644 --- a/tests/README.md +++ b/tests/README.md @@ -136,4 +136,3 @@ pip install pre-commit ``` pre-commit install ``` -