def testCheckPurchase(self): # puchased purchase = Purchase.checkPurchase(game_id=4, user_id=1) self.assertEqual(purchase.purchase_user.id, 1, 'Checking if a user has purchased a game') # not purchased purchase2 = Purchase.checkPurchase(game_id=4, user_id=3) self.assertEqual(purchase2, False, 'Checking if a user has purchased a game') # user doesn't exist purchase3 = Purchase.checkPurchase(game_id=4, user_id=222) self.assertEqual(purchase3, False, 'Checking if a user has purchased a game') # game doesn't exist purchase4 = Purchase.checkPurchase(game_id=222, user_id=1) self.assertEqual(purchase4, False, 'Checking if a user has purchased a game')
def get(self, request, game_id): """ Returns the info for a game, including info on the user that created the request and purchase link if the user did not purchase the game """ try: game = Game.objects.get(pk=game_id) categories = game.getCategories() scores = game.getHighScore() # check if user has login & usergroup if_login = request.user.is_authenticated if_dev = if_login and request.user.id is game.author.id if_purchase = if_login and Purchase.checkPurchase( game_id, request.user.id) highscore = None rank = None history = None bill = None if if_purchase: highscore = game.getUserHighScore(request.user.id) rank = game.getUserRank(request.user.id) history = game.getUserHistory(request.user.id) elif if_login: try: sid = "pandashop" pid = str(game.id) + "!" + str(request.user.id) secret_key = "583e35f04573f59158691b6e21f869dc" checksumstr = "pid={}&sid={}&amount={}&token={}".format( pid, sid, game.price, secret_key) m = md5(checksumstr.encode("ascii")) bill = { 'pid': pid, 'sid': sid, 'checksum': m.hexdigest(), } except (TypeError, NameError) as e: if isPC(request): return Response({'message': str(e)}, template_name='gameinfo.html') else: return Response(rest({'message': str(e)}), status=403) user_status = { 'if_dev': if_dev, 'if_purchase': if_purchase, 'record': { 'highscore': highscore, 'rank': rank, 'history': history } } data = { 'game': game, 'categories': categories, 'scores_list': scores, 'user_status': user_status, 'bill': bill } except Game.DoesNotExist: data = None if isPC(request): return Response(data, template_name='gameinfo.html') else: data = { 'game': game, 'categories': categories, 'scores_list': scores, 'user_status': user_status, } print(data) return Response(rest(data)) if isPC(request): return Response(data, template_name='gameinfo.html') else: data = { 'game': game, 'categories': categories, 'scores_list': scores, 'user_status': user_status, } return Response(rest(data))