Made including a new image optional when updating a template

pull/443/head
Christopher Usher 3 weeks ago
parent 3ba01dda11
commit 1ba8597957

@ -644,9 +644,12 @@ def get_template_metadata(name):
logging.info('Thumbnail metadata of {} fetched'.format(name)) logging.info('Thumbnail metadata of {} fetched'.format(name))
return json.dumps(row._asdict()) return json.dumps(row._asdict())
def validate_template(new_template): def validate_template(new_template, require_image=True):
columns = ['name', 'description', 'attribution', 'crop', 'location']
if require_image or 'image' in new_template:
columns += ['image']
columns = ['name', 'image', 'description', 'attribution', 'crop', 'location']
#check for missing fields #check for missing fields
missing = set(columns) - set(new_template) missing = set(columns) - set(new_template)
if missing: if missing:
@ -656,15 +659,16 @@ def validate_template(new_template):
for extra in extras: for extra in extras:
del new_template[extra] del new_template[extra]
#convert and validate template image if 'image' in columns:
try: #convert and validate template image
new_template['image'] = base64.b64decode(new_template['image']) try:
except binascii.Error: new_template['image'] = base64.b64decode(new_template['image'])
return None, 'Template image must be valid base64', 400 except binascii.Error:
# check for PNG file header return None, 'Template image must be valid base64', 400
if not new_template['thumbnail_image'].startswith(b'\x89PNG\r\n\x1a\n'): # check for PNG file header
return None, 'Template image must be a PNG', 400 if not new_template['thumbnail_image'].startswith(b'\x89PNG\r\n\x1a\n'):
return None, 'Template image must be a PNG', 400
return columns, new_template, 200 return columns, new_template, 200
@ -704,7 +708,7 @@ def add_template(artist=None):
@authenticate_artist @authenticate_artist
def update_template(name, artist=None): def update_template(name, artist=None):
"""Update a template in the database""" """Update a template in the database"""
columns, message, code = validate_template(flask.request.json) columns, message, code = validate_template(flask.request.json, False)
if code != 200: if code != 200:
return message, code return message, code
new_template = message new_template = message

Loading…
Cancel
Save