144
cache=data.content.decode('utf-8'))
return data
def patch_list(self, request, **kwargs):
project__slug = request.GET['project__slug']
gc, created = GlobalCache.objects.get_or_create(
key=CacheKeys.PROJECT_PERMS % project__slug,
defaults={'cache': json.dumps({'objects': [],'meta': {'total_count':
0,'limit': 0,'offset': 0}})}
)
cache = json.loads(gc.cache)
perms = json.loads(request.body.decode('utf-8'))
perms_shortly = [{key: value for key, value in perm.items() if key !=
'layerPermissions'} for perm in perms['objects']]
template = {'deleted_objects': perms['deleted_objects'], 'objects': []}
if cache['objects'] != perms_shortly:
ids_new = [perm['id'] for perm in perms['objects'] if 'id' in perm]
ids_exists = [perm['id'] for perm in cache['objects']]
ids_added = list(set(ids_new) - set(ids_exists))
template['objects'] = [perm for perm in perms['objects'] if 'id' not in
perm or perm['id'] in ids_added]
ids_detail_check = list(set(ids_new) - set(ids_added))
for i, perm in enumerate(perms_shortly):
if perm.get('id') in ids_detail_check and perm !=
gc.service[str(perm['id'])]:
template['objects'].append(perms['objects'][i])
kwargs['body'] = json.dumps(template).encode('utf-8')
return TastyPiePatchList.patch_list(self, request, **kwargs)
def obj_create(self, bundle, **kwargs):
bundle = super(ProjectPermissionResource, self).obj_create(bundle, **kwargs)
project__slug = bundle.request.GET['project__slug']
GlobalCache.objects.get_or_create(
key=CacheKeys.PROJECT_PERMS % project__slug,
defaults={'cache': json.dumps({'objects': [],'meta': {'total_count':
0,'limit': 0,'offset': 0}})})[0]\
.project_perms_add(bundle.data, bundle.obj.id)
return bundle
def obj_update(self, bundle, skip_errors=False, **kwargs):
bundle = super(ProjectPermissionResource, self).obj_update(bundle, skip_errors,
**kwargs)
project__slug = bundle.request.GET['project__slug']
GlobalCache.objects.get(key=CacheKeys.PROJECT_PERMS %
project__slug).project_perm_change(bundle.data)
return bundle
def obj_delete(self, bundle, **kwargs):
project__slug = bundle.request.GET['project__slug']
GlobalCache.objects.get(key=CacheKeys.PROJECT_PERMS %
project__slug).project_perms_del(bundle.obj.id)
return super(ProjectPermissionResource, self).obj_delete(bundle, **kwargs)
class ProjectLayerPermissionResource(ModelResource):
id = IntegerField(attribute='id')
type = CharField(attribute='type')
layer = fields.ForeignKey(LayerTreeResource, 'layer', full=False)
project_permission = fields.ForeignKey(ProjectPermissionResource,
'project_permission', full=False)