Commit 6c57fa86 by David Robinson

Merge pull request #90 from deanq/master

User.current_user() when a valid session token is provided
parents 8a811dba 2885b5a8
...@@ -54,8 +54,11 @@ in the app and may accidentally replace or change existing objects. ...@@ -54,8 +54,11 @@ in the app and may accidentally replace or change existing objects.
You can then test the installation by running the following command: You can then test the installation by running the following command:
python -m 'parse_rest.tests' # test all
python -m unittest parse_rest.tests
# or test individually
python -m unittest parse_rest.tests.TestObject.testCanCreateNewObject
Usage Usage
----------- -----------
...@@ -391,6 +394,26 @@ u.save() ...@@ -391,6 +394,26 @@ u.save()
u.delete() u.delete()
~~~~~ ~~~~~
To get the current user from a Parse session:
~~~~~ {python}
from parse_rest.connection import SessionToken, register
# Acquire a valid parse session somewhere
# Example: token = request.session.get('session_token')
# Method 1: Using a `with` statement
# Do this to isolate use of session token in this block only
with SessionToken(token):
me = User.current_user()
# Method 2: register your parse connection with `session_token` parameter
# Do this to use the session token for all subsequent queries
register(PARSE_APPID, PARSE_APIKEY, session_token=token)
me = User.current_user()
~~~~~
Push Push
--------------- ---------------
......
...@@ -507,6 +507,21 @@ class TestUser(unittest.TestCase): ...@@ -507,6 +507,21 @@ class TestUser(unittest.TestCase):
g.save() g.save()
self.assertEqual(1, len(Game.Query.filter(creator=user))) self.assertEqual(1, len(Game.Query.filter(creator=user)))
def testCanGetCurrentUser(self):
user = User.signup(self.username, self.password)
self.assertIsNotNone(user.sessionToken)
register(
getattr(settings_local, 'APPLICATION_ID'),
getattr(settings_local, 'REST_API_KEY'),
session_token=user.sessionToken
)
current_user = User.current_user()
self.assertIsNotNone(current_user)
self.assertEqual(current_user.sessionToken, user.sessionToken)
self.assertEqual(current_user.username, user.username)
class TestPush(unittest.TestCase): class TestPush(unittest.TestCase):
""" """
......
...@@ -91,6 +91,11 @@ class User(ParseResource): ...@@ -91,6 +91,11 @@ class User(ParseResource):
login_url = User.ENDPOINT_ROOT login_url = User.ENDPOINT_ROOT
return cls(**User.POST(login_url, authData=auth)) return cls(**User.POST(login_url, authData=auth))
@classmethod
def current_user(cls):
user_url = '/'.join([API_ROOT, 'users/me'])
return cls(**User.GET(user_url))
@staticmethod @staticmethod
def request_password_reset(email): def request_password_reset(email):
'''Trigger Parse\'s Password Process. Return True/False '''Trigger Parse\'s Password Process. Return True/False
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment