import logging import structlog from django.conf import settings from rest_framework.throttling import UserRateThrottle from structlog.types import EventDict def structlog_add_app_info( logger: logging.Logger, method_name: str, event_dict: EventDict ) -> EventDict: event_dict["django_app"] = "vbv_lernwelt" event_dict["APP_ENVIRONMENT"] = settings.APP_ENVIRONMENT event_dict["django_app_dev_mode"] = f"vbv_lernwelt_{settings.APP_ENVIRONMENT}" return event_dict def structlog_inject_context_dict(test, level, event_dict): """ Add the structlog context dict to log events generated by the stdlib logging library. """ context_class = structlog.get_config().get("context_class") if context_class: for key, value in context_class().items(): if key not in event_dict: event_dict[key] = value return event_dict class HourUserRateThrottle(UserRateThrottle): scope = "hour-throttle" class DayUserRateThrottle(UserRateThrottle): scope = "day-throttle" def first_true(iterable, default=False, pred=None): """Returns the first true value in the iterable. If no true value is found, returns *default* If *pred* is not None, returns the first item for which pred(item) is true. """ # first_true([a,b,c], x) --> a or b or c or x # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x return next(filter(pred, iterable), default)