Commit 2aad3cd1 by Dan Krause

Added role and ACL support

parent 1f235380
......@@ -12,7 +12,7 @@ parse_rest
- Cloud code integration
- Installation querying
- push
- **PLANNED/TODO**: Roles/ACLs**
- Roles/ACLs**
- **PLANNED/TODO**: Image/File type support
......
......@@ -32,15 +32,20 @@ class ParseType(object):
type_mapping = {}
@staticmethod
def convert_from_parse(parse_data):
def convert_from_parse(parse_key, parse_data):
is_parse_type = isinstance(parse_data, dict) and '__type' in parse_data
parse_type = None
if isinstance(parse_data, dict):
if '__type' in parse_data:
parse_type = parse_data.pop('__type')
elif parse_key == 'ACL':
parse_type = 'ACL'
# if its not a parse type -- simply return it. This means it wasn't a "special class"
if not is_parse_type:
if not parse_type:
return parse_data
native = ParseType.type_mapping.get(parse_data.pop('__type'))
native = ParseType.type_mapping.get(parse_type)
return native.from_native(**parse_data) if native else parse_data
@staticmethod
......@@ -60,7 +65,7 @@ class ParseType(object):
ParseResource: Pointer
}
if hasattr(python_object, '__iter__') and not isinstance(python_object, str):
if hasattr(python_object, '__iter__') and not isinstance(python_object, (basestring, ParseType)):
# It's an iterable? Repeat this whole process on each object
return [ParseType.convert_to_parse(o, as_pointer=as_pointer)
for o in python_object]
......@@ -203,6 +208,54 @@ class File(ParseType):
_absolute_url = property(lambda self: self._api_url)
@complex_type()
class ACL(ParseType):
@classmethod
def from_native(cls, **kw):
return cls(kw)
def __init__(self, acl):
self._acl = acl
def _to_native(self):
return self._acl
def __repr__(self):
return '%s(%s)' % (type(self).__name__, repr(self._acl))
def set_default(self, read=False, write=False):
self._set_permission("*", read, write)
def set_role(self, role, read=False, write=False):
if isinstance(role, ParseResource):
self._set_permissions("role:%s" % role.name, read, write)
else:
self._set_permissions("role:%s" % role, read, write)
def set_user(self, user, read=False, write=False):
if isinstance(user, ParseResource):
self._set_permission(user.objectId, read, write)
else:
self._set_permission(user, read, write)
def set_all(self, permissions):
self._acl.clear()
for k, v in permissions.items():
self._set_permission(k, **v)
def _set_permission(self, name, read=False, write=False):
permissions = {}
if read is True:
permissions["read"] = True
if write is True:
permissions["write"] = True
if len(permissions):
self._acl[name] = permissions
else:
self._acl.pop(name, None)
class Function(ParseBase):
ENDPOINT_ROOT = '/'.join((API_ROOT, 'functions'))
......@@ -236,7 +289,7 @@ class ParseResource(ParseBase):
def _init_attrs(self, args):
for key, value in six.iteritems(args):
setattr(self, key, ParseType.convert_from_parse(value))
setattr(self, key, ParseType.convert_from_parse(key, value))
self._is_loaded = True
def _to_native(self):
......
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from parse_rest.connection import API_ROOT
from parse_rest.datatypes import ParseResource
from parse_rest.query import QueryManager
class Role(ParseResource):
'''
A Role is like a regular Parse object (can be modified and saved) but
it requires additional methods and functionality
'''
ENDPOINT_ROOT = '/'.join([API_ROOT, 'roles'])
@property
def className(self):
return '_Role'
def __repr__(self):
return '<Role:%s (Id %s)>' % (getattr(self, 'name', None), self.objectId)
Role.Query = QueryManager(Role)
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