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

Commit

Permalink
Added some catches that I hope will help ensure dupes and non-Documen…
Browse files Browse the repository at this point in the history
…t objects stay out of the document_list. Fixes #25 and fixes #24.
  • Loading branch information
palewire committed Apr 11, 2011
1 parent 7951cb9 commit b095fe5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 10 deletions.
29 changes: 20 additions & 9 deletions documentcloud.py
Expand Up @@ -311,12 +311,15 @@ class DocumentSet(list):
from getting into the list and ensuring that only Document
objects are appended.
"""

def append(self, obj, n=1):
for i in xrange(n):
existing_ids = [i.id for i in list(self.__iter__())]
if obj.id not in existing_ids:
super(DocumentSet, self).append(copy.copy(obj))
def append(self, obj):
# Verify that the user is trying to add a Document object
if not isinstance(obj, Document):
raise TypeError("Only Document objects can be added to the document_list")
# Check if the object is already in the list
if obj.id in [i.id for i in list(self.__iter__())]:
raise DuplicateObjectError("This object already exists in the document_list")
# If it's all true, append it.
super(DocumentSet, self).append(copy.copy(obj))


class Project(BaseAPIObject):
Expand All @@ -331,7 +334,7 @@ def __setattr__(self, attr, value):
if attr == 'document_list':
if value == None:
self.__dict__[u'document_list'] = DocumentSet([])
elif type(value) == list:
elif isinstance(value, list):
self.__dict__[u'document_list'] = DocumentSet(value)
else:
raise TypeError
Expand Down Expand Up @@ -497,6 +500,12 @@ class DoesNotExistError(Exception):
"""
pass

class DuplicateObjectError(Exception):
"""
Raised when the user tries to add a duplicate to a distinct list.
"""
pass

#
# API connection clients
#
Expand Down Expand Up @@ -716,13 +725,15 @@ def __init__(self, username=None, password=None):
public = DocumentCloud()
private = DocumentCloud(DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD)
bad = DocumentCloud("Bad", "Login")
# Pull the project
proj = private.projects.get("703")
print proj.document_list
print "docs: %s" % len(proj.document_list)
# Zero out the list
doc = private.documents.get(u'12672-the-klee-report-volume-4')
proj.document_list.append(doc)
#proj.document_list = None
proj.document_list = None
print proj.document_list
# Save the changes and check what sticks
proj.put()
proj = private.projects.get("703")
print "docs: %s" % len(proj.document_list)
Expand Down
17 changes: 16 additions & 1 deletion test.py
Expand Up @@ -14,7 +14,7 @@
import textwrap
import unittest
from documentcloud import DocumentCloud
from documentcloud import CredentialsMissingError
from documentcloud import CredentialsMissingError, DuplicateObjectError
from documentcloud import CredentialsFailedError, DoesNotExistError
from documentcloud import Annotation, Document, Project, Section, Entity
from private_settings import DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD
Expand Down Expand Up @@ -270,6 +270,13 @@ def test_put(self):
obj.put()
obj = self.private_client.projects.get("703")
self.assertEqual(len(obj.document_list), len(proj_ids))

def test_document_list_type_restrictions(self):
"""
Make sure document_lists will only accept Document objects
"""
obj = self.private_client.projects.get("703")
self.assertRaises(TypeError, obj.document_list.append, "The letter C")


class ErrorTest(BaseTest):
Expand All @@ -291,6 +298,14 @@ def test_does_not_exist(self):
Make sure DoesNotExistError works.
"""
self.assertRaises(DoesNotExistError, self.public_client.documents.get, 'TK')

def test_duplicate_object(self):
"""
Make sure DuplicateObjectError works.
"""
obj = self.private_client.projects.get("703")
doc = self.private_client.documents.get(u'12672-the-klee-report-volume-4')
self.assertRaises(DuplicateObjectError, obj.document_list.append, doc)


if __name__ == '__main__':
Expand Down

0 comments on commit b095fe5

Please sign in to comment.