From 69a5739aef6bdc5e0ebc12ebfe6a4775e8211304 Mon Sep 17 00:00:00 2001 From: tierno Date: Sat, 7 Jan 2017 01:06:21 +0100 Subject: [PATCH] allow public images to be visible by all tenants Signed-off-by: tierno --- httpserver.py | 14 ++++++++------ vim_db.py | 22 +++++++++++++--------- 2 files changed, 21 insertions(+), 15 deletions(-) diff --git a/httpserver.py b/httpserver.py index ecf878c..ce433ae 100644 --- a/httpserver.py +++ b/httpserver.py @@ -1042,10 +1042,11 @@ def http_get_images(tenant_id): ('id','name','description','path','public') ) if tenant_id=='any': from_ ='images' + where_or_ = None else: - from_ ='tenants_images inner join images on tenants_images.image_id=images.uuid' - where_['tenant_id'] = tenant_id - result, content = my.db.get_table(SELECT=select_, FROM=from_, WHERE=where_, LIMIT=limit_) + from_ ='tenants_images right join images on tenants_images.image_id=images.uuid' + where_or_ = {'tenant_id': tenant_id, 'public': 'yes'} + result, content = my.db.get_table(SELECT=select_, FROM=from_, WHERE=where_, WHERE_OR=where_or_, WHERE_AND_OR="AND", LIMIT=limit_) if result < 0: print "http_get_images Error", content bottle.abort(-result, content) @@ -1067,11 +1068,12 @@ def http_get_image_id(tenant_id, image_id): ('id','name','description','progress', 'status','path', 'created', 'updated','public') ) if tenant_id=='any': from_ ='images' + where_or_ = None else: - from_ ='tenants_images as ti inner join images as i on ti.image_id=i.uuid' - where_['tenant_id'] = tenant_id + from_ ='tenants_images as ti right join images as i on ti.image_id=i.uuid' + where_or_ = {'tenant_id': tenant_id, 'public': "yes"} where_['uuid'] = image_id - result, content = my.db.get_table(SELECT=select_, FROM=from_, WHERE=where_, LIMIT=limit_) + result, content = my.db.get_table(SELECT=select_, FROM=from_, WHERE=where_, WHERE_OR=where_or_, WHERE_AND_OR="AND", LIMIT=limit_) if result < 0: print "http_get_images error %d %s" % (result, content) diff --git a/vim_db.py b/vim_db.py index d88306d..11a7aa1 100644 --- a/vim_db.py +++ b/vim_db.py @@ -216,6 +216,7 @@ class vim_db(): 'WHERE': dict of key:values, translated to key=value AND ... (Optional) 'WHERE_NOT': dict of key:values, translated to key!=value AND ... (Optional) 'WHERE_OR': dict of key:values, translated to key=value OR ... (Optional) + 'WHERE_AND_OR: str 'AND' or 'OR'(by default) mark the priority to 'WHERE AND (WHERE_OR)' or (WHERE) OR WHERE_OR' (Optional) 'LIMIT': limit of number of rows (Optional) Return: a list with dictionarys at each row ''' @@ -227,22 +228,25 @@ class vim_db(): where_and = None where_or = None - if 'WHERE' in sql_dict and len(sql_dict['WHERE']) > 0: - w=sql_dict['WHERE'] - where_and = " AND ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) - if 'WHERE_NOT' in sql_dict and len(sql_dict['WHERE_NOT']) > 0: - w=sql_dict['WHERE_NOT'] - where_and_not = " AND ".join(map( lambda x: str(x) + (" is not Null" if w[x] is None else "!='"+str(w[x])+"'"), w.keys()) ) + w = sql_dict.get('WHERE') + if w: + where_and = " AND ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) + w = sql_dict.get('WHERE_NOT') + if w: + where_and_not = " AND ".join(map( lambda x: str(x) + (" is not Null" if w[x] is None else "!='"+str(w[x])+"'"), w.keys()) ) if where_and: where_and += " AND " + where_and_not else: where_and = where_and_not - if 'WHERE_OR' in sql_dict and len(sql_dict['WHERE_OR']) > 0: - w=sql_dict['WHERE_OR'] + w = sql_dict.get('WHERE_OR') + if w: where_or = " OR ".join(map( lambda x: str(x) + (" is Null" if w[x] is None else "='"+str(w[x])+"'"), w.keys()) ) if where_and!=None and where_or!=None: - where_ = "WHERE (" + where_and + ") OR " + where_or + if sql_dict.get("WHERE_AND_OR") == "AND": + where_ = "WHERE " + where_and + " AND (" + where_or + ")" + else: + where_ = "WHERE (" + where_and + ") OR " + where_or elif where_and!=None and where_or==None: where_ = "WHERE " + where_and elif where_and==None and where_or!=None: -- 2.17.1