Skip to content

Commit 5f98aac

Browse files
authored
Merge pull request #4 from Ankitashinde0/master
Implemented feature of displaying date according to users timezone in logs.
2 parents c20a27e + 437eecb commit 5f98aac

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

src/auditlog/mixins.py

+17
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,24 @@ def disp_remote_addr(self,obj):
3030
disp_remote_addr.short_description = "IP Address"
3131

3232
class LogEntryAdminMixin(object):
33+
34+
#GOIP API used to get user location details.
35+
headers = {
36+
'accept': "application/json",
37+
'content-type': "application/json"
38+
}
39+
response = requests.request("GET", settings.AUDITLOG_GOIP_API, headers=headers)
40+
response_dict = json.loads(response.text) #converting response to dictionary
41+
tz_from_response = response_dict["time_zone"]
42+
3343
def created(self, obj):
44+
Audit_data = LogEntry.objects.all()
45+
for audit_obj in Audit_data:
46+
new_ts = audit_obj.timestamp.strftime("%m/%d/%Y %I:%M %p")
47+
new_ts = audit_obj.timestamp.strptime(new_ts,"%m/%d/%Y %I:%M %p")
48+
system_tz = pytz.timezone('UTC')
49+
local_tz = pytz.timezone(LogEntryAdminMixin.tz_from_response) #local tz set as timezone from response
50+
local_ts = system_tz.localize(new_ts).astimezone(local_tz) #returns datetime in the local timezone
3451
return obj.timestamp.strftime('%b. %d, %Y, %I:%M %p') #Displays date in diff format
3552
created.short_description = 'Date'
3653

0 commit comments

Comments
 (0)