def fast_neural_style_transform(): """ For fast neural style transformation of image we are using API exposed by deepai.org """ data = request.get_json(force=True) try: key = data['request_key'] style_url = data['style_url'] content_url = data['content_url'] except (KeyError): raise JsonError( description='Key Error: Key missing in the request') # Invoke deepai.org neural-style API deepai_resp = requests.post("https://api.deepai.org/api/neural-style", data={ 'style': style_url, 'content': content_url, }, headers={'api-key': DEEPAI_KEY}) deepai_resp_json = deepai_resp.json() try: tranformed_image_url = deepai_resp_json['output_url'] except (KeyError): err_msg = 'Trans Error: Neural style transformation failed' raise JsonError(status_=500, request_key=key, description=err_msg) return json_response(request_key=key, output_url=tranformed_image_url)
def change_password(): """ POST endpoint that changes the current user's password without revoking all the access and refresh tokens. """ user = get_current_user() club = user.club json = g.clean_json old_password = json['old_password'] new_password = json['new_password'] if not hash_manager.verify(old_password, user.password): raise JsonError(status='error', reason='The old password is incorrect.') # Check if the password is the same if old_password == new_password: raise JsonError(status='error', reason='The old and new passwords are identical.') # Check if the password is strong enough is_password_strong = flask_exts.password_checker.check(new_password) if not is_password_strong: raise JsonError(status='error', reason='The new password is not strong enough') # Only set the new password if the old password is verified user.password = hash_manager.hash(new_password) user.save() return {'status': 'success'}
def start_single_run(): req_args = request.args.to_dict() # check whether run_id is specified and valid. if 'run-id' not in req_args.keys() or not req_args['run-id']: raise JsonError(status_=400, description='run-id is not specified') run_id = req_args['run-id'] try: rel_run_path = parse_run_id(run_id) except: raise JsonError(status_=400, description='Error in the given run-id: %s' % run_id) run_path = path.join(UPLOADS_DEFAULT_DEST, rel_run_path) prepare_hec_hms_run(run_path, HEC_HMS_MODEL_TEMPALTE_DIR) success = run_hec_hms(run_path) # TODO update the run_id in the DB with the status if success: return json_response(status_=200, run_id=run_id, run_status='Started and Completed!', description='Model run successful!.') else: return json_response( status_=500, run_id=run_id, run_status='Failed!', description='Model run failure. Please check your inputs.')
def signin(): """Sign in a registered user by adding the user id to the session.""" data = request.get_json() try: jsonschema.validate(schema=JSONSchema, instance=data) except jsonschema.exceptions.ValidationError as e: # pragma: no cover current_app.logger.error(f'JSON-schema validation error: {e}') raise JsonError(message='bad request') from e try: user = User.query.filter_by( username=data.get('username') ).first() except SQLAlchemyError as e: # pragma: no cover current_app.logger.error(f'DB error: {e}') raise JsonError(message='bad request') from e if user is None: current_app.logger.error( f"AUTH error: user \"{data['username']}\": not registered") raise JsonError(401, message='bad request') if not check_password_hash(user.password, data['password']): current_app.logger.error( f"AUTH error: user \"{data['username']}\": wrong password") raise JsonError(401, message='bad request') session.clear() session['user_id'] = user.id session['user_username'] = user.username return json_response()
def extract_data(): req_args = request.args.to_dict() # check whether run_id is specified and valid. if 'run-id' not in req_args.keys() or not req_args['run-id']: raise JsonError(status_=400, description='run-id is not specified') run_id = req_args['run-id'] try: rel_run_path = parse_run_id(run_id) except: raise JsonError(status_=400, description='Error in the given run-id: %s' % run_id) run_path = path.join(UPLOADS_DEFAULT_DEST, rel_run_path) # TODO check the DB for the status of run_id # TODO if status is not finished prepare error response saying so. if not is_output_ready(run_path): raise JsonError(status_=503, run_id=run_id, run_status='Running', description='output is not ready yet.') run_output_path = path.join(run_path, 'output') return send_from_directory(directory=run_output_path, filename=OUTPUT_DISCHARGE_CSV_NAME)
def login(): """ POST endpoint that logs in an existing officer user. """ json = g.clean_json email = json['email'] password = json['password'] potential_user = NewOfficerUser.objects(email=email).first() if potential_user is None: raise JsonError(status='error', reason='The user does not exist.') if not hash_manager.verify(password, potential_user.password): raise JsonError(status='error', reason='The password is incorrect.') access_token = create_access_token(identity=potential_user) refresh_token = create_refresh_token(identity=potential_user) access_jti = get_jti(encoded_token=access_token) refresh_jti = get_jti(encoded_token=refresh_token) AccessJTI(owner=potential_user, token_id=access_jti).save() RefreshJTI(owner=potential_user, token_id=refresh_jti).save() return { 'access': access_token, 'access_expires_in': int(CurrentConfig.JWT_ACCESS_TOKEN_EXPIRES.total_seconds()), 'refresh': refresh_token, 'refresh_expires_in': int(CurrentConfig.JWT_REFRESH_TOKEN_EXPIRES.total_seconds()) }
def resend_confirm_email(): """ POST endpoint that resends a new confirmation email if the user exists. """ json = g.clean_json club_email = json['email'] # Check if email is already registered potential_user = NewOfficerUser.objects(email=club_email).first() if potential_user is None: raise JsonError(status='error', reason='No club under that email exists!', status_=404) if potential_user.confirmed: raise JsonError(status='error', reason='The user is already confirmed.') verification_token = flask_exts.email_verifier.generate_token( club_email, 'confirm-email') confirm_url = CurrentConfig.BACKEND_BASE_URL + url_for( 'user.confirm_email', token=verification_token) html = render_template('confirm-email.html', confirm_url=confirm_url) flask_exts.email_sender.send(subject='Please confirm your email', recipients=[club_email], body=html) return {'status': 'success'}
def upload_data(): print('upload_data') req_args = request.args.to_dict() if 'run-id' not in req_args.keys() or not req_args['run-id']: raise JsonError(status_=400, description='run-id is not specified.') if 'zip-file-name' not in req_args.keys() or not req_args['zip-file-name']: raise JsonError(status_=400, description='zip-file-name is not specified.') run_id = request.args.get('run-id', type=str) zip_file_name = request.args.get('zip-file-name', type=str) # without zip extension. if validate_run_id(run_id): run_date = run_id.split(':')[2] run_name = run_id.split(':')[3] input_file_path = path.join(UPLOADS_DEFAULT_DEST, run_date, run_name, 'input') output_file_path = path.join(UPLOADS_DEFAULT_DEST, run_date, run_name, 'output') copy_input_file_to_output(input_file_path, output_file_path) output_zip = create_output_zip(zip_file_name, output_file_path, output_file_path) return send_from_directory(directory=path.join(UPLOADS_DEFAULT_DEST, ' OUTPUT'), filename=output_zip) else: raise JsonError(status_=400, description='Invalid run id.')
def init_hec_hms_distributed(): req_args = request.args.to_dict() # check whether run-name is specified and valid. if 'run-name' not in req_args.keys() or not req_args['run-name']: raise JsonError(status_=400, description='run-name is not specified.') run_name = req_args['run-name'] run_datetime_str = request.args.get( 'datetime', default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), type=str) run_datetime = datetime.datetime.strptime(run_datetime_str, '%Y-%m-%d %H:%M:%S') input_dir_rel_path = path.join(run_datetime.strftime('%Y-%m-%d'), run_name, 'input') input_dir_abs_path = path.join(UPLOADS_DEFAULT_DEST, input_dir_rel_path) if path.exists(input_dir_abs_path): raise JsonError( status_=400, description='run-name: %s is already taken for run date: %s.' % (run_name, run_datetime)) for f in request.files.getlist('rainfall'): filename = secure_filename(f.filename) f.save(path.join(input_dir_rel_path, filename)) return json_response(status_=200, description='Successfully saved files.')
def music(): try: file = request.files['file'] size = request.content_length if not file: print('file not found') raise JsonError(description='file not found') elif size > ALLOWED_SIZE: print('file size is more than allowed size') raise JsonError( description="file size is more than allowed size 10Mb.") elif not allowed_file(file.filename): print('This file extesion not allowed.') raise JsonError(description="This file extesion not allowed.") elif not file and not allowed_file(file.filename): print('Some error occured.') raise JsonError(description="Some error occured.") else: filename = secure_filename(file.filename) print('filename', filename) file.save('audio.wav') filePath = 'audio.wav' result = predictAudio(filePath) original = 'not original' if (result and result[0] == 1): original = 'original' return json_response(filename=file.filename, description=original) except (KeyError, TypeError, ValueError): raise JsonError(description='Invalid value.')
def extract_250m_waterdischarge(): req_args = request.args.to_dict() # check whether run_id is specified and valid. if 'run-id' not in req_args.keys() or not req_args['run-id']: raise JsonError(status_=400, description='run-id is not specified') run_id = req_args['run-id'] try: rel_run_path = parse_run_id(run_id) except: raise JsonError(status_=400, description='Error in the given run-id: %s' % run_id) run_path = path.join(UPLOADS_DEFAULT_DEST, rel_run_path) try: cell_map = request.get_json() channel_cell_map = cell_map['CHANNEL_CELL_MAP'] except: raise JsonError(status_=400, description='Invalid cell map!') # TODO check the DB for the status of run_id # TODO if status is not finished prepare error response saying so. if not is_output_ready(run_path): raise JsonError(status_=503, run_id=run_id, run_status='Running', description='output is not ready yet.') channel_tms = extract_water_discharge(run_path, channel_cell_map) return jsonify({'CHANNELS': channel_tms})
def init_hec_hms_single(db): print('init_hec_hms_single') req_args = request.args.to_dict() if 'run-name' not in req_args.keys() or not req_args['run-name']: raise JsonError(status_=400, description='run-name is not specified.') run_name = request.args.get('run-name', type=str) # Model running date default is current date. Folder created for this date. if 'run-datetime' not in req_args.keys() or not req_args['run-datetime']: raise JsonError(status_=400, description='run-datetime is not specified.') run_datetime = datetime.datetime.strptime(request.args.get('run-datetime', type=str), '%Y-%m-%d %H:%M:%S') if 'init-state' not in req_args.keys() or not req_args['init-state']: init_state_path = os.path.join(UPLOADS_DEFAULT_DEST, run_datetime.strftime('%Y-%m-%d'), run_name, '2008_2_Events/basinStates') init_state = single_util.is_init_state(db, run_datetime.strftime('%Y-%m-%d'), init_state_path) else: init_state = ast.literal_eval(request.args.get('init-state', type=str)) input_dir_rel_path = os.path.join(run_datetime.strftime('%Y-%m-%d'), run_name, 'input') # Check whether the given run-name is already taken for today. input_dir_abs_path = os.path.join(UPLOADS_DEFAULT_DEST, input_dir_rel_path) if os.path.exists(input_dir_abs_path): raise JsonError(status_=400, description='run-name: %s is already taken for run date: %s.' % (run_name, run_datetime)) req_files = request.files if 'rainfall' in req_files: model_hec.save(req_files['rainfall'], folder=input_dir_rel_path, name='DailyRain.csv') model_tasks.init_single(run_name, run_datetime.strftime('%Y-%m-%d'), init_state) run_id = 'HECHMS:single:%s:%s' % (run_datetime.strftime('%Y-%m-%d'), run_name) # TODO save run_id in a DB with the status return json_response(status_=200, run_id=run_id, description='Successfully saved files.') else: raise JsonError(status_=400, description='No required input files found, Rainfall file missing in request.')
def get(self, token): sessions = list(self.db.select('SELECT * FROM `sessions` WHERE token=?', (token,))) if len(sessions) == 0: raise JsonError(description='Could not find session') if sessions[0]['stopped_at'] is not None: raise JsonError(description='Session was already stopped.') return sessions[0]
def validate_id(id_type, id_param, current_user={}): try: int(id_param) except ValueError: raise JsonError(error=f'Invalid {id_type}, it must be an integer.') if id_type == 'user id' and current_user: if current_user['id'] != int(id_param): raise JsonError( error=f'Cannot access parcels for user id {id_param}.')
def do_upload(): upload = request.files['upload'] if not upload.filename.lower().endswith(('.csv')): #TODO verfy RIGHT WAY raise JsonError(description="Invalid file.") try: _id = util.save_csv(upload) except Exception: raise JsonError(description="Failed to create user. try again.") return jsonify(id=_id)
def excute_query(self, table_query): try: self.cursor.execute(table_query) self.connection.commit() return self.cursor except (Exception, psycopg2.DatabaseError) as error: self.connection.rollback() if 'duplicate key value violates unique constraint' in str(error): raise JsonError( error='User with that username or email already exists.') raise JsonError(error=str(error))
def validate(self, key): consumers = list( self.db.select( 'SELECT id, deleted_at FROM `consumers` WHERE key=?', (key, ))) if len(consumers) == 0: raise JsonError(error='Invalid consumer_key') if consumers[0]['deleted_at'] is not None: raise JsonError(error='Consumer is not active.') return consumers[0]['id']
def insert_single_log_entry(log_id): log_client = make_log_client(log_id) try: log_client.queue_entry_base64(request.json['base64_data']) except ValueError as e: raise JsonError(description=str(e)) except KeyError as e: raise JsonError( description='JSON payload must include a "base64_data" value') return {'message': 'OK, queued entry for inclusion in merkle tree'}
def decorated_function(*args, **kwargs): if app.config.get('MICROTICKS_KEY') is not None: if request.args.get('key') is None: raise JsonError( description= 'Please specify an API key using the "key" query string parameter.', status=401) elif request.args.get('key') != app.config.get('MICROTICKS_KEY'): raise JsonError( description='The key does not seem to be the right one.', status=401) return f(*args, **kwargs)
def validate(data): if not data: raise JsonError(description=('No JSON content found. Did you use ' '`Content-Type: application/json`')) try: if not isinstance(data['name'], str): raise JsonError(description='`name` must be a string') if not isinstance(data['description'], str): raise JsonError(description='`description` must be a string') except KeyError: raise JsonError(description='Must pass `name` and `description`') return data
def init_hec_hms_single(): req_args = request.args.to_dict() # check whether run-name is specified and valid. if 'run-name' not in req_args.keys() or not req_args['run-name']: raise JsonError(status_=400, description='run-name is not specified.') run_name = req_args['run-name'] run_datetime_str = request.args.get( 'datetime', default=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), type=str) init_state_str = request.args.get('init-state', default=False, type=str) init_state = ast.literal_eval(init_state_str) # input_dir_rel_path = path.join(run_datetime, run_name, 'input') run_datetime = datetime.datetime.strptime(run_datetime_str, '%Y-%m-%d %H:%M:%S') input_dir_rel_path = path.join(run_datetime.strftime('%Y-%m-%d'), run_name, 'input') # Check whether the given run-name is already taken for today. input_dir_abs_path = path.join(UPLOADS_DEFAULT_DEST, input_dir_rel_path) # /home/uwcc-admin/udp_150/hec-hms/2018-07-12/<run-name>/input/ if path.exists(input_dir_abs_path): raise JsonError( status_=400, description='run-name: %s is already taken for run date: %s.' % (run_name, run_datetime)) req_files = request.files if 'rainfall' in req_files: model_hec.save(req_files['rainfall'], folder=input_dir_rel_path, name='DailyRain.csv') init_hec_hms_models(run_name, run_datetime_str, init_state, 'single') elif ('forward' in req_args.keys() or req_args['forward']) and ('backward' in req_args.keys() or req_args['backward']): backward = request.args.get('datetime', default=2, tterype=int) forward = request.args.get('datetime', default=3, type=int) init_hec_hms_models_rf_gen(run_name, run_datetime_str, init_state, backward, forward) else: raise JsonError( status_=400, description='Missing required input file. Required DailyRain.csv') return json_response(status_=200, description='HecHms successfully initialized.')
def create_member(): data = request.get_json(force=True) try: id = data["id"] first_name = data["first_name"] last_name = data["last_name"] phone_number = data["phone_number"] client_member_id = data["client_member_id"] account_id = data["account_id"] except (KeyError, TypeError, ValueError): raise JsonError(description='Invalid value.') val = { "id": id, "first_name": first_name, "last_name": last_name, "phone_number": phone_number, "client_member_id": client_member_id, "account_id": account_id } # print("val", val, "id", id) with r.pipeline() as pipe: pipe.hmset("member:" + str(id), val) pipe.sadd("account:" + str(account_id), id) pipe.set("phone_link:" + str(phone_number), id) pipe.set("client_member_id_link:" + str(client_member_id), id) pipe.execute() return "OK"
def showId(): data = request.get_json(force=True) try: value = int(data['value']) except (KeyError, TypeError, ValueError): raise JsonError(description='Invalid value') return json_response(value=value + 1)
def wrapper(*args, **kwargs): user_data = self.decode_token() if type(user_data) is dict: self.save_user() email = user_data['email'] user = User.query.filter_by(email=email).first() headers = {"Authorization": 'Bearer ' + self.get_token()} data = requests.get(api_url + "users?email=%s" % user.email, headers=headers) response = json.loads(data.content.decode("utf-8")) if response['values'][0]['location']: user.location = \ response['values'][0]['location']['name'] user.save() else: user.location = "Nairobi" user.save() user_role = UsersRole.query.filter_by( user_id=user.id).first() role = Role.query.filter_by(id=user_role.role_id).first() if role.role in expected_args: return func(*args, **kwargs) else: res = 'You are not authorized to perform this action' if 'REST' in expected_args: raise JsonError(message=res, status=401) raise GraphQLError(res) else: raise GraphQLError(user_data[0].data)
def prepare_input_files( run_datetime=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), back_days=2, forward_days=3): print('prepare_input_files.') print('run_datetime : ', run_datetime) print('back_days : ', back_days) print('forward_days : ', forward_days) run_datetime = datetime.strptime(run_datetime, '%Y-%m-%d %H:%M:%S') to_date = run_datetime + timedelta(days=forward_days) from_date = run_datetime - timedelta(days=back_days) file_date = run_datetime.strftime('%Y-%m-%d') from_date = from_date.strftime('%Y-%m-%d %H:%M:%S') to_date = to_date.strftime('%Y-%m-%d %H:%M:%S') file_name = RAIN_FALL_FILE_NAME.format(file_date) print('file_name : ', file_name) print('{from_date, to_date} : ', {from_date, to_date}) try: create_rain_files(file_name, run_datetime.strftime('%Y-%m-%d %H:%M:%S'), forward_days, back_days) # rain_fall_file = Path(file_name) # if rain_fall_file.is_file(): # create_gage_file_by_rain_file('distributed_model', file_name) # create_control_file_by_rain_file('distributed_model', file_name) # else: # create_gage_file('distributed_model', from_date, to_date) # create_control_file('distributed_model', from_date, to_date) # create_run_file('distributed_model', run_datetime.strftime('%Y-%m-%d %H:%M:%S')) return json_response(status_=200, description='Successfully created input files.') except Exception as e: print(e) raise JsonError(status_=400, description='Input file creation error.')
def read_member(): data = request.get_json(force=True) # Need to do a bunch of tries, because we only need one of the three to read. try: phone_number = data['phone_number'] client_member_id = data['client_member_id'] id = data['id'] except (KeyError, TypeError, ValueError): raise (JsonError(description='Invalid value.')) cand = r.hgetall(id) if isinstance(cand, dict) and len(cand) > 0: return cand phone_link = r.get("phone_link:" + str(phone_number)) print("phone_link", phone_link) if phone_link != None: cand = r.hgetall("member:" + str(phone_link)) else: cand = None if isinstance(cand, dict) and len(cand) > 0: return cand client_member_id_link = r.get("client_member_id_link:" + str(client_member_id)) print("client_member_id_link", client_member_id_link) if client_member_id_link != None: cand = r.hgetall("member:" + str(client_member_id_link)) else: cand = None if isinstance(cand, dict) and len(cand) > 0: return cand return "Not found."
def schema(endpoint: str, task_name: str): """Get static schema description""" path = os.path.join('.', 'schema', endpoint, task_name + '.json') try: return unchanged_file(path), 200 except FileNotFoundError: raise JsonError(description='Unknown task: ' + task_name, status_=404)
def layout(endpoint: str, task_name: str): """Get dynamic layout description""" path = os.path.join('.', 'layout', endpoint, task_name + '.json') try: return file_with_datasets_substitution(path), 200 except FileNotFoundError: raise JsonError(description='Unknown task: ' + task_name, status_=404)
def delete_node(id): s = Node.query.filter_by(id=id).first() if s is not None: db.session.delete(s) db.session.commit() return json_response(response='ok') raise JsonError(error='node not in database')
def save_user(): new_name = request.form["name"] try: util.set_username(current_user.id, new_name) except Exception: raise JsonError(description="Failed to set username.") return json_response(user=_user)