Skip to content

Commit

Permalink
BF(workaround): to avoid crash for user lacking metadata - return INC…
Browse files Browse the repository at this point in the history
…OMPLETE

Underlying issue is somewhere in the logic/instructions so that locally created
user lacks .metadata somehow, although it seems to be available within /admin
interface among Users table for that user.  So must be some glue missing etc.
With this workaround it should still work ok, but would avoid crashing
and due to INCOMPLETE I would expect that user lacking any super powers
otherwise granted

Workaround for #1085
  • Loading branch information
yarikoptic committed May 5, 2022
1 parent c29850b commit a792d07
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 a792d07

Please sign in to comment.