Skip to content

Commit

Permalink
allows use of overwrite + privacy in write operations
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-esch committed Dec 15, 2017
1 parent b24295e commit 8aa90ae
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions cartoframes/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from carto.auth import APIKeyAuthClient
from carto.sql import SQLClient, BatchSQLClient
from carto.exceptions import CartoException
from carto.datasets import DatasetManager

from cartoframes.credentials import Credentials
from cartoframes.dataobs import get_countrytag
Expand Down Expand Up @@ -165,9 +166,8 @@ def read(self, table_name, limit=None, index='cartodb_id',

return self.query(query, decode_geom=decode_geom)

def write(self, df, table_name, temp_dir=CACHE_DIR,
overwrite=False, lnglat=None, encode_geom=False, geom_col=None,
**kwargs):
def write(self, df, table_name, temp_dir=CACHE_DIR, overwrite=False,
lnglat=None, encode_geom=False, geom_col=None, **kwargs):
"""Write a DataFrame to a CARTO table.
Example:
Expand Down Expand Up @@ -224,11 +224,9 @@ def write(self, df, table_name, temp_dir=CACHE_DIR,
for more information. For example, when using
`content_guessing='true'`, a column named 'countries' with
country names will be used to generate polygons for each
country. To avoid unintended consequences, avoid `file`,
`url`, and other similar arguments. Note: Combining `privacy`
with `overwrite` (defined above) does not currently update
the privacy of a dataset if it already exists:
https://github.com/CartoDB/cartoframes/issues/252.
country. Another use is setting the privacy of a dataset. To
avoid unintended consequences, avoid `file`, `url`, and other
similar arguments.
Returns:
:obj:`BatchJobStatus` or None: If `lnglat` flag is set and the
Expand Down Expand Up @@ -277,6 +275,9 @@ def write(self, df, table_name, temp_dir=CACHE_DIR,
kwargs)
self._set_schema(_df, final_table_name, pgcolnames)

if kwargs.get('privacy'):
self._update_privacy(final_table_name, kwargs.get('privacy'))

# create geometry column from long/lats if requested
if lnglat:
query = utils.minify_sql((
Expand Down Expand Up @@ -314,6 +315,13 @@ def write(self, df, table_name, temp_dir=CACHE_DIR,
'dataset',
final_table_name)))

def _update_privacy(self, table_name, privacy):
"""Updates the privacy of a dataset"""
ds_manager = DatasetManager(self.auth_client)
dataset = ds_manager.get(table_name)
dataset.privacy = privacy
dataset.save()

def delete(self, table_name):
"""Delete a table in user's CARTO account.
Expand Down

0 comments on commit 8aa90ae

Please sign in to comment.