Skip to content

Commit

Permalink
authome.views: replace queryset 0-indexing with first()
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Percival committed Jul 27, 2017
1 parent 49b9b8e commit 87e7fa1
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions authome/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@

def force_email(username):
if username.find("@") == -1:
candidates = User.objects.filter(
username__iexact=username)
if not candidates:
return None
return candidates[0].email
candidate = User.objects.filter(
username__iexact=username).first()
return candidate.email if candidate else None
return username


Expand Down Expand Up @@ -50,18 +48,15 @@ def adal_authenticate(email, password):
except adal.adal_error.AdalError:
return None

candidates = User.objects.filter(email__iexact=token['userId'])
if candidates.exists():
return candidates[0]
else:
return None
candidates = User.objects.filter(email__iexact=token['userId']).first()
return candidates


def shared_id_authenticate(email, shared_id):
us = UserSession.objects.filter(user__email__iexact=email).order_by('-session__expire_date')
if (not us.exists()) or (us[0].shared_id != shared_id):
us = UserSession.objects.filter(user__email__iexact=email).order_by('-session__expire_date').first()
if (not us) or (us.shared_id != shared_id):
return None
return us[0].user
return us.user


@csrf_exempt
Expand Down Expand Up @@ -127,17 +122,16 @@ def auth_ip(request):
if request.user.is_authenticated():
return auth(request)

headers = {'client_logon_ip': current_ip}

# We can assume that the Session and UserSession tables only contain
# current sessions.
qs = UserSession.objects.filter(
session__isnull=False,
ip=current_ip).order_by("-session__expire_date")

headers = {'client_logon_ip': current_ip}
usersession = UserSession.objects.filter(
session__isnull=False,
ip=current_ip).order_by("-session__expire_date").first()

if qs.exists():
user = qs[0].user
headers["email"] = user.email
if usersession:
headers["email"] = usersession.user.email

response = HttpResponse(json.dumps(headers), content_type='application/json')
for key, val in headers.items():
Expand Down

0 comments on commit 87e7fa1

Please sign in to comment.