-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TreyWW/feature/decorator_for_django_views
Added new decorator
- Loading branch information
Showing
2 changed files
with
33 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,42 @@ | ||
from functools import wraps | ||
from typing import Optional | ||
|
||
from django.http import HttpRequest | ||
|
||
from django_umami.core import umami, UmamiEventData | ||
|
||
|
||
def track(event_name: str, event_data: UmamiEventData = None): | ||
def track(event: UmamiEventData | str, event_data: Optional[UmamiEventData] = None): | ||
def decorator(func): | ||
@wraps(func) | ||
def wrapper(*args, **kwargs): | ||
umami.track(event_name, event_data) | ||
umami.track(event, event_data) | ||
return func(*args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator | ||
|
||
|
||
def track_visit(event_data: Optional[UmamiEventData] = None): | ||
def decorator(func): | ||
@wraps(func) | ||
def wrapper(request: HttpRequest, *args, **kwargs): | ||
new_event_data: UmamiEventData = event_data or UmamiEventData() | ||
|
||
data: UmamiEventData = UmamiEventData( | ||
url=request.path, | ||
referrer=request.META.get("HTTP_REFERER", ""), | ||
title=request.META.get("HTTP_TITLE", ""), | ||
) | ||
|
||
new_event_data.update( | ||
{k: v for k, v in data.items() if k not in new_event_data} # type: ignore[typeddict-item] | ||
) | ||
|
||
umami.track(new_event_data) | ||
return func(request, *args, **kwargs) | ||
|
||
return wrapper | ||
|
||
return decorator |