コード例 #1
0
ファイル: views.py プロジェクト: kevinfiol/djangosteam
def profile(request, form_id):
	api 		= SteamAPI()
	user_id 	= retrieve_steam_id(form_id, api)
	api.user_id = user_id
	
	try:
		user = SteamUser.objects.get(steamid=user_id)
		user.update_profile(api.user_call('user'))
		user.update_games(api.user_call(mode='game', include_played_free_games=1))
		user.update_appid_list()
		user.save()

	except SteamUser.DoesNotExist:
		user = SteamUser()
		user.update_profile(api.user_call('user'))
		user.update_games(api.user_call(mode='game', include_played_free_games=1))
		user.update_appid_list()
		user.save()

	except:
		raise
		return HttpResponseRedirect(reverse('gamelist:index'))

	finally:
		#Declare / Initialize User Statistics
		user_stats = {}
		user_stats['account_worth'] 		= float(0)
		user_stats['playtimes'] 			= user.get_playtimes()
		user_stats['total_playtime'] 		= user.get_total_playtime()
		user_stats['unplayed_game_count'] 	= user.get_unplayed_game_count()

		#Declare / Initialize Game List
		allGames 		= []
		newGames 		= []
		existingGames 	= Game.objects.filter(steam_appid__in=user.appid_list)
		newIds 			= set(user.appid_list) - set(game.steam_appid for game in existingGames)
		
		#If there are new games to be added to database
		if len(newIds) != 0:
			#Filter out InvalidApp from newIds
			invalidIds = InvalidApp.objects.filter(steam_appid__in=newIds)
			newIds = newIds - set(game.steam_appid for game in invalidIds)

			#Loop to add new games to DB
			for newId in newIds:
				try:
					print "Game Does Not Exist, %s. Adding to Database." % newId
					data = api.store_call(interface='appdetails', appids=newId)
					newGame = Game()
					newGame.update_game(newId, data)
					newGame.update_igdb_data()
					newGame.save()
					newGames.append(newGame)
				except:
					pass

		#Create List of Up-To-Date Games
		for game in chain(existingGames, newGames):
			try:
				if len(game.price_overview) != 0:
					user_stats['account_worth'] += game.price_overview['initial']		
				allGames.append(game)
			except(TypeError):
				pass

		#sort game for table
		allGames.sort(key=lambda x: x.name.lower())
		
		#pagination
		paginator = Paginator(allGames, 50)
		page = request.GET.get('page')
		try:
			games_list = paginator.page(page)
		except PageNotAnInteger:
			games_list = paginator.page(1)
		except EmptyPage:
			games_list = paginator.page(paginator.num_pages)

		return render(request, 'gamelist/profile.html', {
			#User Data and List
			'steam_id': api.user_id,
			'user': user,
			'games_list': games_list,
			#User Stats
			'account_worth': user_stats['account_worth']/100,
			'playtimes': user_stats['playtimes'],
			'total_playtime': round(user_stats['total_playtime']/60, 2),
			'unplayed_games': user_stats['unplayed_game_count']
		})