Skip to content

Commit

Permalink
Merge pull request #613 from petridishdev/format-credential-timeline
Browse files Browse the repository at this point in the history
Format credential names correctly on timeline
  • Loading branch information
ianco authored Feb 8, 2021
2 parents eada827 + 1f27bbc commit f97c248
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 81 deletions.
4 changes: 2 additions & 2 deletions server/vcr-server/api/v2/models/Credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ def _cached(self, key, val):
return cache[key]

def get_local_name(self):
return local_name(self.all_names)
return local_name(self.all_names) or self.topic.get_local_name()

def get_remote_name(self):
return remote_name(self.all_names)
return remote_name(self.all_names) or self.topic.get_remote_name()

# used by solr document index
@property
Expand Down
3 changes: 3 additions & 0 deletions server/vcr-server/api/v2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,9 @@ def local_name(names=[]):
if type in types:
local_name = names[types.index(type)]
break
if not local_name:
# Take the first one
local_name = names[0]
return local_name
except Exception as e:
LOGGER.error("Exception was raised: " + str(e))
Expand Down
144 changes: 65 additions & 79 deletions server/vcr-server/api/v2/views/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
TRACE_PROOF_EVENTS = os.getenv("TRACE_PROOF_EVENTS", "false").lower() == "true"

# max attempts to wait for a proof response
PROOF_RETRY_MAX_ATTEMPTS = int(os.getenv("PROOF_RETRY_MAX_ATTEMPTS", "7"))
PROOF_RETRY_MAX_ATTEMPTS = int(os.getenv("PROOF_RETRY_MAX_ATTEMPTS", "7"))
# initial delay in msec between checks for a proof response
PROOF_RETRY_INITIAL_DELAY = float(os.getenv("PROOF_RETRY_INITIAL_DELAY", "500"))
# backoff factor (2 = double the delay with each proof response check)
Expand Down Expand Up @@ -176,38 +176,79 @@ def list_credential_sets(self, request, pk=None):
.all()
)

def get_credential_local_name(credential):
if not credential.get_local_name():
return get_topic_local_name(credential.topic)
else:
return {
"id": credential.get_local_name().id,
"text": credential.get_local_name().text or None,
"language": credential.get_local_name().language or None,
"credential_id": credential.get_local_name().credential_id or None,
"type": credential.get_local_name().type or None,
}

def get_credential_remote_name(credential):
if not credential.get_remote_name():
return get_topic_remote_name(credential.topic)
else:
return {
"id": credential.get_remote_name().id,
"text": credential.get_remote_name().text or None,
"language": credential.get_remote_name().language or None,
"credential_id": credential.get_remote_name().credential_id or None,
"type": credential.get_remote_name().type or None,
}

def get_topic_local_name(topic):
if not topic.get_local_name():
return {}
else:
return {
"id": topic.get_local_name().id,
"text": topic.get_local_name().text or None,
"language": topic.get_local_name().language or None,
"credential_id": topic.get_local_name().credential_id or None,
"type": topic.get_local_name().type or None,
}

def get_topic_remote_name(topic):
if not topic.get_remote_name():
return {}
else:
return {
"id": topic.get_remote_name().id,
"text": topic.get_remote_name().text or None,
"language": topic.get_remote_name().language or None,
"credential_id": topic.get_remote_name().credential_id or None,
"type": topic.get_remote_name().type or None,
}

