Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Delete publications #1497

Merged
merged 7 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cartoframes/viz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -77,5 +79,8 @@
'default_widget',

'popup_element',
'default_popup_element'
'default_popup_element',

'all_publications',
'delete_publication'
]
50 changes: 43 additions & 7 deletions cartoframes/viz/kuviz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -19,13 +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]

def get_layers(self):
return self._layers

Expand Down Expand Up @@ -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)
18 changes: 0 additions & 18 deletions cartoframes/viz/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,6 @@ def publish(self, name, password, credentials=None, if_exists='fail', maps_api_k
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.
Expand All @@ -240,20 +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 <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(
Expand Down
1 change: 0 additions & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,3 @@ pip install pre-commit
```
pre-commit install
```

38 changes: 33 additions & 5 deletions tests/unit/viz/test_kuviz.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -163,21 +165,47 @@ def test_kuviz_publisher_update(self, mocker):
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()
11 changes: 0 additions & 11 deletions tests/unit/viz/test_map.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,17 +317,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)

Expand Down