Dennis
Dennis
DevOps Engineer A.I Integration Recommendation Engine Fine-tuning AI models WebRTC Development RPC Systems Chatbot Developement
Dennis

Blog

How to Implement Activity Logs API

How to Implement Activity Logs API

It's crucial to keep track of user activity, especially user logins, for security and auditing purposes. By implementing activity logs, you can monitor successful and failed login attempts and record additional information such as the user's IP address. In this blog post, we'll explore how to implement user login activity logs with IP addresses in a Django app using Django Rest Framework.

Prerequisites

Before we begin, ensure that you have Django and Django Rest Framework installed. If not, you can install them using the following commands:

Step 1: Add the ActivityLog Model
To start, we'll update the ActivityLog model to include fields for the user's login status, IP address, and other relevant information. Here's an example of the updated model:

In this model, we added a status field to store the login status (successful or failed) and an ip_address field to store the user's IP address. The user field is a foreign key to the Django User model, allowing us to associate each activity log with a specific user.

Step 2: Add the Serializer
Add the ActivityLogSerializer to include the the following fields

With this serializer, we can control how the activity log data is serialized and deserialized when interacting with the Django Rest Framework API.

Step 3: Add the API View
Implement the ActivityLogViewSet to include a custom method for handling user login activity. In this method, you can retrieve the user's IP address and create an activity log based on the login status. Here's an example:

...to be Continued

 

Add Comment