Skip to content

Commit

Permalink
Wrote a DELETE method for Projects and a test that passes. Fixes #19
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Apr 12, 2011
1 parent c527ba3 commit 155ee61
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
29 changes: 24 additions & 5 deletions documentcloud/__init__.py
Expand Up @@ -67,7 +67,7 @@ def _make_request(self, url, params=None, opener=None):
raise CredentialsFailedError("The resource you've requested requires proper credentials.")
else:
raise e
# Read the response
# Read the response and return it
return response.read()

@credentials_required
Expand All @@ -91,6 +91,7 @@ def put(self, method, params):
else:
# Otherwise, we can just use the vanilla urllib prep method
params = urllib.urlencode(params)
# Make the request
content = self._make_request(
self.BASE_URI + method,
params,
Expand All @@ -100,6 +101,7 @@ def fetch(self, method, params=None):
"""
Fetch an url.
"""
# Encode params if they exist
if params:
params = urllib.urlencode(params)
content = self._make_request(
Expand Down Expand Up @@ -291,6 +293,16 @@ def create(self, title, description=None, document_ids=None):
if not new_id:
raise DuplicateObjectError("The Project title you tried to create already exists")
return new_id

@credentials_required
def delete(self, id):
"""
Deletes a Project.
"""
data = self.fetch(
'projects/%s.json' % id,
{'_method': 'delete'},
)


#
Expand Down Expand Up @@ -669,12 +681,12 @@ def __setattr__(self, attr, value):
object.__setattr__(self, attr, value)

#
# Updates
# Updates and such
#

def put(self):
"""
Save changes made to the object to DocumentCloud.
Save changes made to the object to documentcloud.org
According to DocumentCloud's docs, edits are allowed for the following
fields:
Expand All @@ -692,6 +704,12 @@ def put(self):
)
self._connection.put('projects/%s.json' % self.id, params)

def delete(self):
"""
Deletes this object from documentcloud.org.
"""
self._connection.projects.delete(self.id)

#
# Documents
#
Expand Down Expand Up @@ -762,6 +780,7 @@ class Section(BaseAPIObject):
# Pull the project
#proj = private.projects.get("703")
#doc = private.documents.get(u'83251-fbi-file-on-christopher-biggie-smalls-wallace')
upload = private.projects.create("This is bad test")
print upload
#upload = private.projects.create("0001 - This is a test")
obj = private.projects.get(1630)
obj.delete()

9 changes: 7 additions & 2 deletions test.py
Expand Up @@ -279,13 +279,18 @@ def test_document_list_type_restrictions(self):
obj = self.private_client.projects.get("703")
self.assertRaises(TypeError, obj.document_list.append, "The letter C")

def test_create(self):
def test_create_and_delete(self):
"""
Test whether you can create a new project.
"""
new_id = self.private_client.projects.create("This is only a test (%s)" % get_random_string())
# Create it
title = "00 - (%s) - This is only a test" % get_random_string()
new_id = self.private_client.projects.create(title)
proj = self.private_client.projects.get(new_id)
self.assertEqual(type(proj), Project)
# Delete it
proj.delete()
self.assertRaises(DoesNotExistError, self.private_client.projects.get, new_id)


class ErrorTest(BaseTest):
Expand Down

0 comments on commit 155ee61

Please sign in to comment.