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 test_populate_from_scope_record(self): scope_id = gen_string_id() sweep_id = gen_string_id() console_token = 'console token' platform_token = 'platform token' scope_record = AssetScope() scope_record.scope = scope_id scope_record.scope_api_token = console_token scope_record.set_cache(platform_tokens={platform_token}) PlatformTokenManager.populate_from_scope_entity(scope_record, sweep_id) # now let's make sure we see those tokens: # Scope-centered jobs must result in scope-centered key for token storage job_scope = JobScope(sweep_id=sweep_id, entity_type=Entity.Scope, entity_id=scope_id) assert console_token == PlatformTokenManager.from_job_scope( job_scope).get_best_token() job_scope = JobScope( sweep_id=sweep_id, # uses .namespace default value as 2nd value in redis key. no need to set here. ) assert platform_token == PlatformTokenManager.from_job_scope( job_scope).get_best_token()
def page_remote_view(cts, scope, id=None, token=None): from oozer.common.facebook_api import PlatformApiContext, get_default_fields from facebook_business.adobjects.user import User if not token: scope = AssetScope.get(scope) token = PlatformToken.get(list(scope.platform_token_ids)[0]) with PlatformApiContext(token.token) as fb_ctx: pages = User(fbid='me', api=fb_ctx.api).get_accounts() for page in pages: print(page)
def ad_account_remote_view(cts, scope, id, token=None): from oozer.common.facebook_api import PlatformApiContext, get_default_fields from common.enums.entity import Entity if not token: scope = AssetScope.get(scope) token = PlatformToken.get(list(scope.platform_token_ids)[0]) with PlatformApiContext(token.token) as fb_ctx: ad_account = fb_ctx.to_fb_model(id, Entity.AdAccount) fields = get_default_fields(ad_account.__class__) ad_account_with_selected_fields = ad_account.api_get( fields=['id', 'name']) # Read just the fields we need ad_account_data_dict = ad_account_with_selected_fields.export_all_data( ) # Export the object to a dict print(ad_account_data_dict)
def scope_model(self): from common.store.scope import AssetScope return AssetScope.get(self.scope)
def scope_set(ctx, scope, token): PlatformToken.upsert(scope, token=token) AssetScope.upsert(scope, platform_token_ids={scope})
def scope_list(ctx): for scope in AssetScope.scan(): for token_id in scope.platform_token_ids: pt = PlatformToken.get(token_id) print(scope.scope, pt.token)