def _upsert_ad_account_entity(entity_data: Dict[str, Any], entity_type: str): assert entity_type == Entity.AdAccount upsert_data = {'timezone': entity_data['timezone_name']} ad_account_id = entity_data['account_id'] # The scope enum here must be hardcoded to Console (it is not available on JobScope or entity data). # Will have to be changed once we get more than one scope. AdAccountEntity.upsert(DEFAULT_SCOPE, ad_account_id, **upsert_data)
def do_it_all(): from common.store.base import BaseMeta if BaseMeta.host is None: raise ValueError(f'DynamoDB host config not set. Could be running against prod/staging environment') from common.store.sync_schema import sync_schema sync_schema(brute_force=True) # Eventually this should go away and be replaced # by direct pull of AdAccount-to-tokens pairings from # some sort of Platform assets API # Since we anticipate only one Source of AdAccounts - Console # and since we use one and same token, just injecting # the thing into DB to act as seed scope for iteration over AdAccounts # per given token (hardcoded / passed through configs) from common.store.scope import AssetScope, PlatformToken, DEFAULT_SCOPE from common.store.entities import AdAccountEntity from config.facebook import TOKEN, AD_ACCOUNT, AD_ACCOUNT_TIME_ZONE # this is silly. This means that only one token per scope is possible. Rethink. token_id = DEFAULT_SCOPE PlatformToken.upsert(token_id, token=TOKEN) AssetScope.upsert(DEFAULT_SCOPE, platform_token_ids={token_id}) if AD_ACCOUNT: AdAccountEntity.upsert(DEFAULT_SCOPE, AD_ACCOUNT, is_active=True, timezone=AD_ACCOUNT_TIME_ZONE)
def ad_account_delete(ctx, scope, id, complain=False): """ :param ctx: :param scope: :param id: is "*" deletes them all :param complain: :return: """ if id == '*': for aa in AdAccountEntity.query(scope): aa.delete() if complain: AdAccountEntity.get(scope, id).delete() else: AdAccountEntity(scope, id).delete()
def get_model(cls, account_id): if account_id not in cls._cache: try: aa = AdAccountEntity.get(cls.scope, account_id) except AdAccountEntity.DoesNotExist: aa = None cls._cache[account_id] = aa return cls._cache[account_id]
def test_runs_correctly(self): account_id = random.gen_string_id() job_scope = JobScope( ad_account_id=self.ad_account_id, entity_id=self.ad_account_id, tokens=['A_REAL_TOKEN'], report_time=datetime.utcnow(), report_type='entity', report_variant=Entity.AdAccount, sweep_id='1', ) universal_id_should_be = generate_universal_id( ad_account_id=self.ad_account_id, report_type=ReportType.entity, entity_id=self.ad_account_id, entity_type=Entity.AdAccount, ) account_data = AdAccount(fbid=account_id) # Did not find a better way how to set this data on the inner AbstractCrudObject. timezone = 'Europe/Prague' account_data._data['timezone_name'] = timezone account_data._data['account_id'] = account_id with mock.patch.object(FB_ADACCOUNT_MODEL, 'api_get', return_value=account_data), mock.patch.object( NormalStore, 'store') as store: collect_adaccount(job_scope) assert store.called_with( account_data), 'Data should be stored with the cold store module' assert store.called store_args, store_keyword_args = store.call_args assert not store_keyword_args assert len( store_args ) == 1, 'Store method should be called with just 1 parameter' data_actual = store_args[0] vendor_data_key = '__oprm' ad_account_dynamo = AdAccountEntity.get(DEFAULT_SCOPE, account_id) assert ad_account_dynamo.timezone == timezone assert ad_account_dynamo.ad_account_id == account_id assert (vendor_data_key in data_actual and type(data_actual[vendor_data_key]) == dict), 'Special vendor key is present in the returned data' assert data_actual[vendor_data_key] == { 'id': universal_id_should_be }, 'Vendor data is set with the right universal id'
def ad_account_set(ctx, scope, id=None, name='AdAccount', is_active=True, data=None): # PlatformToken.upsert(scope, token=TOKEN) # AssetScope.upsert(scope, platform_token_ids={scope}) if data: data = json.loads(data) else: data = {} a = AdAccountEntity.upsert(scope, gen_string_id() if id is None else id, is_active=is_active, **data) print(a.to_dict())
def ad_account_list(ctx): for aa in AdAccountEntity.scan(): print(aa)