Example #1
0
def image_list_json(request):
    #TODO duplication,I have to DRY below code
    blob_info_query = blobstore.BlobInfo.all().order('-creation')
    blob_info_results = blob_info_query.fetch(10)
    for r in blob_info_results:
        bsi_entity = BlobStoreImage.get_by_key_name(r.md5_hash)
        if bsi_entity is None:
            bsi_entity = BlobStoreImage(key_name=r.md5_hash,file_name=r.filename,blob_key=r.key())
            bsi_entity.put()
    query = BlobStoreImage.all().order('-update')
    paginator = Paginator(query,10)
    try:
        page = int(request.args.get('page','1'))
    except ValueError:
        page = 1
    try:
        results = paginator.page(page)
    except (EmptyPage,InvalidPage):
        results = paginator.page(paginator.num_pages)
    return_list = []
    for r in results.object_list:
        if r.image_path is None or r.image_path == '':
            r.image_path = get_serving_url(r.blob_key.key())
            r.put()
        return_list.append({'key':str(r.key()),
            'id':r.key().name(),
            'title':r.title,
            'file_name':r.file_name,
            'note':r.note,
            'image_path':RE_REMOVE_HTTP.sub('',r.image_path),
            'update':str(r.update)[:16]})
    return Response(json.dumps({'images':return_list,
        'current_page':results.number,
        'total_pages':results.paginator.num_pages}, ensure_ascii=False))