Skip to content

Commit

Permalink
[#18] Functions to get documents in a zaak folder
Browse files Browse the repository at this point in the history
  • Loading branch information
SilviaAmAm committed Aug 28, 2020
1 parent 4658c50 commit 9fc1b2f
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 59 deletions.
10 changes: 9 additions & 1 deletion drc_cmis/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ class CMISConfigAdmin(SingletonModelAdmin):
)
},
),
(_("Credentials"), {"fields": ("client_user", "client_password",)}),
(
_("Credentials"),
{
"fields": (
"client_user",
"client_password",
)
},
),
)

def cmis_enabled(self, obj):
Expand Down
13 changes: 13 additions & 0 deletions drc_cmis/browser/drc_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,3 +343,16 @@ class ZaakFolder(CMISBaseObject):
table = "drc:zaakfolder"
name_map = ZAAK_MAP
type_name = "zaak"

def get_children_documents(self) -> List[Document]:
"""Get all the documents in a zaak folder
:return: list of documents
"""
data = {
"cmisaction": "query",
"statement": f"SELECT * FROM drc:document WHERE IN_FOLDER('{self.objectId}')",
}
json_response = self.post_request(self.base_url, data=data)

return self.get_all_results(json_response, Document)
22 changes: 21 additions & 1 deletion drc_cmis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ def create_oio(self, data: dict) -> ObjectInformatieObject:
rhs=[data.get("informatieobject")],
)

# Get the parent folder to check if the document is in a Zaak folder
parent_folder = document.get_parent_folders()[0]

# Check if there are gebruiksrechten related to the document
related_gebruiksrechten = self.query(
return_type_name="gebruiksrechten",
Expand All @@ -258,7 +261,7 @@ def create_oio(self, data: dict) -> ObjectInformatieObject:
)

# Case 1: Already related to a zaak. Copy the document to the destination folder.
if len(retrieved_oios) > 0:
if len(retrieved_oios) > 0 or "drc:zaakfolder" in parent_folder.objectTypeId:
self.copy_document(document, destination_folder)
if len(related_gebruiksrechten) > 0:
for gebruiksrechten in related_gebruiksrechten:
Expand Down Expand Up @@ -378,3 +381,20 @@ def get_or_create_other_folder(self) -> Folder:
parent_folder = self.get_or_create_folder(folder_name, parent_folder, props)

return parent_folder

def get_documents_related_to_zaak(self, identificatie: str) -> List[Document]:
"""Get all documents in the zaak folder with drc:zaakfolder__identificatie.
:param identificatie: str, identification of the zaak
:return: list of Documents
"""

zaak_folder = self.query(
return_type_name="zaakfolder",
lhs=["drc:zaak__identificatie = '%s'"],
rhs=[str(identificatie)],
)[0]

documents = zaak_folder.get_children_documents()

return documents
9 changes: 7 additions & 2 deletions drc_cmis/migrations/0004_auto_20200616_0919.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RemoveField(model_name="cmisconfig", name="locations",),
migrations.RemoveField(
model_name="cmisconfig",
name="locations",
),
migrations.AddField(
model_name="cmisconfig",
name="base_folder",
Expand Down Expand Up @@ -49,5 +52,7 @@ class Migration(migrations.Migration):
max_length=200,
),
),
migrations.DeleteModel(name="CMISFolderLocation",),
migrations.DeleteModel(
name="CMISFolderLocation",
),
]
5 changes: 4 additions & 1 deletion drc_cmis/migrations/0012_auto_20200821_1512.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RemoveField(model_name="cmisconfig", name="base_folder_name",),
migrations.RemoveField(
model_name="cmisconfig",
name="base_folder_name",
),
migrations.AddField(
model_name="cmisconfig",
name="other_folder_path",
Expand Down
4 changes: 3 additions & 1 deletion drc_cmis/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ class CMISConfig(SingletonModel):
"http://domain_name:port_number/alfresco/api/-default-/public/cmis/versions/1.1/browser",
)
binding = models.CharField(
choices=BINDING_CHOICES, default="BROWSER", max_length=200,
choices=BINDING_CHOICES,
default="BROWSER",
max_length=200,
)
time_zone = models.CharField(
default="UTC",
Expand Down
15 changes: 10 additions & 5 deletions drc_cmis/utils/folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@
PathElementTemplate = namedtuple("PathElementTemplate", ["folder_name", "required"])

YEAR_PATH_ELEMENT_TEMPLATE = PathElementTemplate(
folder_name="{{ year }}", required=False,
folder_name="{{ year }}",
required=False,
)
MONTH_PATH_ELEMENT_TEMPLATE = PathElementTemplate(
folder_name="{{ month }}", required=False,
folder_name="{{ month }}",
required=False,
)
DAY_PATH_ELEMENT_TEMPLATE = PathElementTemplate(
folder_name="{{ day }}", required=False,
folder_name="{{ day }}",
required=False,
)
ZAAKTYPE_PATH_ELEMENT_TEMPLATE = PathElementTemplate(
folder_name="{{ zaaktype }}", required=True,
folder_name="{{ zaaktype }}",
required=True,
)
ZAAK_PATH_ELEMENT_TEMPLATE = PathElementTemplate(
folder_name="{{ zaak }}", required=True,
folder_name="{{ zaak }}",
required=True,
)


Expand Down
6 changes: 4 additions & 2 deletions drc_cmis/webservice/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ def copy_gebruiksrechten(
)

soap_response = self.request(
"ObjectService", soap_envelope=soap_envelope.toxml(),
"ObjectService",
soap_envelope=soap_envelope.toxml(),
)

# Creating the document only returns its ID
Expand Down Expand Up @@ -390,7 +391,8 @@ def create_content_object(
)

soap_response = self.request(
"ObjectService", soap_envelope=soap_envelope.toxml(),
"ObjectService",
soap_envelope=soap_envelope.toxml(),
)

xml_response = extract_xml_from_soap(soap_response)
Expand Down
33 changes: 31 additions & 2 deletions drc_cmis/webservice/drc_document.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,8 @@ def update_properties(
)

soap_response = self.request(
"ObjectService", soap_envelope=soap_envelope.toxml(),
"ObjectService",
soap_envelope=soap_envelope.toxml(),
)

xml_response = extract_xml_from_soap(soap_response)
Expand Down Expand Up @@ -397,7 +398,8 @@ def delete_object(self):
)

