Skip to content

Commit

Permalink
mypy issues with get_user_model
Browse files Browse the repository at this point in the history
  • Loading branch information
pyepye committed Jul 13, 2023
1 parent a481b72 commit a21b841
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
29 changes: 22 additions & 7 deletions amplitude/amplitude.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import logging
import time
from typing import Any, Dict, List
Expand Down Expand Up @@ -174,14 +176,24 @@ def user_properties_from_request(self, request: HttpRequest) -> dict:

user_data = {
'username': user.get_username(),
'email': user.email,
'full_name': user.get_full_name(),
'is_staff': user.is_staff,
'is_superuser': user.is_superuser,
}
if user.last_login:

if hasattr(user, 'email') and user.email:
user_data['email'] = user.email
if hasattr(user, 'full_name') and user.full_name:
get_full_name = getattr(user, "get_full_name")
if get_full_name and callable(get_full_name):
user_data['full_name'] = user.get_full_name() # type: ignore
else:
user_data['full_name'] = user.full_name
if hasattr(user, 'is_staff') and user.is_staff:
user_data['is_staff'] = user.is_staff
if hasattr(user, 'is_superuser') and user.is_superuser:
user_data['is_superuser'] = user.is_superuser

if hasattr(user, 'last_login') and user.last_login:
user_data['last_login'] = user.last_login.isoformat()
if user.date_joined:
if hasattr(user, 'date_joined') and user.date_joined:
user_data['date_joined'] = user.date_joined.isoformat()
return user_data

Expand All @@ -196,7 +208,10 @@ def group_from_request(self, request: HttpRequest) -> list:

User = get_user_model()
user = User.objects.get(pk=request.user.pk)
groups = user.groups.all().values_list('name', flat=True)

groups = []
if hasattr(user, 'groups'):
groups = user.groups.all().values_list('name', flat=True)
return list(groups)

def location_data_from_ip_address(self, ip_address: str) -> dict:
Expand Down
6 changes: 3 additions & 3 deletions amplitude/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
if not API_KEY:
raise ImproperlyConfigured('"AMPLITUDE_API_KEY" is not set')

INCLUDE_USER_DATA = getattr(settings, 'AMPLITUDE_INCLUDE_USER_DATA', False) # NOQA: E501
INCLUDE_GROUP_DATA = getattr(settings, 'AMPLITUDE_INCLUDE_GROUP_DATA', False) # NOQA: E501
INCLUDE_USER_DATA: bool = getattr(settings, 'AMPLITUDE_INCLUDE_USER_DATA', False) # NOQA: E501
INCLUDE_GROUP_DATA: bool = getattr(settings, 'AMPLITUDE_INCLUDE_GROUP_DATA', False) # NOQA: E501
IGNORE_URLS = getattr(settings, 'AMPLITUDE_IGNORE_URLS', [])
if not isinstance(IGNORE_URLS, list):
error = '"AMPLITUDE_IGNORE_URLS" must be a list of URLs or URL names'
raise ImproperlyConfigured(error)

MIN_ID_LENGTH = getattr(settings, 'AMPLITUDE_MIN_ID_LENGTH', None)
MIN_ID_LENGTH: int | None = getattr(settings, 'AMPLITUDE_MIN_ID_LENGTH', None)
if MIN_ID_LENGTH and not isinstance(MIN_ID_LENGTH, int):
raise ImproperlyConfigured('"AMPLITUDE_MIN_ID_LENGTH" must be an integer')

Expand Down
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

SECRET_KEY = 'amplitude-test'
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
AUTH_USER_MODEL = "authentication.User"

SESSION_ENGINE = 'django.contrib.sessions.backends.signed_cookies'
SESSION_COOKIE_HTTPONLY = True
Expand Down

0 comments on commit a21b841

Please sign in to comment.