Skip to content

Commit

Permalink
Changed get_avatar_id_from_user_id() to get_avatar_id_from_user() (#779)
Browse files Browse the repository at this point in the history
* Getting user object from user id

* Stuff

* Fixed test

* Fixed indentation

* Indentation
  • Loading branch information
riaJha97 committed Sep 3, 2018
1 parent 921df4c commit c0b0001
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 10 deletions.
10 changes: 5 additions & 5 deletions aimmo/game_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

from django.shortcuts import render, get_object_or_404
from django.contrib.auth.models import User
from aimmo import app_settings, exceptions
from models import Game

Expand Down Expand Up @@ -55,19 +56,18 @@ def _connection_on_k8s_mode(game_port):
return game_port == 0


def get_avatar_id_from_user_id(user_id, game_id):
def get_avatar_id_from_user(user, game_id):
"""
A helper function which will return an avatar ID that is assigned to a
particular django user owner in a game.
:param user_id: A django user ID taken from the request.
:param user: A django user object taken from the request.
:param game_id: The game ID in which the avatar is being requested from.
:return: An integer containing the avatar_ID.
"""
game = get_object_or_404(Game, id=game_id)

if not game.can_user_play(user_id):
if not game.can_user_play(user):
raise exceptions.UserCannotPlayGameException

avatar = game.avatar_set.get(owner=user_id)
avatar = game.avatar_set.get(owner=user.id)

return avatar.id
1 change: 0 additions & 1 deletion aimmo/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ def test_current_avatar_api_for_non_existant_game(self):

def test_current_avatar_api_for_unauthorised_games(self):
self.game.public = False
self.game.can_play = [self.user]
self.game.save()
c = self.login()
response = c.get(reverse('aimmo/current_avatar_in_game', kwargs={'game_id': 1}))
Expand Down
6 changes: 2 additions & 4 deletions aimmo/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ def connection_parameters(request, game_id):
env_connection_settings = game_renderer.get_environment_connection_settings(game_id)

try:
avatar_id = game_renderer.get_avatar_id_from_user_id(user_id=request.user.id,
game_id=game_id)
avatar_id = game_renderer.get_avatar_id_from_user(user=request.user, game_id=game_id)
except UserCannotPlayGameException:
LOGGER.warning('HTTP 401 returned. User {} unauthorised to play.'.format(request.user.id))
return HttpResponse('User unauthorized to play',
Expand Down Expand Up @@ -196,8 +195,7 @@ def add_game(request):
def current_avatar_in_game(request, game_id):

try:
avatar_id = game_renderer.get_avatar_id_from_user_id(user_id=request.user.id,
game_id=game_id)
avatar_id = game_renderer.get_avatar_id_from_user(user=request.user, game_id=game_id)
except UserCannotPlayGameException:
LOGGER.warning('HTTP 401 returned. User {} unauthorised to play.'.format(request.user.id))
return HttpResponse('User unauthorized to play',
Expand Down

0 comments on commit c0b0001

Please sign in to comment.