def update_test_data(data): """Update test by its id. Note: This method will update name of test by id. Args: data contains: id (int), name(varchar) Returns: response.Response: update message. """ if data == {}: return response.Response('Invalid data.') with mysql.db_session() as session: update_test = session.query(Test) \ .filter_by(test_id=data['id']) \ .one_or_none() if not update_test: return response.Response('{} test id not found.'.format( data['id'])) else: update_test.name = data['name'] session.merge(update_test) return response.Response(message=update_test.to_dict())
def update_product_by_id(data): """Update product data for the given product_id. Args: product_id (int): id product. data (dict): dict containing product data. e.g; {product_id : product_id, product_name : product_name, ..} session (object): db session object, to make it transactional. Returns: response.Response: response object containing dict of product or error. """ if data == {}: return response.create_error_response( error.ERROR_CODE_NOT_FOUND, 'Invalid data sent for update product.') with mysql.db_session() as session: update_product = session.query(Product) \ .get(data.get('product_id')) if not update_product: return response.create_not_found_response( 'product id:{} not found.'.format(data['product_id'])) else: update_product.product_name = data.get('product_name') session.merge(update_product) return response.Response(message=update_product.to_dict())
def seed_models(models): """Save the given model(s) to the DB.""" if not hasattr(models, '__iter__'): models = [models] with db_session() as session: _exit_if_not_test_environment(session) for model in models: session.merge(model) session.commit()
def insert_users(data): with db_session() as session: user1 = VectorapiUsers() user1.name = data.get('name') user1.api_key = data.get('api_key') user1.api_secret = data.get('api_secret') session.add(user1) session.flush() return response.Response(message=user1.to_dict())
def insert_user(data): with mysql.db_session() as session: new_user = VectorapiUsers() new_user.name = data.get('user_name', 'demo') new_user.api_key = data.get('api_key') new_user.api_secret = data.get('api_secret', 'secret') session.add(new_user) session.flush() # only if you need the new id return response.Response(message=new_user.to_dict())
def get_all_users(user_id): with db_session() as sesion: result = sesion.query(VectorapiUsers) \ .filter(VectorapiUsers.user_id == user_id)\ .all() if not result: return response.create_not_found_response('No user found.') dict_result = [user.to_dict() for user in result] return response.Response(message=dict_result)
def update_users(user_id, data): with db_session() as session: existing_user = session.query(VectorapiUsers).get(user_id) if not existing_user: return response.create_not_found_response('No user found.') existing_user.name = data.get('name') existing_user.api_key = data.get('api_key') existing_user.api_secret = data.get('api_secret') session.merge(existing_user) session.flush() return response.Response(message=existing_user.to_dict())
def get_all_tests(): """Get all test list. Returns: list of tests. """ with mysql.db_session() as session: result_set = session.query(Test).all() if not result_set: return response.Response('test data not available.') total_records = [r.to_dict() for r in result_set] return response.Response(message=total_records)
def add_product(data): """Add new product data for the given data. Args: product_name (string): product name. data (dict): dict containing product data. e.g; [{product_name : product_name}] Returns: response.Response: response object containing dict of product or error. """ with mysql.db_session() as session: new_product = Product(product_name=data.get('product_name')) session.add(new_product) return response.Response(message='{} successfully added'.format(data))
def get_all_product(): """Get all the products. Returns: response.Response: containing dict of product or error. """ with mysql.db_session() as session: product = session.query(Product).all() if not product: return response.create_not_found_response() response_data = [each.to_dict() for each in product] return response.Response(message=response_data)
def get_product_by_id(product_id): """Get product information by product id. Args: product_id (int): unique identifier for the product (product_id). Returns: response.Response: containing dict of product or error. """ with mysql.db_session() as session: product = session.query(Product).get(product_id) if not product: return response.create_not_found_response( 'product id:{} not found.'.format(product_id)) return response.Response(message=product.to_dict())
def add_test_data(data): """Add test data. Args: data contains: name(varchar) Returns: saved records. """ # validate data sent are valid or non empty (this is optional case). if not data: return response.Response('data is invalid.') with mysql.db_session() as session: new_test = Test(name=data) session.add(new_test) return response.Response(new_test.to_dict())
def get_by_id(tid): """Get a test by its id. Note: This method will return test by its id. Args: tid (int): test id. Returns: test data. """ with mysql.db_session() as session: test_data = session.query(Test).get(tid) if not test_data: return response.Response('{} test id not available.'.format(tid)) return response.Response(test_data.to_dict())
def delete_product_by_id(data): """Delete product with data provided. Args: product_id (int): primary key product_id. data (dict): additional translations for fields. Returns: Response: A Response obj with message """ with mysql.db_session() as session: saved_artist = session.query(Product).get(data) if not saved_artist: return response.create_not_found_response( 'product id:{} not found.'.format(data)) product = saved_artist.to_dict() session.delete(saved_artist) return response.Response( message='{} successfully deleted.'.format(product))
def delete_test_data(tid): """Delete test by its id. Note: This method will delete test by id. Args: tid (int): test id. Returns: delete message with test id. """ with mysql.db_session() as session: delete_test = session.query(Test).get(tid) if not delete_test: return response.Response('{} record id not found.'.format(tid)) session.delete(delete_test) return response.Response('{} test deleted successfully.'.format(tid))
def insert_itunes_alllanguages(): """Insert data into itunes_languages table.""" with db_session() as session: session.execute(INSERT_ITUNES_ALLLANGUAGES)
def insert_vendor(): """Insert data into vendor table.""" with db_session() as session: session.execute(INSERT_VENDOR)
def get_all_vectorapi_users(): with mysql.db_session() as session: result = session.query(VectorapiUsers).all() response_date = [user.to_dict() for user in result] return response.Response(message=response_date)
def drop_vendor(): """Drop the `vendor` table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_TABLE_VENDOR)
def drop_product(): """Drop the `product` table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_TABLE_PRODUCT)
def insert_product(): """Insert data into product table.""" with db_session() as session: session.execute(INSERT_PRODUCT)
def drop_itunes_languages(): """Drop the `itunes_languages` table.""" with db_session() as session: _exit_if_not_test_environment(session) session.execute(DROP_TABLE_ITUNES_LANGUAGES)
def create_itunes_languages(): """Create the `itunes_languages` table.""" with db_session() as session: _exit_if_not_test_environment(session) drop_itunes_languages() session.execute(CREATE_TABLE_ITUNES_LANGUAGES)
def create_product(): """Create the `product` table.""" with db_session() as session: _exit_if_not_test_environment(session) drop_product() session.execute(CREATE_TABLE_PRODUCT)
def create_vendor(): """Create the `vendor` table.""" with db_session() as session: _exit_if_not_test_environment(session) drop_vendor() session.execute(CREATE_TABLE_VENDOR)