Skip to content

Commit 0cfa0a7

Browse files
committed
update authority class to make display field optional, added new test
1 parent f122aa7 commit 0cfa0a7

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

pyPreservica/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@
3535
__author__ = "James Carr (drjamescarr@gmail.com)"
3636

3737
# Version of the pyPreservica package
38-
__version__ = "3.4.3"
38+
__version__ = "3.4.4"
3939

4040
__license__ = "Apache License Version 2.0"

pyPreservica/authorityAPI.py

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111

1212
import csv
13+
import json
1314
from typing import List, Set
1415

1516
from pyPreservica.common import *
@@ -20,22 +21,30 @@
2021

2122

2223
class Table:
23-
def __init__(self, reference: str, name: str, security_tag: str, metadataConnections: list, displayField: str=None):
24-
self.reference = reference
24+
def __init__(self, name: str, security_tag: str, displayField: str=None, metadataConnections: list=[]):
25+
self.reference = None
2526
self.name = name
27+
self.description = None
2628
self.security_tag = security_tag
2729
self.displayField = displayField
2830
self.metadataConnections = metadataConnections
2931
self.fields = None
3032

3133
def __str__(self):
32-
displayField = self.displayField or "<None>"
33-
return f"Ref:\t\t\t{self.reference}\n" \
34+
table_str: str = f"Ref:\t\t\t{self.reference}\n" \
3435
f"Name:\t\t\t{self.name}\n" \
35-
f"Security Tag:\t{self.security_tag}\n" \
36-
f"Display Field:\t\t\t{displayField}\n" \
37-
f"Metadata Connections:\t\t\t{self.metadataConnections}\n" \
38-
f"Fields:\t\t\t{self.fields}\n"
36+
f"Security Tag:\t{self.security_tag}\n"
37+
38+
if self.description is not None:
39+
table_str = table_str + f"Description:\t\t\t{self.description}\n"
40+
if self.displayField is not None:
41+
table_str = table_str + f"Display Field:\t\t\t{self.displayField}\n"
42+
if self.metadataConnections is not None:
43+
table_str = table_str + f"Metadata Connections:\t\t\t{self.metadataConnections}\n"
44+
if self.fields is not None:
45+
table_str = table_str + f"Fields:\t\t\t{self.fields}\n"
46+
47+
return table_str
3948

4049

4150
class AuthorityAPI(AuthenticatedAPI):
@@ -171,6 +180,45 @@ def records(self, table: Table) -> List[dict]:
171180
logger.error(exception)
172181
raise exception
173182

183+
def add_table(self, new_table: Table):
184+
"""
185+
Add a new authority table
186+
187+
:return: An authority table
188+
:rtype: Table
189+
190+
"""
191+
headers = {HEADER_TOKEN: self.token, 'accept': 'application/json;charset=UTF-8', 'Content-Type': 'application/json'}
192+
193+
table_data = {"name": new_table.name}
194+
if new_table.description is not None:
195+
table_data['description'] = new_table.description
196+
if new_table.security_tag is not None:
197+
table_data['securityDescriptor'] = new_table.security_tag
198+
if new_table.displayField is not None:
199+
table_data['displayField'] = new_table.displayField
200+
if new_table.metadataConnections is not None:
201+
table_data['metadataConnections'] = new_table.metadataConnections
202+
if new_table.fields is not None:
203+
table_data['fields'] = new_table.fields
204+
205+
response = self.session.post(f'{self.protocol}://{self.server}{BASE_ENDPOINT}/tables', data=json.dumps(table_data), headers=headers)
206+
207+
if response.status_code == requests.codes.unauthorized:
208+
self.token = self.__token__()
209+
return self.add_table(new_table)
210+
if response.status_code == requests.codes.created:
211+
json_response = str(response.content.decode('utf-8'))
212+
doc = json.loads(json_response)
213+
return self.table(doc['ref'])
214+
else:
215+
exception = HTTPException("", response.status_code, response.url, "add_table",
216+
response.content.decode('utf-8'))
217+
logger.error(exception)
218+
raise exception
219+
220+
return None
221+
174222
def table(self, reference: str) -> Table:
175223
"""
176224
fetch an authority table by its reference
@@ -191,8 +239,12 @@ def table(self, reference: str) -> Table:
191239
if response.status_code == requests.codes.ok:
192240
json_response = str(response.content.decode('utf-8'))
193241
doc = json.loads(json_response)
194-
table = Table(doc['ref'], doc['name'], doc['securityDescriptor'], doc['metadataConnections'], doc.get('displayField'))
195-
table.fields = doc['fields']
242+
table = Table(doc['name'], doc['securityDescriptor'], doc.get('displayField', None), doc.get('metadataConnections', None))
243+
table.reference = doc['ref']
244+
if 'fields' in doc:
245+
table.fields = doc['fields']
246+
if 'description' in doc:
247+
table.fields = doc['description']
196248
return table
197249
else:
198250
exception = HTTPException("", response.status_code, response.url, "table",
@@ -218,7 +270,10 @@ def tables(self) -> Set[Table]:
218270
doc = json.loads(json_response)
219271
results = set()
220272
for table in doc['tables']:
221-
t = Table(table['ref'], table['name'], table['securityDescriptor'], table['metadataConnections'], table.get('displayField'))
273+
t = Table(table['name'], table['securityDescriptor'], table.get('displayField', None), table.get('metadataConnections', None))
274+
t.reference = table['ref']
275+
if 'description' in table:
276+
t.description = table['description']
222277
results.add(t)
223278
return results
224279
else:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# This call to setup() does all the work
2222
setup(
2323
name=PKG,
24-
version="3.4.3",
24+
version="3.4.4",
2525
description="Python library for the Preservica API",
2626
long_description=README,
2727
long_description_content_type="text/markdown",

tests/test_authority_records.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,25 @@ def test_get_tables(setup_data):
2727
results = client.tables()
2828
assert isinstance(results, set)
2929
for table in results:
30+
print(table)
3031
assert isinstance(table, Table)
31-
assert table.name in ["Countries", 'Physical Storage']
3232

3333

3434
def test_get_records(setup_data):
3535
client = AuthorityAPI()
3636
tables = client.tables()
3737
for tab in tables:
38+
print(tab)
3839
records = client.records(tab)
40+
print(records)
41+
42+
43+
def test_add_table(setup_data):
44+
client = AuthorityAPI()
45+
name: str = f"Test Table {datetime.now().isoformat()}"
46+
table = Table(name=name, security_tag="open")
47+
table.description ="An API test table"
48+
table.fields = [{"name" : "creator", "type" : "ShortText", "displayName" : "The Creator", "includeInSummary" : True}]
49+
new_table = client.add_table(table)
50+
print(new_table)
51+
assert new_table.name == name

0 commit comments

Comments
 (0)