data = [
{
"id": credential_set.id,
"create_timestamp": credential_set.create_timestamp.isoformat()
if credential_set.create_timestamp is not None
else None,
if credential_set.create_timestamp is not None else None,
"update_timestamp": credential_set.update_timestamp.isoformat()
if credential_set.update_timestamp is not None
else None,
if credential_set.update_timestamp is not None else None,
"latest_credential_id": credential_set.latest_credential_id,
"topic_id": credential_set.topic_id,
"first_effective_date": credential_set.first_effective_date.isoformat()
if credential_set.first_effective_date is not None
else None,
if credential_set.first_effective_date is not None else None,
"last_effective_date": credential_set.last_effective_date.isoformat()
if credential_set.last_effective_date is not None
else None,
if credential_set.last_effective_date is not None else None,
"credentials": [
{
"id": credential.id,
"create_timestamp": credential.create_timestamp.isoformat()
if credential.create_timestamp
else None,
if credential.create_timestamp else None,
"effective_date": credential.effective_date.isoformat()
if credential.effective_date
else None,
if credential.effective_date else None,
"inactive": credential.inactive,
"latest": credential.latest,
"revoked": credential.revoked,
"revoked_date": credential.revoked_date.isoformat()
if credential.revoked_date
else None,
if credential.revoked_date else None,
"credential_id": credential.credential_id,
"names": [
{
Expand All @@ -217,59 +258,20 @@ def list_credential_sets(self, request, pk=None):
"credential_id": name.credential_id,
"type": name.type,
} if name else {}
for name in credential.topic.get_active_names()
for name in credential.all_names
],
"local_name": {
"id": credential.topic.get_local_name().id,
"text": credential.topic.get_local_name().text or None,
"language": credential.topic.get_local_name().language
or None,
"credential_id": credential.topic.get_local_name().credential_id
or None,
"type": credential.topic.get_local_name().type or None,
} if credential.topic.get_local_name() else {},
"remote_name": {
"id": credential.topic.get_remote_name().id,
"text": credential.topic.get_remote_name().text or None,
"language": credential.topic.get_remote_name().language
or None,
"credential_id": credential.topic.get_remote_name().credential_id
or None,
"type": credential.topic.get_remote_name().type or None,
} if credential.topic.get_remote_name() else {},
# "addresses": [
# {
# "country": address.country or None,
# "addressee": address.addressee or None,
# "province": address.province or None,
# "create_timestamp": address.create_timestamp.isoformat()
# if address.create_timestamp is not None
# else None,
# "credential_id": address.credential_id or None,
# "civic_address": address.civic_address or None,
# "update_timestamp": address.update_timestamp.isoformat()
# if address.update_timestamp is not None
# else None,
# "id": address.id,
# "postal_code": address.postal_code or None,
# "city": address.city or None,
# }
# for address in credential.addresses.all()
# ],
"local_name": get_credential_local_name(credential),
"remote_name": get_credential_remote_name(credential),
"topic": {
"id": credential.topic.id,
"source_id": credential.topic.source_id,
"type": credential.topic.type,
"local_name": get_topic_local_name(credential.topic),
"remote_name": get_topic_remote_name(credential.topic)
},
"related_topics": [
{
"id": related_topic.id,
# "create_timestamp": related_topic.create_timestamp
# if related_topic.create_timestamp is not None
# else None,
# "update_timestamp": related_topic.update_timestamp
# if related_topic.update_timestamp is not None
# else None,
"source_id": related_topic.source_id,
"type": related_topic.type,
"names": [
Expand All @@ -282,24 +284,8 @@ def list_credential_sets(self, request, pk=None):
} if name else {}
for name in related_topic.get_active_names()
],
"local_name": {
"id": related_topic.get_local_name().id,
"text": related_topic.get_local_name().text or None,
"language": related_topic.get_local_name().language
or None,
"credential_id": related_topic.get_local_name().credential_id
or None,
"type": related_topic.get_local_name().type or None,
} if related_topic.get_local_name() else {},
"remote_name": {
"id": related_topic.get_remote_name().id,
"text": related_topic.get_remote_name().text or None,
"language": related_topic.get_remote_name().language
or None,
"credential_id": related_topic.get_remote_name().credential_id
or None,
"type": related_topic.get_remote_name().type or None,
} if related_topic.get_remote_name() else {},
"local_name": get_topic_local_name(related_topic),
"remote_name": get_topic_remote_name(related_topic)
} if related_topic else {}
for related_topic in credential.related_topics.all()
],
Expand Down Expand Up @@ -349,7 +335,7 @@ def get_object(self):
# queryset = self.filter_queryset(self.get_queryset())
# obj = get_object_or_404(queryset, type=type, source_id=source_id)

## May raise a permission denied
# May raise a permission denied
# self.check_object_permissions(self.request, obj)
# return obj

Expand Down Expand Up @@ -427,7 +413,7 @@ def verify(self, request, pk=None):
# TODO: if the agent was not started with the --auto-verify-presentation flag, verification will need to be initiated
retries = PROOF_RETRY_MAX_ATTEMPTS
result = None
delay = float(PROOF_RETRY_INITIAL_DELAY/1000)
delay = float(PROOF_RETRY_INITIAL_DELAY / 1000)
while retries > 0:
sleep(delay)
retries -= 1
Expand Down

0 comments on commit f97c248

Please sign in to comment.