Repository: CartoDB/cartoframes
Pull request #1497 (2020-01-17 11:04:51)

Delete publications
Related to https://github.com/CartoDB/cartoframes/issues/1496


Patch:
diff --git a/cartoframes/viz/__init__.py b/cartoframes/viz/__init__.py
index 83bad4b7..89d60e84 100644
--- a/cartoframes/viz/__init__.py
+++ b/cartoframes/viz/__init__.py
@@ -38,6 +38,8 @@ from .widgets import default_widget
 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 @@ __all__ = [
     '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 23db067a..f23d2d8c 100644
--- a/cartoframes/viz/kuviz.py
+++ b/cartoframes/viz/kuviz.py
@@ -6,6 +6,7 @@ from carto.kuvizs import KuvizManager
 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,13 +20,6 @@ class KuvizPublisher:
         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]
-
     def get_layers(self):
         return self._layers
 
@@ -135,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 <cartoframes.auth.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 <cartoframes.auth.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 e79c76a4..eba469d7 100644
--- a/cartoframes/viz/map.py
+++ b/cartoframes/viz/map.py
@@ -219,10 +219,6 @@ class Map:
         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.
 
@@ -240,20 +236,6 @@ class Map:
         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 <cartoframes.auth.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)
-
     def _get_publication_html(self, name):
         html_map = HTMLMap('templates/viz/main.html.j2')
         html_map.set_content(


Test patch:
diff --git a/tests/README.md b/tests/README.md
index 44b229fe..e7ad4db1 100644
--- a/tests/README.md
+++ b/tests/README.md
@@ -159,4 +159,3 @@ pip install pre-commit
 ```
 pre-commit install
 ```
-
diff --git a/tests/unit/viz/test_kuviz.py b/tests/unit/viz/test_kuviz.py
index 773c6272..e928c27c 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
@@ -163,21 +165,47 @@ class TestKuvizPublisher(object):
         assert result_update == kuviz_to_dict(kuviz)
         assert result_publish != kuviz_to_dict(kuviz)
 
-    def test_kuviz_publisher_delete(self, mocker):
+    def test_kuviz_all_publications(self, mocker):
         setup_mocks(mocker, self.credentials)
 
-        kuviz = CartoKuvizMock('fake_kuviz')
+        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)
 
-        kuviz_publisher.delete()
+        delete_publication(kuviz_name, credentials=self.credentials)
 
-        assert kuviz_publisher.kuviz is None
+        mock.assert_called_once_with()
diff --git a/tests/unit/viz/test_map.py b/tests/unit/viz/test_map.py
index bcd97988..d4ab1823 100644
--- a/tests/unit/viz/test_map.py
+++ b/tests/unit/viz/test_map.py
@@ -317,17 +317,6 @@ width: ramp(linear(zoom(),0,18),[2,10])
         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)