Skip to content

Commit

Permalink
Merge pull request #1 from opengisch/useraccount_fix
Browse files Browse the repository at this point in the history
useraccount is not guaranteed to exist (e.g. teams)
  • Loading branch information
suricactus committed May 9, 2021
2 parents ee4a304 + 02c3521 commit 9a0beb2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
6 changes: 3 additions & 3 deletions docker-app/qfieldcloud/core/permissions_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ def can_become_collaborator(user: QfcUser, project: Project) -> bool:


def can_read_geodb(user: QfcUser, profile: QfcUser) -> bool:
if not profile.useraccount.is_geodb_enabled:
if not hasattr(profile, "useraccount") or not profile.useraccount.is_geodb_enabled:
return False

if can_update_user(user, profile):
Expand All @@ -384,7 +384,7 @@ def can_read_geodb(user: QfcUser, profile: QfcUser) -> bool:


def can_create_geodb(user: QfcUser, profile: QfcUser) -> bool:
if not profile.useraccount.is_geodb_enabled:
if not hasattr(profile, "useraccount") or not profile.useraccount.is_geodb_enabled:
return False

if profile.has_geodb:
Expand All @@ -397,7 +397,7 @@ def can_create_geodb(user: QfcUser, profile: QfcUser) -> bool:


def can_delete_geodb(user: QfcUser, profile: QfcUser) -> bool:
if not profile.useraccount.is_geodb_enabled:
if not hasattr(profile, "useraccount") or not profile.useraccount.is_geodb_enabled:
return False

if not profile.has_geodb:
Expand Down
12 changes: 8 additions & 4 deletions docker-app/qfieldcloud/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class CompleteUserSerializer(serializers.ModelSerializer):
avatar_url = serializers.SerializerMethodField()

def get_avatar_url(self, obj):
return obj.useraccount.avatar_url
return obj.useraccount.avatar_url if hasattr(obj, "useraccount") else None

class Meta:
model = User
Expand All @@ -86,7 +86,7 @@ class PublicInfoUserSerializer(serializers.ModelSerializer):
avatar_url = serializers.SerializerMethodField()

def get_avatar_url(self, obj):
return obj.useraccount.avatar_url
return obj.useraccount.avatar_url if hasattr(obj, "useraccount") else None

class Meta:
model = User
Expand All @@ -100,7 +100,7 @@ class OrganizationSerializer(serializers.ModelSerializer):
avatar_url = serializers.SerializerMethodField()

def get_avatar_url(self, obj):
return obj.useraccount.avatar_url
return obj.useraccount.avatar_url if hasattr(obj, "useraccount") else None

class Meta:
model = Organization
Expand Down Expand Up @@ -142,7 +142,11 @@ def get_email(self, obj):
return obj.user.email

def get_avatar_url(self, obj):
return obj.user.useraccount.avatar_url
return (
obj.user.useraccount.avatar_url
if hasattr(obj.user, "useraccount")
else None
)

class Meta:
model = Token
Expand Down
5 changes: 4 additions & 1 deletion docker-app/qfieldcloud/core/views/auth_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ def post(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
user = serializer.validated_data["user"]
token, created = Token.objects.get_or_create(user=user)
avatar_url = (
user.useraccount.avatar_url if hasattr(user, "useraccount") else None
)
return Response(
{
"token": token.key,
"username": user.username,
"email": user.email,
"avatar_url": user.useraccount.avatar_url,
"avatar_url": avatar_url,
}
)

0 comments on commit 9a0beb2

Please sign in to comment.