Skip to content

Commit

Permalink
Merge pull request #1086 from dandi/bf-user-no-metadata
Browse files Browse the repository at this point in the history
BF(workaround): to avoid crash for user lacking metadata - return INCOMPLETE
  • Loading branch information
yarikoptic authored May 6, 2022
2 parents e4905af + a792d07 commit 9509dc4
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions dandiapi/api/views/users.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import logging

from allauth.socialaccount.models import SocialAccount
from django.contrib.auth.models import User
from django.db.models import Q
Expand All @@ -15,6 +17,19 @@
from dandiapi.api.permissions import IsApproved
from dandiapi.api.views.serializers import UserDetailSerializer, UserSerializer

logger = logging.getLogger(__name__)


def _get_user_status(user: User):
# Workaround for https://github.com/dandi/dandi-archive/issues/1085
# TODO: remove/robustify some other way whenever underlying reason is
# identified
metadata = getattr(user, 'metadata', None)
if metadata:
return metadata.status
logger.error("User %s lacks .metadata. Returning user's status as INCOMPLETE", user)
return UserMetadata.Status.INCOMPLETE


def user_to_dict(user: User):
"""
Expand All @@ -26,7 +41,7 @@ def user_to_dict(user: User):
'admin': user.is_superuser,
'username': user.username,
'name': f'{user.first_name} {user.last_name}'.strip(),
'status': user.metadata.status,
'status': _get_user_status(user),
'created': user.date_joined,
}

Expand All @@ -46,7 +61,7 @@ def social_account_to_dict(social_account: SocialAccount):
'admin': user.is_superuser,
'username': username,
'name': name,
'status': user.metadata.status,
'status': _get_user_status(user),
'created': created,
}

Expand Down

0 comments on commit 9509dc4

Please sign in to comment.