Skip to content

Commit

Permalink
done: #187 - activity log in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
rkshaon committed Dec 2, 2024
1 parent 6053e01 commit dd6b16f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
49 changes: 46 additions & 3 deletions backend/BookShelf/middlewares/activity.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
# from django.utils.timezone import now
from django.conf import settings
from django_user_agents.utils import get_user_agent

import json
import time

from BookShelf.utilities.client import get_client_ip
from BookShelf.utilities.client import get_device_type

from activity_api.models import Device
from activity_api.models import ActivityLog


Expand All @@ -21,7 +24,12 @@ def __call__(self, request):
request.start_time = time.time()
log_entry = self.log_request_start(request)
response = self.get_response(request)
self.log_response_end(log_entry, request)
ip_address = get_client_ip(request)
self.log_response_end(
request,
log_entry,
ip_address
)

return response

Expand All @@ -38,15 +46,19 @@ def log_request_start(self, request):
'path': request.path,
'query_params': json.dumps(request.GET.dict()),
'body': json.dumps(request.POST.dict() if request.POST else None),
'ip_address': get_client_ip(request),
'user_agent': request.META.get('HTTP_USER_AGENT'),
}

log_entry = ActivityLog.objects.create(**activity_data)

return log_entry

def log_response_end(self, log_entry, request):
def log_response_end(
self,
request,
log_entry,
ip_address,
):
if not log_entry:
return

Expand All @@ -56,5 +68,36 @@ def log_response_end(self, log_entry, request):
if user and not log_entry.user:
log_entry.user = user

log_entry.ip_address = ip_address
log_entry.device = self.get_log_device(
request,
user,
ip_address,
)
log_entry.duration = duration
log_entry.save()

def get_log_device(
self,
request,
user,
ip_address
):
user_agent = get_user_agent(request)
device, created = Device.objects.get_or_create(
user=user,
user_agent=user_agent.ua_string,
device_type=get_device_type(
user_agent.is_mobile,
user_agent.is_tablet,
user_agent.is_pc
),
browser=user_agent.browser.family,
browser_version=user_agent.browser.version_string,
os=user_agent.os.family,
os_version=user_agent.os.version_string,
ip_address=ip_address,
screen_resolution=None,
)

return device
15 changes: 15 additions & 0 deletions backend/BookShelf/utilities/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,18 @@ def get_client_ip(request):
if x_forwarded_for:
return x_forwarded_for.split(',')[0]
return request.META.get('REMOTE_ADDR')


def get_device_type(
is_mobile: bool,
is_tablet: bool,
is_pc: bool
) -> str:
"""Return device type based on user agent."""
if is_mobile:
return "mobile"
if is_tablet:
return "tablet"
if is_pc:
return "pc"
# return "Unknown"
1 change: 1 addition & 0 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ elasticsearch-dsl
django-elasticsearch-dsl
django-redis
PyPDF2
django-user-agents

0 comments on commit dd6b16f

Please sign in to comment.