Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
ParsePy
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenEdx
ParsePy
Commits
9791f2f4
Commit
9791f2f4
authored
Sep 26, 2011
by
Paul Kastner
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feature-complete
parent
5a859b02
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
101 additions
and
29 deletions
+101
-29
__init__.py
+101
-29
No files found.
__init__.py
View file @
9791f2f4
...
...
@@ -2,6 +2,7 @@ import urllib, urllib2
import
base64
import
json
import
datetime
import
collections
API_ROOT
=
'https://api.parse.com/1/classes'
...
...
@@ -12,6 +13,7 @@ MASTER_KEY = ''
class
ParseBinaryDataWrapper
(
str
):
pass
class
ParseBase
(
object
):
def
_executeCall
(
self
,
uri
,
http_verb
,
data
=
None
):
url
=
API_ROOT
+
uri
...
...
@@ -39,14 +41,29 @@ class ParseBase(object):
return
date
class
ParseObject
(
ParseBase
):
def
__init__
(
self
,
class_name
):
def
__init__
(
self
,
class_name
,
attrs_dict
=
None
):
self
.
_class_name
=
class_name
self
.
_object_id
=
None
self
.
_updated_at
=
None
self
.
_created_at
=
None
if
attrs_dict
:
self
.
_populateFromDict
(
attrs_dict
)
def
_populateFromDict
(
self
,
attrs_dict
):
self
.
_object_id
=
attrs_dict
[
'objectId'
]
self
.
_created_at
=
self
.
_ISO8601ToDatetime
(
attrs_dict
[
'createdAt'
])
self
.
_updated_at
=
self
.
_ISO8601ToDatetime
(
attrs_dict
[
'updatedAt'
])
del
attrs_dict
[
'objectId'
]
del
attrs_dict
[
'createdAt'
]
del
attrs_dict
[
'updatedAt'
]
attrs_dict
=
dict
(
map
(
self
.
_convertFromParseType
,
attrs_dict
.
items
()))
self
.
__dict__
.
update
(
attrs_dict
)
def
objectId
(
self
):
return
self
.
_object_id
...
...
@@ -88,6 +105,23 @@ class ParseObject(ParseBase):
return
(
key
,
value
)
def
_convertFromParseType
(
self
,
prop
):
key
,
value
=
prop
if
type
(
value
)
==
dict
and
value
.
has_key
(
'__type'
):
if
value
[
'__type'
]
==
'Pointer'
:
value
=
ParseQuery
(
value
[
'className'
])
.
get
(
value
[
'objectId'
])
elif
value
[
'__type'
]
==
'Date'
:
value
=
self
.
_ISO8601ToDatetime
(
value
[
'iso'
])
elif
value
[
'__type'
]
==
'Bytes'
:
value
=
ParseBinaryDataWrapper
(
base64
.
b64decode
(
value
[
'base64'
]))
else
:
raise
Exception
(
'Invalid __type.'
)
return
(
key
,
value
)
def
_getJSONProperties
(
self
):
properties_list
=
self
.
__dict__
.
items
()
...
...
@@ -132,45 +166,83 @@ class ParseObject(ParseBase):
class
ParseQuery
(
ParseBase
):
def
__init__
(
self
,
class_name
):
self
.
class_name
=
class_name
self
.
_class_name
=
class_name
self
.
_where
=
collections
.
defaultdict
(
dict
)
self
.
_options
=
{}
self
.
_object_id
=
''
def
get
(
self
,
object_id
):
# URL: /1/classes/<className>/<objectId>
# HTTP Verb: GET
def
eq
(
self
,
name
,
value
):
self
.
_where
[
name
]
=
value
return
self
uri
=
'/
%
s/
%
s'
%
(
self
.
class_name
,
object_id
)
# It's tempting to generate the comparison functions programatically,
# but probably not worth the decrease in readability of the code.
def
lt
(
self
,
name
,
value
):
self
.
_where
[
name
][
'$lt'
]
=
value
return
self
response_dict
=
self
.
_executeCall
(
uri
,
'GET'
)
def
lte
(
self
,
name
,
value
):
self
.
_where
[
name
][
'$lte'
]
=
value
return
self
new_parse_obj
=
ParseObject
(
self
.
class_name
)
new_parse_obj
.
_object_id
=
response_dict
[
'objectId'
]
new_parse_obj
.
_created_at
=
self
.
_ISO8601ToDatetime
(
response_dict
[
'createdAt'
])
new_parse_obj
.
_updated_at
=
self
.
_ISO8601ToDatetime
(
response_dict
[
'updatedAt'
])
def
gt
(
self
,
name
,
value
):
self
.
_where
[
name
][
'$gt'
]
=
value
return
self
del
response_dict
[
'objectId'
]
del
response_dict
[
'createdAt'
]
del
response_dict
[
'updatedAt'
]
def
gte
(
self
,
name
,
value
):
self
.
_where
[
name
][
'$gte'
]
=
value
return
self
response_dict
=
dict
(
map
(
self
.
_convertFromParseType
,
response_dict
.
items
()))
def
ne
(
self
,
name
,
value
):
self
.
_where
[
name
][
'$ne'
]
=
value
return
self
new_parse_obj
.
__dict__
.
update
(
response_dict
)
def
order
(
self
,
order
,
decending
=
False
):
# add a minus sign before the order value if decending == True
self
.
_options
[
'order'
]
=
decending
and
(
'-'
+
order
)
or
order
return
self
return
new_parse_obj
def
limit
(
self
,
limit
):
self
.
_options
[
'limit'
]
=
limit
return
self
def
_convertFromParseType
(
self
,
prop
):
key
,
value
=
prop
def
skip
(
self
,
skip
):
self
.
_options
[
'skip'
]
=
skip
return
self
if
type
(
value
)
==
dict
and
value
.
has_key
(
'__type'
):
if
value
[
'__type'
]
==
'Pointer'
:
value
=
ParseQuery
(
value
[
'className'
])
.
get
(
value
[
'objectId'
])
elif
value
[
'__type'
]
==
'Date'
:
value
=
self
.
_ISO8601ToDatetime
(
value
[
'iso'
])
elif
value
[
'__type'
]
==
'Bytes'
:
value
=
ParseBinaryDataWrapper
(
base64
.
b64decode
(
value
[
'base64'
]))
def
get
(
self
,
object_id
):
self
.
_object_id
=
object_id
return
self
.
_fetch
(
single_result
=
True
)
def
fetch
(
self
):
# hide the single_result param of the _fetch method from the library user
# since it's only useful internally
return
self
.
_fetch
()
def
_fetch
(
self
,
single_result
=
False
):
# URL: /1/classes/<className>/<objectId>
# HTTP Verb: GET
if
self
.
_object_id
:
uri
=
'/
%
s/
%
s'
%
(
self
.
_class_name
,
object_id
)
else
:
raise
Exception
(
'Invalid __type.'
)
options
=
dict
(
self
.
_options
)
# make a local copy
if
self
.
_where
:
# JSON encode WHERE values
where
=
json
.
dumps
(
self
.
_where
)
options
.
update
({
'where'
:
where
})
uri
=
'/
%
s?
%
s'
%
(
self
.
_class_name
,
urllib
.
urlencode
(
options
))
response_dict
=
self
.
_executeCall
(
uri
,
'GET'
)
if
single_result
:
return
ParseObject
(
self
.
_class_name
,
response_dict
)
else
:
return
[
ParseObject
(
self
.
_class_name
,
result
)
for
result
in
response_dict
[
'results'
]]
return
(
key
,
value
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment