"""Logic for Marketing Highlights. Perform CRUD operations against the sales goals and art relations schema. """ import base64 from oto import response from sales_goals import features from sales_goals.constants import models from sales_goals.models.marketing_drivers import ( bulk_delete_marketing_drivers, delete_marketing_driver, fetch_digital_marketing_drivers, fetch_global_marketing_driver, fetch_marketing_drivers, upsert_digital_marketing_driver, upsert_global_marketing_driver, upsert_global_sync_driver, upsert_marketing_driver, ) def create_project_marketing_highlights(project_id, data): """Create or update project marketing highlights.""" for highlight_data in data: marketing_highlight = highlight_data['marketing_highlight'] if features.is_encoded_marketing_highlight_enabled(): # Decode the base64 encoded string, then decode the # UTF-8 encoding for the latin and special characters. marketing_highlight_base64_decode = \ base64.b64decode(marketing_highlight) marketing_highlight = marketing_highlight_base64_decode.decode() sync_highlight = highlight_data.get('sync_highlight') country_id = highlight_data.get('country_id') store = highlight_data.get('store') slogan = highlight_data.get('slogan') if country_id is not None: upsert_marketing_driver( project_id, country_id, marketing_highlight, slogan) elif store is not None: upsert_digital_marketing_driver( project_id, store, marketing_highlight) else: upsert_global_marketing_driver(project_id, marketing_highlight) if sync_highlight: upsert_global_sync_driver(project_id, sync_highlight) return response.Response({ 'ok': True, }, status=201) def upsert_project_marketing_highlights(project_id, data): """Upsert project marketing highlights.""" new_country_ids = [ int(highlight_data['country_id']) for highlight_data in data if highlight_data.get('country_id') ] current_highlights = get_project_marketing_highlights( project_id ).message or [] country_ids_to_remove = [] for highlight_data in current_highlights: country_id = highlight_data.get('country_id') if country_id and int(country_id) not in new_country_ids: country_ids_to_remove.append(int(country_id)) bulk_delete_marketing_drivers(project_id, country_ids_to_remove) return create_project_marketing_highlights(project_id, data) def get_project_marketing_highlights(project_id): """Get project marketing highlights.""" drivers = fetch_marketing_drivers(project_id) results = [] for driver in drivers: results.append({ 'marketing_highlight': driver.value, 'slogan': driver.slogan, 'country_id': driver.country_id, 'country_name': driver.country_name, }) results.append({ 'marketing_highlight': _get_global_project_highlight(project_id), 'sync_highlight': _get_global_project_sync_highlight(project_id), 'country_id': None, 'country_name': None, }) digital_drivers = fetch_digital_marketing_drivers(project_id) for driver in digital_drivers: results.append({ 'marketing_highlight': driver[0].value, 'store_id': driver[0].store_id, 'store_name': driver[1].store.lower(), 'customer_master_master_id': driver[1].customer_master_master_id }) return response.Response(results) def delete_project_marketing_highlight(project_id, territory_id): """Delete project marketing highlight.""" success = delete_marketing_driver(project_id, territory_id) if success: return response.Response({'ok': True}) return response.Response({'ok': False}) def _get_global_project_highlight(project_id): """Get the global marketing highlight text.""" global_driver = fetch_global_marketing_driver( project_id, models.MARKETING_HIGHLIGHTS_ID) if global_driver: return global_driver.description return '' def _get_global_project_sync_highlight(project_id): """Get the global sync highlight text.""" global_driver = fetch_global_marketing_driver( project_id, models.SYNC_HIGHLIGHTS_ID) if global_driver: return global_driver.description return ''