示例#1
0
    def with_empty_plenty_data(sample_google_sheet_normal,
                               sample_plenty_data_full, mocker):
        mocker.patch(get_data_function, return_value=sample_plenty_data_full)
        result = add_new_items(google=sample_google_sheet_normal,
                               plenty=pandas.DataFrame())

        # should be unchanged
        assert_frame_equal(sample_google_sheet_normal, result)
示例#2
0
    def with_empty_google_sheet(sample_plenty_data_full,
                                sample_plenty_data_inventory, mocker):
        mocker.patch(get_data_function, return_value=sample_plenty_data_full)
        result = add_new_items(google=pandas.DataFrame(),
                               plenty=sample_plenty_data_inventory)

        # should be empty
        assert_frame_equal(pandas.DataFrame(), result)
示例#3
0
    def with_malfunctioning_plenty_api(sample_google_sheet_normal,
                                       sample_plenty_data_inventory, mocker):
        """
            Mock the get_full_data_from_plentymarkets function with
            an empty dataframe result.
        """
        mocker.patch(get_data_function, return_value=pandas.DataFrame())
        result = add_new_items(google=sample_google_sheet_normal,
                               plenty=sample_plenty_data_inventory)

        # should be unchanged
        assert_frame_equal(sample_google_sheet_normal, result)
示例#4
0
    def with_new_ids(sample_google_sheet_normal, sample_plenty_data_full,
                     sample_plenty_data_new_items, expected_add_new_items_full,
                     mocker):
        """
            Mock the get_full_data_from_plentymarkets function with
            the sample_plenty_data_full fixture.
        """
        mocker.patch(get_data_function, return_value=sample_plenty_data_full)
        result = add_new_items(google=sample_google_sheet_normal,
                               plenty=sample_plenty_data_new_items)

        assert_frame_equal(expected_add_new_items_full, result)
示例#5
0
    def with_less_ids(sample_google_sheet_normal, sample_plenty_data_full,
                      sample_plenty_data_less_items, mocker):
        """
            Mock the get_full_data_from_plentymarkets function with
            the sample_plenty_data_full fixture.
        """
        mocker.patch(get_data_function, return_value=sample_plenty_data_full)
        result = add_new_items(google=sample_google_sheet_normal,
                               plenty=sample_plenty_data_less_items)

        # should be unchanged
        assert_frame_equal(sample_google_sheet_normal, result)
示例#6
0
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)