def upload(): host_uid = get_current_host_id() if not host_uid: return jsonify({'message': "Host id must be provided"}), HTTP_403_FORBIDDEN host = Host(uid=host_uid) if host.uid is None: return jsonify({'message': "No host with uid=" + host_uid + " in db" }), HTTP_404_NOT_FOUND if current_user.uid != host.owner_uid: return jsonify({'message': "You are not this host"}), HTTP_403_FORBIDDEN # name the new file based on host uid current_picture_name = host.logo if current_picture_name is None: filename = str(host.uid) + '_0' else: number = current_picture_name.split('_')[-1] if not number.isdecimal(): filename = str(host.uid) + '_0' else: number = int(number) + 1 filename = str(host.uid) + '_' + str(number) file = request.files.get("picture") if not file: return jsonify({'message': "No file"}), HTTP_400_BAD_REQUEST file.save(UPLOAD_FOLDER + "/" + filename) host.logo = filename host.save() return jsonify(SUCCESS)
def create_host(): """Required: title""" data = get_request_data(request) if not data.get(TITLE): return jsonify(HOST_CREATION_FAILED), HTTP_400_BAD_REQUEST data[OWNER_UID] = current_user.uid owner = User(uid=current_user.uid) if owner.workplace_uid is not None: return jsonify({'message': "Please retire first"}), HTTP_403_FORBIDDEN host = Host(data) host_uid = host.save() if host_uid is None: return jsonify({ 'message': "Host with this title (and owner) already exists" }), HTTP_409_CONFLICT owner.workplace_uid = session['host_id'] = host_uid owner.save() return jsonify({'code': 0, 'host_id': host_uid, 'message': 'OK'})
def update_host(): """All fields updated at a time""" host_uid = get_current_host_id() if not host_uid: return jsonify({'message': "Not logged in"}), HTTP_403_FORBIDDEN data = get_request_data(request) if not data.get(TITLE): return jsonify({'message': "Title required"}), HTTP_400_BAD_REQUEST host = Host(uid=host_uid) if current_user.uid != host.owner_uid: return jsonify({'message': "You are not this host"}), HTTP_403_FORBIDDEN host.title = data[TITLE] host.description = data.get(DESCRIPTION) host.address = data.get(ADDRESS) host.latitude = data.get(LATITUDE) host.longitude = data.get(LONGITUDE) host.time_open = Host.parse_time(data.get(TIME_OPEN)) host.time_close = Host.parse_time(data.get(TIME_CLOSE)) result_uid = host.save() if result_uid is None: return jsonify({'message': "Update failed"}), HTTP_409_CONFLICT return jsonify({'code': 0, 'message': "OK"})