self.request(
"VersioningService", soap_envelope=soap_envelope.toxml(),
"VersioningService",
soap_envelope=soap_envelope.toxml(),
)
refreshed_document = self.get_latest_version()
return refreshed_document.delete_object()
Expand Down Expand Up @@ -497,3 +499,30 @@ class ZaakFolder(CMISBaseObject):
name_map = ZAAK_MAP
type_name = "zaak"
type_class = ZaakFolderData

def get_children_documents(self) -> List[Document]:
"""Get all the documents in a zaak folder
:return: list of documents
"""
# In Corsa, the query "SELECT * FROM cmis:document WHERE IN_FOLDER('objectId')" doesnt work
# Operation getChildren returns all the children (including folders)
soap_envelope = make_soap_envelope(
auth=(self.user, self.password),
repository_id=self.main_repo_id,
folder_id=self.objectId,
cmis_action="getChildren",
)

soap_response = self.request(
"NavigationService", soap_envelope=soap_envelope.toxml()
)

xml_response = extract_xml_from_soap(soap_response)
extracted_data = extract_object_properties_from_xml(xml_response, "getChildren")

return [
Document(data)
for data in extracted_data
if "drc:document" in data["properties"]["cmis:objectTypeId"]["value"]
]
35 changes: 28 additions & 7 deletions drc_cmis/webservice/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,31 +146,52 @@ def request(
error = soap_response.text
if soap_response.status_code == 401:
raise CmisPermissionDeniedException(
status=soap_response.status_code, url=url, message=error, code=401,
status=soap_response.status_code,
url=url,
message=error,
code=401,
)
elif soap_response.status_code == 400:
raise CmisInvalidArgumentException(
status=soap_response.status_code, url=url, message=error, code=400,
status=soap_response.status_code,
url=url,
message=error,
code=400,
)
elif soap_response.status_code == 404:
raise CmisObjectNotFoundException(
status=soap_response.status_code, url=url, message=error, code=404,
status=soap_response.status_code,
url=url,
message=error,
code=404,
)
elif soap_response.status_code == 403:
raise CmisPermissionDeniedException(
status=soap_response.status_code, url=url, message=error, code=403,
status=soap_response.status_code,
url=url,
message=error,
code=403,
)
elif soap_response.status_code == 405:
raise CmisNotSupportedException(
status=soap_response.status_code, url=url, message=error, code=405,
status=soap_response.status_code,
url=url,
message=error,
code=405,
)
elif soap_response.status_code == 409:
raise CmisUpdateConflictException(
status=soap_response.status_code, url=url, message=error, code=409,
status=soap_response.status_code,
url=url,
message=error,
code=409,
)
elif soap_response.status_code == 500:
raise CmisRuntimeException(
status=soap_response.status_code, url=url, message=error, code=500,
status=soap_response.status_code,
url=url,
message=error,
code=500,
)
else:
raise CmisBaseException(
Expand Down
6 changes: 4 additions & 2 deletions drc_cmis/webservice/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ def make_soap_envelope(
# Username token
username_token = xml_doc.createElement("wsse:UsernameToken")
username_token.setAttribute(
"wsu:Id", f"UsernameToken-{security_header_id}",
"wsu:Id",
f"UsernameToken-{security_header_id}",
)
username_tag = xml_doc.createElement("wsse:Username")
username_text = xml_doc.createTextNode(auth[0])
Expand All @@ -241,7 +242,8 @@ def make_soap_envelope(
# Time stamp
time_stamp_tag = xml_doc.createElement("wsu:Timestamp")
time_stamp_tag.setAttribute(
"wsu:Id", f"TS-{security_header_id}",
"wsu:Id",
f"TS-{security_header_id}",
)

created_tag = xml_doc.createElement("wsu:Created")
Expand Down
Loading

0 comments on commit 9fc1b2f

Please sign in to comment.