Some fixes for asset unique name and prepare device script

This commit is contained in:
Rusko124
2018-11-23 11:45:47 +06:00
parent 6959a04de5
commit 3f194bbdbd
2 changed files with 28 additions and 38 deletions

View File

@@ -45,15 +45,3 @@ curl --header "Content-Type: application/json" \
\"skip_asset_check\": 0
}" \
http://127.0.0.1:8080/api/v1.2/assets
# Add GPU memory
echo -e "\n# Lock down GPU memory usage\ngpu_mem_256=96\ngpu_mem_512=128\ngpu_mem_1024=196\n" >> /boot/config.txt
# Install filesystem resizer
wget -O /etc/init.d/resize2fs_once https://github.com/RPi-Distro/pi-gen/blob/master/stage2/01-sys-tweaks/files/resize2fs_once
chmod +x /etc/init.d/resize2fs_once
systemctl enable resize2fs_once
sed -i '$s/$/ init=\/usr\/lib\/raspi-config\/init_resize.sh/' /boot/cmdline.txt
# Clean the apt cache
apt-get clean

View File

@@ -214,7 +214,7 @@ class AssetContentModel(Schema):
# API
################################
def prepare_asset(request):
def prepare_asset(request, unique_name=False):
req = Request(request.environ)
data = None
@@ -239,17 +239,18 @@ def prepare_asset(request):
raise Exception("Not enough information provided. Please specify 'name', 'uri', and 'mimetype'.")
name = get('name')
with db.conn(settings['database']) as conn:
names = assets_helper.get_names_of_assets(conn)
if name in names:
i = 1
while True:
new_name = '%s-%i' % (name, i)
if new_name in names:
i += 1
else:
name = new_name
break
if unique_name:
with db.conn(settings['database']) as conn:
names = assets_helper.get_names_of_assets(conn)
if name in names:
i = 1
while True:
new_name = '%s-%i' % (name, i)
if new_name in names:
i += 1
else:
name = new_name
break
asset = {
'name': name,
@@ -305,7 +306,7 @@ def prepare_asset(request):
return asset
def prepare_asset_v1_2(request_environ, asset_id=None):
def prepare_asset_v1_2(request_environ, asset_id=None, unique_name=False):
data = json.loads(request_environ.data)
def get(key):
@@ -327,17 +328,18 @@ def prepare_asset_v1_2(request_environ, asset_id=None):
"Not enough information provided. Please specify 'name', 'uri', 'mimetype', 'is_enabled', 'start_date' and 'end_date'.")
name = get('name')
with db.conn(settings['database']) as conn:
names = assets_helper.get_names_of_assets(conn)
if name in names:
i = 1
while True:
new_name = '%s-%i' % (name, i)
if new_name in names:
i += 1
else:
name = new_name
break
if unique_name:
with db.conn(settings['database']) as conn:
names = assets_helper.get_names_of_assets(conn)
if name in names:
i = 1
while True:
new_name = '%s-%i' % (name, i)
if new_name in names:
i += 1
else:
name = new_name
break
asset = {
'name': name,
@@ -597,7 +599,7 @@ class AssetsV1_1(Resource):
}
})
def post(self):
asset = prepare_asset(request)
asset = prepare_asset(request, unique_name=True)
if url_fails(asset['uri']):
raise Exception("Could not retrieve file. Check the asset URL.")
with db.conn(settings['database']) as conn:
@@ -722,7 +724,7 @@ class AssetsV1_2(Resource):
})
def post(self):
request_environ = Request(request.environ)
asset = prepare_asset_v1_2(request_environ)
asset = prepare_asset_v1_2(request_environ, unique_name=True)
if not asset['skip_asset_check'] and url_fails(asset['uri']):
raise Exception("Could not retrieve file. Check the asset URL.")
with db.conn(settings['database']) as conn: