o
    \h=                     @   sZ   d dl mZmZ d dlmZ er$d dlmZmZ d dl mZm	Z	m
Z
mZ G dd dZdS )    )TYPE_CHECKINGcast)scopes)HttpRequestHttpResponse)CallableDictAnyOptionalc                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	PosthogContextMiddlewarea  Middleware to automatically track Django requests.

    This middleware wraps all calls with a posthog context. It attempts to extract the following from the request headers:
    - Session ID, (extracted from `X-POSTHOG-SESSION-ID`)
    - Distinct ID, (extracted from `X-POSTHOG-DISTINCT-ID`)
    - Request URL as $current_url
    - Request Method as $request_method

    The context will also auto-capture exceptions and send them to PostHog, unless you disable it by setting
    `POSTHOG_MW_CAPTURE_EXCEPTIONS` to `False` in your Django settings.

    The middleware behaviour is customisable through 3 additional functions:
    - `POSTHOG_MW_EXTRA_TAGS`, which is a Callable[[HttpRequest], Dict[str, Any]] expected to return a dictionary of additional tags to be added to the context.
    - `POSTHOG_MW_REQUEST_FILTER`, which is a Callable[[HttpRequest], bool] expected to return `False` if the request should not be tracked.
    - `POSTHOG_MW_TAG_MAP`, which is a Callable[[Dict[str, Any]], Dict[str, Any]], which you can use to modify the tags before they're added to the context.

    You can use the `POSTHOG_MW_TAG_MAP` function to remove any default tags you don't want to capture, or override them with your own values.

    Context tags are automatically included as properties on all events captured within a context, including exceptions.
    See the context documentation for more information. The extracted distinct ID and session ID, if found, are used to
    associate all events captured in the middleware context with the same distinct ID and session as currently active on the
    frontend. See the documentation for `set_context_session` and `identify_context` for more details.
    c                 C   s   || _ ddlm} t|drt|jrtd|j| _nd | _t|dr0t|jr0td|j| _	nd | _	t|drEt|j
rEtd|j
| _nd | _t|d	rYt|jtrY|j| _d S d
| _d S )Nr   )settingsPOSTHOG_MW_EXTRA_TAGSz1Optional[Callable[[HttpRequest], Dict[str, Any]]]POSTHOG_MW_REQUEST_FILTERz'Optional[Callable[[HttpRequest], bool]]POSTHOG_MW_TAG_MAPz4Optional[Callable[[Dict[str, Any]], Dict[str, Any]]]POSTHOG_MW_CAPTURE_EXCEPTIONST)get_responsedjango.confr   hasattrcallabler   r   
extra_tagsr   request_filterr   tag_map
isinstancer   boolcapture_exceptions)selfr   r    r   ^/home/air/segue/gemini/backup/venv/lib/python3.10/site-packages/posthog/integrations/django.py__init__"   s>   
z!PosthogContextMiddleware.__init__c                 C   s   i }|j d}|rt| |j d}|rt| | }|r&||d< |jr.|j|d< | jr=| |}|r=|| | j	rE| 	|}|S )NzX-POSTHOG-SESSION-IDzX-POSTHOG-DISTINCT-IDz$current_urlz$request_method)
headersgetr   set_context_sessionidentify_contextbuild_absolute_urimethodr   updater   )r   requesttags
session_iddistinct_idabsolute_urlextrar   r   r   extract_tagsM   s&   





z%PosthogContextMiddleware.extract_tagsc                 C   sx   | j r|  |s| |S t| j | | D ]
\}}t|| q| |W  d    S 1 s5w   Y  d S )N)r   r   r   new_contextr   r,   itemstag)r   r&   kvr   r   r   __call__p   s   
$z!PosthogContextMiddleware.__call__N)__name__
__module____qualname____doc__r   r,   r2   r   r   r   r   r   	   s
    +#r   N)typingr   r   posthogr   django.httpr   r   r   r   r	   r
   r   r   r   r   r   <module>   s    