Ejemplo n.º 1
0
	def view( self, request ):
		
		# check if user with the username exists...

		username = request.POST[ 'username' ]

		try:
			user_profile = UserProfile.objects.get( fb_id = request.POST[ 'id' ] )
		except UserProfile.DoesNotExist:
			fb_username = get_fb_username( username )
			user = User.objects.create_user( username = fb_username, password = _DEFAULT_PASS )
			user.first_name = request.POST[ 'first_name' ]
			user.last_name = request.POST[ 'last_name' ]
			user.save()
			user_profile = UserProfile( user = user )
			user_profile.fb_id = request.POST[ 'id' ]
			user_profile.save()
			UserVirtMoney.init_start( user )

			# upload photo
			f = urllib2.urlopen( "https://graph.facebook.com/{0}/picture?type=large".format( username ) )
			# read the photo
			content = f.read()
			f = EditForm( user, {}, { 'image': SimpleUploadedFile( "filename", content ) }, instance = user_profile )
			if f.is_valid():
				f.save()

			link = '/registration-complete/'
		else:			
			user = user_profile.user
			link = '/'

		user.backend = 'django.contrib.auth.backends.ModelBackend'
		login( request, user )
		
		return super( AuthFbView, self ).view( request, { 'link': link } )
		
Ejemplo n.º 2
0
	def view( self, request ):
		get = request.GET
		
		if 'error' in get: 
			if get[ 'error' ] == "access_denied" and 'error_reason' in get and get[ 'error_reason' ] == "user_denied":
				return super( AuthVkView, self ).view( request, { 'action': "cancel" } )
			else:
				raise AuthVkPoint1Error( "get={0}".format( get ) )
		elif 'code' in get:
			code = get[ 'code' ]
			f = urllib2.urlopen( "https://oauth.vk.com/access_token?client_id={0}&client_secret={1}&code={2}&redirect_uri=http://{3}/auth/vk".format( VK_ID, VK_KEY, code, DOMAIN ) )
			result = json.loads( f.read() )

			if 'error' in result:
				raise AuthVkPoint2Error( "result={0}".format( result ) )

			access_token = result[ 'access_token' ]
			user_id = result[ 'user_id' ]
			try:
				user_profile = UserProfile.objects.get( vk_id = user_id )
				user = user_profile.user
				# the next line: http://stackoverflow.com/questions/2787650/manually-logging-in-a-user-without-password
				user.backend = 'django.contrib.auth.backends.ModelBackend'
				login( request, user )
				return super( AuthVkView, self ).view( request, { 'action': "login" } )
			except UserProfile.DoesNotExist:			
				f = urllib2.urlopen( "https://api.vk.com/method/users.get?uids={0}&fields=first_name,last_name,photo_big,nickname&access_token={1}".format( user_id, access_token ) )
				result = json.loads( f.read() )

				if 'error' in result:
					raise AuthVkPoint3Error( "result={0}".format( result ) )

				info = result[ 'response' ][ 0 ]
				first_name = info[ 'first_name' ]
				last_name = info[ 'last_name' ]
				user = _create_user_if_does_not_exist( _get_vk_username( user_id ), _DEFAULT_PASS )
				
				user.first_name = first_name
				user.last_name = last_name
				user.save()				

				# now image does not load
				
				user_profile = UserProfile( user = user )
				user_profile.vk_id = user_id
				user_profile.save()

				try:
					user_virt_money = UserVirtMoney.objects.get( user = user )
				except UserVirtMoney.DoesNotExist:
					UserVirtMoney.init_start( user )

				if 'photo_big' in info and info[ 'photo_big' ] != "":
					f = urllib2.urlopen( info[ 'photo_big' ] )
					# read the photo
					content = f.read()
					f = EditForm( user, {}, { 'image': SimpleUploadedFile( "filename", content ) }, instance = user_profile )
					if f.is_valid():
						f.save()

				user.backend = 'django.contrib.auth.backends.ModelBackend'
				login( request, user )

				return super( AuthVkView, self ).view( request, { 'action': "register" } )