def get_session_key(): """ returns a 7 character string that varies with the current "session" (task or request) """ session_id = None current_task = get_current_task() if current_task: # at least in tests, current_task may have the same id between different tasks! session_id = current_task.request.id if not session_id: request = get_request() if request: session_id = str(id(get_request())) else: session_id = None if session_id: session_id = session_id.encode('utf-8') # hash it so that similar numbers end up very different (esp. because we're truncating) return hashlib.md5(session_id).hexdigest()[:7] else: # quickcache catches this and skips the cache # this happens during tests (outside a fake task/request context) # and during management commands raise ForceSkipCache("Not part of a session")
def notify_exception(request, message=None, details=None, exec_info=None): """ :param request: a Django request object :param message: message string :param details: dict with additional details to be included in the output """ if request is None: request = get_request() if request is not None: message = message or request.path if isinstance(message, bytes): message = message.decode('utf-8') message = 'Notify Exception: %s' % ( message or "No message provided, fix error handler" ) notify_logger.error( message, exc_info=exec_info or sys.exc_info(), extra={ 'status_code': 500, 'request': request, 'details': details, } )
def _handle_exceptions(*args, **kwargs): try: return fn(*args, **kwargs) except Exception as e: msg = "Uncaught exception from {}.{}".format(fn.__module__, fn.__name__) if mail_admins: notify_exception(get_request(), msg) else: logging.exception(msg)
def _send_message(info, backend): backend( subject='Soft Assert: [{}] {}'.format(info.key[:8], info.msg), message=('Message: {info.msg}\n' 'Value: {info.obj!r}\n' 'Traceback:\n{info.traceback}\n' 'Request:\n{request}\n' 'Occurrences to date: {info.count}\n').format( info=info, request=get_request()) )
def get_timezone_for_request(request=None): if request is None: request = get_request() user = getattr(request, 'couch_user', None) domain = getattr(request, 'domain', None) if user or domain: return get_timezone_for_user(user, domain) else: return None
def notify_submission_error(instance, exception, message): from corehq.util.global_request.api import get_request domain = getattr(instance, 'domain', '---') details = { 'domain': domain, 'error form ID': instance.form_id, } should_email = not isinstance(exception, CouchSaveAborted) # intentionally don't double-email these if should_email: request = get_request() notify_exception(request, message, details=details) else: logging.error(message, exc_info=sys.exc_info(), extra={'details': details})
def _send_message(info, backend): request = get_request() request_repr = get_sanitized_request_repr(request) backend( subject='Soft Assert: [{}] {}'.format(info.key[:8], info.msg), message=('Message: {info.msg}\n' 'Value: {info.obj!r}\n' 'Traceback:\n{info.traceback}\n' 'Request:\n{request}\n' 'Occurrences to date: {info.count}\n' 'Breadcrumbs: {info.breadcrumbs}\n').format( info=info, request=request_repr) )
def do_post_save_actions(case_db, xforms, case_stock_result): instance = xforms[0] try: case_stock_result.case_result.commit_dirtiness_flags() case_stock_result.stock_result.finalize() SubmissionPost._fire_post_save_signals(instance, case_stock_result.case_models) case_stock_result.case_result.close_extensions( case_db, "SubmissionPost-%s-close_extensions" % instance.form_id ) except PostSaveError: raise except Exception: notify_exception(get_request(), "Error performing post save actions during form processing", { 'domain': instance.domain, 'form_id': instance.form_id, }) raise PostSaveError
def send_subscription_change_alert(domain, new_subscription, old_subscription, internal_change): billing_account = ( new_subscription.account if new_subscription else old_subscription.account if old_subscription else None ) # this can be None, though usually this will be initiated # by an http request request = get_request() email_context = { 'domain': domain, 'domain_url': get_default_domain_url(domain), 'old_plan': old_subscription.plan_version if old_subscription else None, 'new_plan': new_subscription.plan_version if new_subscription else None, 'old_subscription': old_subscription, 'new_subscription': new_subscription, 'billing_account': billing_account, 'username': request.couch_user.username if getattr(request, 'couch_user', None) else None, 'referer': request.META.get('HTTP_REFERER') if request else None, } email_subject = "{env}Subscription Change Alert: {domain} from {old_plan} to {new_plan}".format( env=("[{}] ".format(settings.SERVER_ENVIRONMENT.upper()) if settings.SERVER_ENVIRONMENT == "staging" else ""), domain=email_context['domain'], old_plan=email_context['old_plan'], new_plan=email_context['new_plan'], ) sub_change_email_address = (settings.INTERNAL_SUBSCRIPTION_CHANGE_EMAIL if internal_change else settings.SUBSCRIPTION_CHANGE_EMAIL) send_html_email_async.delay( email_subject, sub_change_email_address, render_to_string('accounting/email/subscription_change.html', email_context), text_content=render_to_string('accounting/email/subscription_change.txt', email_context), )
def get_ip_address(): request = get_request() if request: return get_ip(request) else: return None
def get_request(): return global_request.get_request()
def _domain_has_legacy_toggle_set(): # old versions of commcare (< 2.10ish) didn't purge on form completion # so can still modify cases that should no longer be on the phone. request = get_request() domain = getattr(request, 'domain', None) return LEGACY_SYNC_SUPPORT.enabled(domain) if domain else False