Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid user model access on import on django 1.7 #28

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions user_sessions/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import django
from django.contrib import admin
from django.utils.timezone import now
from django.utils.translation import ugettext_lazy as _
Expand All @@ -6,8 +7,9 @@
from django.contrib.auth import get_user_model
except ImportError:
from django.contrib.auth.models import User
else:
User = get_user_model()

def get_user_model():
return User

from user_sessions.templatetags.user_sessions import device, location

Expand Down Expand Up @@ -47,12 +49,18 @@ def queryset(self, request, queryset):

class SessionAdmin(admin.ModelAdmin):
list_display = 'ip', 'user', 'is_valid', 'location', 'device',
search_fields = (
'ip',
'user__%s' % getattr(User, 'USERNAME_FIELD', 'username'),
)
search_fields = []
list_filter = ExpiredFilter, OwnerFilter

def __init__(self, *args, **kwargs):
super(SessionAdmin, self).__init__(*args, **kwargs)
if not self.search_fields and django.VERSION[:2] < (1, 7):
self.search_fields = self.get_search_fields(None)

def get_search_fields(self, request):
User = get_user_model()
return ('ip', 'user__%s' % getattr(User, 'USERNAME_FIELD', 'username'))

def is_valid(self, obj):
return obj.expire_date > now()
is_valid.boolean = True
Expand All @@ -62,4 +70,5 @@ def location(self, obj):

def device(self, obj):
return device(obj.user_agent)

admin.site.register(Session, SessionAdmin)