diff --git a/documentcloud.py b/documentcloud.py index 75fa2a1..7863d3a 100644 --- a/documentcloud.py +++ b/documentcloud.py @@ -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): @@ -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 @@ -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 # @@ -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) diff --git a/test.py b/test.py index 486d738..23beb23 100644 --- a/test.py +++ b/test.py @@ -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 @@ -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): @@ -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__':