Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Implement PUT method for empty JSON list
Browse files Browse the repository at this point in the history
To clear/empty a data-set, you PUT an empty JSON list to the data
endpoint (ie /data/my-data-group/my-data-type).

We currently only allow an empty list - in the future we could extend to
allow setting the entire contents of the data set.

https://www.pivotaltracker.com/story/show/69622004

[Delivers #69622004]
  • Loading branch information
Paul M Furley committed May 1, 2014
1 parent 17f6952 commit 68fb8df
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions backdrop/core/data_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def query(self, query):

return result

def empty(self):
collection = self.db.get_collection(self.config.name)
collection.remove({})

def _add_id(self, datum):
self._validate_presence_of_auto_id_keys(datum)
return dict(datum.items() + [("_id", self._generate_id(datum))])
Expand Down
3 changes: 3 additions & 0 deletions backdrop/core/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ def group(self, keys, query, collect_fields):
reduce=self._build_reducer_function(collect_fields)
)

def remove(self, *args, **kwargs):
return self._collection.remove(*args, **kwargs)

def _build_collector_code(self, collect_fields):
template = "if (current['{c}'] !== undefined) " \
"{{ previous['{c}'].push(current['{c}']); }}"
Expand Down
36 changes: 36 additions & 0 deletions backdrop/write/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,34 @@ def write_by_group(data_group, data_type):
abort(400, repr(e))


@app.route('/data/<data_group>/<data_type>', methods=['PUT'])
@cache_control.nocache
def put_by_group_and_type(data_group, data_type):
"""
Put by group/type
e.g. PUT https://BACKDROP/data/gcloud/sales
Note: At the moment this is just being used to empty data-sets.
Trying to PUT a non empty list of records will result in a
PutNonEmptyNotImplementedError exception.
"""
data_set_config = data_set_repository.get_data_set_for_query(
data_group,
data_type)

_validate_config(data_set_config)
_validate_auth(data_set_config)

try:
data = listify_json(request.json)
if len(data) > 0:
abort(400, 'Not implemented: you can only pass an empty JSON list')

return _empty_data_set(data_set_config)

except (ParseError, ValidationError) as e:
abort(400, repr(e))


@app.route('/<data_set:data_set_name>', methods=['POST'])
@cache_control.nocache
def post_to_data_set(data_set_name):
Expand Down Expand Up @@ -198,6 +226,14 @@ def _append_to_data_set(data_set_config, data, ok_message=None):
return jsonify(status='ok')


def _empty_data_set(data_set_config):
data_set = DataSet(db, data_set_config)
data_set.empty()
return jsonify(
status='ok',
message='{} now contains 0 records'.format(data_set_config.name))


def listify_json(data):
if data is None:
raise ValidationError("Request must be JSON")
Expand Down
17 changes: 15 additions & 2 deletions features/support/http_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,30 @@ def get(self, url, headers=None):
return HTTPTestResponse(response)

def post(self, url, **message):
return self.post_or_put('POST', url, **message)

def put(self, url, **message):
return self.post_or_put('PUT', url, **message)

def post_or_put(self, method, url, **message):
assert method in ('POST', 'PUT'), 'Only support POST, PUT'
http_function = {
'POST': requests.post,
'PUT': requests.put
}[method]

headers = dict(message.get("headers", []))

if "data" in message:
headers.update({"Content-type": message['content_type']})
response = requests.post(
response = http_function(
self._write_api.url(url),
data=message['data'],
headers=headers,
timeout=60,
)
elif "files" in message:
response = requests.post(
response = http_function(
self._write_api.url(url),
files=message['files'],
headers=headers,
Expand Down

0 comments on commit 68fb8df

Please sign in to comment.