diff --git a/Helpers.py b/Helpers.py new file mode 100644 index 0000000..4bea54e --- /dev/null +++ b/Helpers.py @@ -0,0 +1,12 @@ +from datetime import datetime + +def parseDate(date: str): + + if date==None: + return None + + for format in ["%Y-%m-%dT%H:%M:%S.%fZ","%Y-%m-%dT%H:%M:%SZ"]: + try: + return datetime.strptime(date, format) + except ValueError: + pass \ No newline at end of file diff --git a/Models.py b/Models.py index 3f4fde8..3d68f87 100644 --- a/Models.py +++ b/Models.py @@ -1,8 +1,10 @@ import datetime -from decimal import Decimal import uuid -from Enums import DataFormat, ResultValueType +from Enums import * +from decimal import Decimal +from datetime import datetime +from Helpers import parseDate # generic @@ -84,7 +86,7 @@ class File: self.content = content class FileInfo: - def __init__(self, type: str, contentType: str, name: str, createdDate: str, modifiedDate: str, owner: str, fileHref: str): + def __init__(self, type: str, contentType: str, name: str, createdDate: datetime, modifiedDate: datetime, owner: str, fileHref: str): self.type = type self.contentType = contentType self.name = name @@ -133,44 +135,59 @@ class AddOrUpdateListItemResponse: class StringFieldValue: def __init__(self, value: str, fieldId: int): - self.type = ResultValueType.String.name self.fieldId = fieldId self.value = value class IntegerFieldValue: def __init__(self, value: int, fieldId: int): - self.type = ResultValueType.Integer.name self.value = value self.fieldId = fieldId class DecimalFieldValue: def __init__(self, value: Decimal, fieldId: int): - self.type = ResultValueType.Decimal.name self.value = value self.fieldId = fieldId class DateFieldValue: def __init__(self, value: datetime, fieldId: int): - self.type = ResultValueType.Date.name self.value = value self.fieldId = fieldId class GuidFieldValue: - def __init__(self, value: uuid, fieldId: int): - self.type = ResultValueType.Guid.name + def __init__(self, value: uuid.UUID, fieldId: int): self.value = value self.fieldId = fieldId class TimeSpanData: - def __init__(self, quantity: Decimal, ): - pass + def __init__(self, quantity: Decimal, increment: str, recurrence: str=None, endByDate: datetime=None, endAfterOccurrences: int=None): + self.quantity = quantity + self.increment = increment + self.recurrence = recurrence + self.endByDate = endByDate + self.endAfterOccurrences = endAfterOccurrences + class TimeSpanValue: - def __init__(self, value: uuid, fieldId: int): - self.type = ResultValueType.Guid.name + def __init__(self, value: TimeSpanData, fieldId: int): self.value = value self.fieldId = fieldId +class StringListValue: + def __init__(self, value: list[str], fieldId: int): + self.value = value + self.fieldId = fieldId + +class IntegerListValue: + def __init__(self, value: list[int], fieldId: int): + self.value = value + self.fieldId = fieldId + +class GuidListValue: + def __init__(self, value: list[uuid.UUID], fieldId: int): + self.value = value + self.fieldId = fieldId + + # record specific class RecordFieldValue: @@ -184,7 +201,7 @@ class RecordFieldValue: if self.type != ResultValueType.String.name: return None - return StringFieldValue(str(self.value), self.fieldId).value + return StringFieldValue(self.value, self.fieldId).value def AsInt(self): @@ -205,11 +222,7 @@ class RecordFieldValue: if self.type != ResultValueType.Date.name: return None - for format in ["%Y-%m-%dT%H:%M:%S.%fZ","%Y-%m-%dT%H:%M:%SZ"]: - try: - date = datetime.datetime.strptime(self.value, format) - except ValueError: - pass + date = parseDate(self.value) return DateFieldValue(date, self.fieldId).value @@ -220,14 +233,70 @@ class RecordFieldValue: return GuidFieldValue(uuid.UUID(self.value), self.fieldId).value - def AsTimeSpan(): + def AsTimeSpan(self): + + if self.type != ResultValueType.TimeSpan.name: + return None + + value = dict(self.value) + + quantity = value.get('quantity') + increment = value.get('increment') + recurrence = value.get('recurrence') + endByDate = value.get('endByDate') + endAfterOccurrences = value.get('endAfterOccurrences') + + endByDate = parseDate(endByDate) + + data = TimeSpanData( + quantity, + increment, + recurrence, + endByDate, + endAfterOccurrences) + + return TimeSpanValue(data, self.fieldId).value + + def AsStringList(self): + + if self.type != ResultValueType.StringList.name: + return None + + return StringListValue(self.value, self.fieldId).value + + def AsIntegerList(self): + + if self.type != ResultValueType.IntegerList.name: + return None + + return IntegerListValue(self.value, self.fieldId).value + + def AsGuidList(self): + + if self.type != ResultValueType.GuidList.name: + return None + + guids = [] + + for guid in self.value: + guids.append(uuid.UUID(guid)) + + return GuidListValue(guids, self.fieldId).value + + def AsAttachmentList(self): + return + + def AsScoringGroupList(self): + return + + def AsFileList(self): return class Record: - def __init__(self, appId: int, recordId: int, fieldData: list[RecordFieldValue]): + def __init__(self, appId: int, recordId: int, fields: list[RecordFieldValue]): self.appId = appId self.recordId = recordId - self.fieldData = fieldData + self.fields = fields class GetRecordsByAppRequest: def __init__(self, appId: int, fieldIds: list[int]=[], dataFormat: str=DataFormat.Raw.name, pagingRequest: PagingRequest=PagingRequest(1,50)): diff --git a/OnspringClient.py b/OnspringClient.py index ff2ed20..14de29e 100644 --- a/OnspringClient.py +++ b/OnspringClient.py @@ -459,12 +459,15 @@ class OnspringClient: jsonResponse = response.json() + createdDate = parseDate(jsonResponse['createdDate']) + modifiedDate = parseDate(jsonResponse['modifiedDate']) + fileInfo = FileInfo( jsonResponse['type'], jsonResponse['contentType'], jsonResponse['name'], - jsonResponse['createdDate'], - jsonResponse['modifiedDate'], + createdDate, + modifiedDate, jsonResponse['owner'], jsonResponse['fileHref']) @@ -839,7 +842,7 @@ class OnspringClient: fields.append(field) - record.fieldData = fields + record.fields = fields records.append(record) diff --git a/script.py b/script.py index e8a0b23..af860ce 100644 --- a/script.py +++ b/script.py @@ -10,7 +10,7 @@ url = cfg['prod']['url'] onspringClient = OnspringClient(url, key) -request = GetRecordsByAppRequest(195) +request = GetRecordsByAppRequest(195, dataFormat=DataFormat.Raw.name) response = onspringClient.GetRecordsByAppId(request) @@ -24,6 +24,7 @@ for record in response.data.records: print(f' AppId: {record.appId}') print(f' RecordId: {record.recordId}') - for value in record.fieldData: - print(f' Type: {value.type}, FieldId: {value.fieldId}, Value: {value.value}') - + for field in record.fields: + if field.type == ResultValueType.Decimal.name: + print(f' Type: {field.type}, FieldId: {field.fieldId}, Value: {field.AsDecimal()}') + print(field.AsDecimal()) \ No newline at end of file