def _set_request_authenticated_user_found_in_middleware_attribute(self): """ Add custom attribute 'request_authenticated_user_found_in_middleware' if authenticated user was found. """ cached_response = DEFAULT_REQUEST_CACHE.get_cached_response( self.AUTHENTICATED_USER_FOUND_CACHE_KEY) if cached_response.is_found: monitoring.set_custom_attribute( 'request_authenticated_user_found_in_middleware', cached_response.value)
def flush(self): """Remove all products in basket and fire Segment 'Product Removed' Analytic event for each""" cached_response = DEFAULT_REQUEST_CACHE.get_cached_response(TEMPORARY_BASKET_CACHE_KEY) if cached_response.is_found: # Do not track anything. This is a temporary basket calculation. return for line in self.all_lines(): # Do not fire events for free items. The volume we see for edX.org leads to a dramatic increase in CPU # usage. Given that orders for free items are ignored, there is no need for these events. if line.stockrecord.price_excl_tax > 0: properties = translate_basket_line_for_segment(line) track_segment_event(self.site, self.owner, 'Product Removed', properties) # Call flush after we fetch all_lines() which is cleared during flush() super(Basket, self).flush() # pylint: disable=bad-super-call
def _cache_if_authenticated_user_found_in_middleware(self, request, value): """ Updates the cached process step in which the authenticated user was found, if it hasn't already been found. """ cached_response = DEFAULT_REQUEST_CACHE.get_cached_response( self.AUTHENTICATED_USER_FOUND_CACHE_KEY) if cached_response.is_found: # since we are tracking the earliest point the authenticated user was found, # and the value was already set in earlier middleware step, do not set again. return if hasattr(request, 'user') and request.user and request.user.is_authenticated: DEFAULT_REQUEST_CACHE.set(self.AUTHENTICATED_USER_FOUND_CACHE_KEY, value)
def _set_view_func_compare_metric(self, view_func): """ Set temporary metric to ensure that the view_func of `process_view` always matches the one from using `resolve` on the request. """ try: view_func_module = view_func.__module__ cached_response = DEFAULT_REQUEST_CACHE.get_cached_response( self._VIEW_FUNC_MODULE_METRIC_CACHE_KEY) if cached_response.is_found: view_func_compare = 'success' if view_func_module == cached_response.value else view_func_module else: view_func_compare = 'missing' set_custom_metric('temp_view_func_compare', view_func_compare) except Exception as e: set_custom_metric('temp_view_func_compare_error', e)
def flush(self): """Remove all products in basket and fire Segment 'Product Removed' Analytic event for each""" cached_response = DEFAULT_REQUEST_CACHE.get_cached_response( TEMPORARY_BASKET_CACHE_KEY) if cached_response.is_found: # Do not track anything. This is a temporary basket calculation. return product_removed_event_fired = False for line in self.all_lines(): # Do not fire events for free items. The volume we see for edX.org leads to a dramatic increase in CPU # usage. Given that orders for free items are ignored, there is no need for these events. if line.stockrecord.price_excl_tax > 0: properties = translate_basket_line_for_segment(line) track_segment_event(self.site, self.owner, 'Product Removed', properties) product_removed_event_fired = True # Validate we sent an event for > 0 products to check if the bundle event is even necessary if product_removed_event_fired: try: bundle_id = BasketAttribute.objects.get( basket=self, attribute_type__name=BUNDLE).value_text program = get_program(bundle_id, self.site.siteconfiguration) bundle_properties = { 'bundle_id': bundle_id, 'title': program.get('title'), 'total_price': self.total_excl_tax, 'quantity': self.lines.count(), } if program.get( 'type_attrs', {}).get('slug') and program.get('marketing_slug'): bundle_properties['marketing_slug'] = ( program['type_attrs']['slug'] + '/' + program.get('marketing_slug')) track_segment_event(self.site, self.owner, 'edx.bi.ecommerce.basket.bundle_removed', bundle_properties) except BasketAttribute.DoesNotExist: # Nothing to do here. It's not a bundle ¯\_(ツ)_/¯ pass # Call flush after we fetch all_lines() which is cleared during flush() super(Basket, self).flush() # pylint: disable=bad-super-call
def add_product(self, product, quantity=1, options=None): """ Add the indicated product to basket. Performs AbstractBasket add_product method and fires Google Analytics 'Product Added' event. """ line, created = super(Basket, self).add_product(product, quantity, options) # pylint: disable=bad-super-call cached_response = DEFAULT_REQUEST_CACHE.get_cached_response(TEMPORARY_BASKET_CACHE_KEY) if cached_response.is_found: # Do not track anything. This is a temporary basket calculation. return line, created # Do not fire events for free items. The volume we see for edX.org leads to a dramatic increase in CPU # usage. Given that orders for free items are ignored, there is no need for these events. if line.stockrecord.price_excl_tax > 0: properties = translate_basket_line_for_segment(line) properties['cart_id'] = self.id track_segment_event(self.site, self.owner, 'Product Added', properties) return line, created