def api_search(request, resource_name): if request.method != 'POST': return response_403() # Setup model = get_resource_model(resource_name) data = json.loads(request.body) search_params = data['search_params'] #meta_data = data.get('meta', {}) #full_user = _make_fulluser(request, meta_data) # Validate search params # search_params must be a list of [field_name, match_type, value] tuples. # for example, 'username', 'iexact', 'bobRoss' for field_name, match_type, value in search_params: if not permissions.is_allowed_search(model, field_name, match_type): return response_403() # Execute search kwargs = {field_name + '__' + match_type: value for field_name, match_type, value in search_params} qs = model.objects.filter(**kwargs) # Build and return result fields = permissions.get_model_default_readable_fields(model) result_data = [object_to_dict(m, fields) for m in qs] return HttpJsonResponse(json.dumps(result_data, cls=APIEncoder))
def send_page_update(page, socket_id): """Send page create/update notification through Pusher.""" model = page.__class__ pusher_fields = permissions.get_model_default_readable_fields(model) dct = object_to_dict(page, pusher_fields) pusher_helpers.try_pusher_page_send( page.id, 'page-update', dct, socket_id)
def send_item_create_or_update(item, event_name, socket_id, full_user): """Send item create/update notification through Pusher.""" model = item.__class__ pusher_fields = permissions.get_model_default_readable_fields(model) modelDct = object_to_dict(item, pusher_fields) dct = {} # Client JS needs 'type' field dct['type'] = item.SHORTNAME # Lets keep a copy of the id at top level dct['id'] = item.id dct['creator_identifier'] = get_user_identifier(full_user, item.page_id) if item.creator_id: creator = User.objects.get(id=item.creator_id) dct['creator_username'] = creator.username else: dct['creator_username'] = None dct['model_data'] = modelDct pusher_helpers.try_pusher_page_send( item.page_id, event_name, dct, socket_id)
def serialize_object(obj, field_names): dct = object_to_dict(obj, field_names) return json.dumps(dct, cls=APIEncoder)