def batch_images(context, request, get_image_info=get_image_info, # unittest get_images_batch=get_images_batch): # unittest # Find query parameters based on the 'source' param, # which signifies the selection index of the source button # in the imagedrawer dialog. source = int(request.params.get('source', '0')) if source == 0: # My Recent creator = 'admin' community_path = None elif source == 1: # This Community creator = None community = find_community(context) # batching api requires the community path community_path = model_path(community) else: # All Karl creator = None community_path = None # batching # Decide start and size here, don't let the lower levels # apply their default. This allows us to enforce # a MINIMAL_BATCH size. batch_start = int(request.params.get('start', '0')) batch_size = max(int(request.params.get('limit', '0')), MINIMAL_BATCH) # there is a minimal batch size to enforce, if the client # does not ask for one # Just pass the values to lower levels where sensible # defaults will be applied. sort_index = request.params.get('sort_on', None) reverse = request.params.get('reverse', None) search_params = dict( creator=creator, community=community_path, batch_start=batch_start, batch_size=batch_size, ) if sort_index: search_params['sort_index'] = sort_index if reverse: search_params['reverse'] = bool(int(reverse)) batch_info = get_images_batch( context, request, **search_params ) records = [get_image_info(image, request) for image in batch_info['entries']] return dict( records=records, start=batch_info['batch_start'], totalRecords=batch_info['total'], )
def batch_images(context, request, get_image_info=get_image_info, # unittest get_images_batch=get_images_batch): # unittest include_image_url = request.params.get('include_image_url', None) # include_image_url is a special case. include_info = None if include_image_url is not None: # Note, we must use the path only, as IE submits the full domain # and without the next line IE would fail. path = urlparse.urlparse(include_image_url)[2] include_context = traverse(context, path)['context'] if IImage.providedBy(include_context): # We have a good image to include. include_info = get_image_info(include_context, request) # Find query parameters based on the 'source' param, # which signifies the selection index of the source button # in the imagedrawer dialog. source = request.params.get('source') assert source in ('myrecent', 'thiscommunity', 'allkarl') if source == 'myrecent': creator = authenticated_userid(request) community_path = None elif source == 'thiscommunity': creator = None community = find_community(context) # batching api requires the community path community_path = resource_path(community) else: # All Karl creator = None community_path = None # batching # Decide start and size here, don't let the lower levels # apply their default. This allows us to enforce # a MINIMAL_BATCH size. batch_start = int(request.params.get('start', '0')) batch_size = int(request.params.get('limit', '0')) # there is a minimal batch size to enforce, if the client # does not ask for one # Just pass the values to lower levels where sensible # defaults will be applied. sort_index = request.params.get('sort_on', None) reverse = request.params.get('reverse', None) # XXX include_image will now be inserted in the first # position, as extra image. insert_extra = False if include_info is not None: if batch_start == 0: batch_size -= 1 insert_extra = True else: batch_start -= 1 # Enforce the minimal batch size batch_size = max(batch_size, MINIMAL_BATCH) search_params = dict( creator=creator, community=community_path, batch_start=batch_start, batch_size=batch_size, ) if sort_index: search_params['sort_index'] = sort_index if reverse: search_params['reverse'] = bool(int(reverse)) batch_info = get_images_batch( context, request, **search_params ) records = [get_image_info(image, request) for image in batch_info['entries']] start = batch_info['batch_start'] totalRecords = batch_info['total'] # add the fake included image if include_info is not None: totalRecords += 1 if insert_extra: records.insert(0, include_info) else: start += 1 return dict( records = records, start = start, totalRecords = totalRecords, )
def _call_fut(self, context, request, **search_params): from karl.utilities.image import get_images_batch return get_images_batch(context, request, **search_params)