def with_multiple_variations(sample_variations: list, expected_get_data_from_pm: dict, mock_plenty_api_items_response_de: list, mock_plenty_api_attribute_response: list, mock_plenty_api_manufacturers_response: list): shared.plenty_variations = sample_variations shared.lang = 'de' shared.item_name_number = 3 shared.gender_property_id = 3 shared.age_property_id = 4 shared.google_category_property_id = 2 shared.url_property_id = 2 shared.material_property_id = 3 mock_plenty = unittest.mock.Mock(spec=plenty_api.PlentyApi) mock_plenty.plenty_api_get_items.return_value =\ mock_plenty_api_items_response_de mock_plenty.plenty_api_get_attributes.return_value =\ mock_plenty_api_attribute_response mock_plenty.plenty_api_get_manufacturers.return_value =\ mock_plenty_api_manufacturers_response shared.plenty_api_instance = mock_plenty target = ['1234x', '1345x', '1456x'] result = get_data_from_plentymarkets(target=target) assert_frame_equal(expected_get_data_from_pm['multi'], result)
def with_invalid_sync_type(sample_variations: list, expected_get_data_from_pm: dict): shared.plenty_variations = sample_variations mock_plenty = unittest.mock.Mock(spec=plenty_api.PlentyApi) shared.plenty_api_instance = mock_plenty header = ['id', 'invalid'] result = get_data_from_plentymarkets(header=header) assert len(result.index) == 0
def with_sync_type_price(sample_variations: list, expected_get_data_from_pm: dict): shared.plenty_variations = sample_variations shared.price_id = 1 mock_plenty = unittest.mock.Mock(spec=plenty_api.PlentyApi) shared.plenty_api_instance = mock_plenty header = ['id', 'price'] result = get_data_from_plentymarkets(header=header) assert_frame_equal(expected_get_data_from_pm['price'], result)
def with_sync_type_link(sample_variations: list, expected_get_data_from_pm: dict): shared.plenty_variations = sample_variations shared.url_property_id = 2 shared.referrer = 4 mock_plenty = unittest.mock.Mock(spec=plenty_api.PlentyApi) shared.plenty_api_instance = mock_plenty header = ['id', 'link', 'image_link'] result = get_data_from_plentymarkets(header=header) assert_frame_equal(expected_get_data_from_pm['link'], result)
def add_new_items(google: pandas.DataFrame, plenty: pandas.DataFrame) -> pandas.DataFrame: """ Find & add variations from PlentyMarkets to the google sheet, which were previously not found in the google sheet. Get all data for the new variations from the PlentyMarkets API. Parameter: google [DataFrame] - facebook sync google sheet plenty [DataFrame] - plentymarkets API data Return: [DataFrame] - updated google sheet """ if valid_dataframe(parameter=plenty) <= 0: return google # Fill the google sheet with data as it was left empty on purpose if len(google.index) == 0 and len(google.columns) == len(GSHEET_HEADER): return plenty_data.get_data_from_plentymarkets( target=plenty['id'].values) # Abort because the google sheet is empty because of an error if len(google.index) == 0 and len(google.columns) == 0: return pandas.DataFrame() plenty_copy = copy.deepcopy(plenty) # ITEMS found in plenty but not in gsheet plenty_copy['create'] = np.where(~plenty_copy['id'].isin(google['id']), 1, 0) new_items = plenty_copy[plenty_copy['create'] == 1] if len(new_items.index) == 0: return google new_items = plenty_data.get_data_from_plentymarkets( target=new_items['id'].values) return pandas.concat([google, new_items], ignore_index=True)
def with_sync_type_text_it(sample_variations: list, expected_get_data_from_pm: dict, mock_plenty_api_items_response_en: list): shared.plenty_variations = sample_variations shared.lang = 'it' shared.item_name_number = 3 mock_plenty = unittest.mock.Mock(spec=plenty_api.PlentyApi) mock_plenty.plenty_api_get_items.return_value =\ mock_plenty_api_items_response_en shared.plenty_api_instance = mock_plenty header = ['id', 'title', 'description'] result = get_data_from_plentymarkets(header=header) assert_frame_equal(expected_get_data_from_pm['text']['it'], result)
def with_sync_type_attribute_it(sample_variations: list, expected_get_data_from_pm: dict, mock_plenty_api_attribute_response: list): shared.plenty_variations = sample_variations shared.lang = 'it' shared.color_attribute_id = 2 shared.size_attribute_id = 3 mock_plenty = unittest.mock.Mock(spec=plenty_api.PlentyApi) mock_plenty.plenty_api_get_attributes.return_value =\ mock_plenty_api_attribute_response shared.plenty_api_instance = mock_plenty header = ['id', 'color', 'size'] result = get_data_from_plentymarkets(header=header) assert_frame_equal(expected_get_data_from_pm['attribute']['it'], result)
def cli(): parser = setup_argparser() verbose = logger.info if parser.verbose else lambda *a, **k: None config = configparser.ConfigParser() config.read(CONFIG_PATH) if not check_config(config=config): sys.exit(1) direct_credentials = get_config_credentials(config=config) if direct_credentials: verbose("Use credentials from configuration.") api = plenty_api.PlentyApi( base_url=config['General']['plenty_api_url'], use_keyring=True, data_format='json', username=direct_credentials['user'], password=direct_credentials['pw'] ) if not direct_credentials or not api.creds['Authorization']: verbose("Use credentials from keyring.") api = plenty_api.PlentyApi( base_url=config['General']['plenty_api_url'], use_keyring=True, data_format='json' ) if not get_config_values(config=config): logger.error("Invalid configuration.") sys.exit(1) if not parser.synctype: logger.error("Specify the sync type: [inventory, price, text, " "attribute, link, all].") sys.exit(1) verbose("Load the google-sheet and get the first sheet.") google_account = gspread.oauth() sheet = google_account.open_by_key(config['General']['google_sheet_id']) worksheet = sheet.get_worksheet(0) verbose("Read the google-sheet.") google = gsheet.gsheet_read(worksheet=worksheet) verbose("Get all Plentymarkets variations through the API.") variations = api.plenty_api_get_variations( refine={'referrerId': config['Mapping']['facebook_referrer'], 'isActive': True}, additional=['properties', 'images', 'variationAttributeValues', 'stock', 'variationSalesPrices'], lang=shared.lang ) if not variations: sys.exit(1) variations = [var for var in variations if not var['isMain']] shared.plenty_variations = variations shared.plenty_api_instance = api verbose("Fetch necessary data for the specified sync type.") sync = plenty.get_data_from_plentymarkets( header=HEADER_SYNC_MAP[parser.synctype]) verbose("Check if new variations were added in Plentymarkets.") google = gsheet.add_new_items(google=google, plenty=sync) verbose("Check if variations were deleted in PlentyMarkets.") google = gsheet.delete_removed_items(google=google, plenty=sync) verbose("Update the columns of the google-sheet.") google = gsheet.update_column(google=google, plenty=sync) if len(google.index) > 0: verbose("Write the changes to the google-sheet.") gsheet.gsheet_write(worksheet=worksheet, dataframe=google) verbose("Resize the google-sheet to it's current size.") worksheet.resize(rows=len(google.index)+1)