1
1
import json
2
-
2
+ from django .utils .html import mark_safe
3
+ import datetime
3
4
from django .conf import settings
5
+ from .middleware import AuditlogMiddleware
6
+ import threading
7
+ import time
8
+
9
+
4
10
try :
5
11
from django .core import urlresolvers
6
12
except ImportError :
9
15
from django .urls .exceptions import NoReverseMatch
10
16
except ImportError :
11
17
from django .core .urlresolvers import NoReverseMatch
12
- from django .utils .html import format_html
13
- from django .utils .safestring import mark_safe
18
+ try :
19
+ from django .utils .deprecation import MiddlewareMixin
20
+ except ImportError :
21
+ MiddlewareMixin = object
14
22
15
23
MAX = 75
16
24
25
+ threadlocal = threading .local ()
17
26
18
- class LogEntryAdminMixin (object ):
27
+ class MiddlewareMixinclass (MiddlewareMixin ):
28
+ def disp_remote_addr (self ,obj ):
29
+ return obj .remote_addr
30
+ disp_remote_addr .short_description = "IP Address"
19
31
32
+ class LogEntryAdminMixin (object ):
20
33
def created (self , obj ):
21
- return obj .timestamp .strftime ('%Y-%m-%d %H:%M:%S' )
22
- created .short_description = 'Created'
34
+ return obj .timestamp .strftime ('%b. %d, %Y, %I:%M %p' ) #Displays date in diff format
35
+ created .short_description = 'Date'
36
+
37
+ def entity_type (self ,obj ):
38
+ #entity type for KLC objects
39
+ if obj .content_type_id == 0 :
40
+ return "N/A"
41
+ if obj .content_type_id == 8 or obj .content_type_id == 9 : #Displays client for all models inside it
42
+ return "client"
43
+ if obj .content_type_id == 90 or obj .content_type_id == 10 : #Displays user for all models inside it
44
+ return "user"
45
+
46
+ return obj .content_type .model
47
+ entity_type .short_description = "Entity type"
23
48
24
49
def user_url (self , obj ):
25
50
if obj .actor :
@@ -28,10 +53,10 @@ def user_url(self, obj):
28
53
try :
29
54
link = urlresolvers .reverse (viewname , args = [obj .actor .id ])
30
55
except NoReverseMatch :
31
- return u'%s' % (obj .actor )
32
- return format_html ( u'<a href="{}">{}</a>' , link , obj .actor )
33
-
34
- return 'system'
56
+ return (obj .actor )
57
+ return ( obj .actor )
58
+ return ( obj . object_repr ) #Previously returned system ,now changed to return object_repr(username) to display username in last_login entries
59
+ user_url . allow_tags = True #Returns user whose last_login is changed which is the username itself.
35
60
user_url .short_description = 'User'
36
61
37
62
def resource_url (self , obj ):
@@ -41,32 +66,50 @@ def resource_url(self, obj):
41
66
args = [obj .object_pk ] if obj .object_id is None else [obj .object_id ]
42
67
link = urlresolvers .reverse (viewname , args = args )
43
68
except NoReverseMatch :
69
+ obj_store = obj .object_repr
70
+ print ('obj_store---- in alice test----' ,obj_store )
71
+ print ("type(obj_store-----1st in alice test----" ,type (obj_store ))
72
+ print (obj_store .isnumeric ())
44
73
return obj .object_repr
45
74
else :
46
- return format_html (u'<a href="{}">{}</a>' , link , obj .object_repr )
47
- resource_url .short_description = 'Resource'
75
+ obj_store = str (obj .object_repr )
76
+ return (obj .object_repr )
77
+ resource_url .allow_tags = True
78
+ resource_url .short_description = 'Entity name'
48
79
49
80
def msg_short (self , obj ):
50
81
if obj .action == 2 :
51
- return '' # delete
82
+ return 'Deleted object' # delete
83
+ if obj .action == 3 or obj .action == 4 or obj .action == 1 and obj .content_type_id == 108 and obj .additional_data == "Client_name" : #to display changes for actions of download-3,KLC-4
84
+ return obj .changes #and also when update action-1 is done for client name in Client Group
52
85
changes = json .loads (obj .changes )
53
86
s = '' if len (changes ) == 1 else 's'
54
87
fields = ', ' .join (changes .keys ())
55
88
if len (fields ) > MAX :
56
89
i = fields .rfind (' ' , 0 , MAX )
57
90
fields = fields [:i ] + ' ..'
58
91
return '%d change%s: %s' % (len (changes ), s , fields )
59
- msg_short .short_description = 'Changes '
92
+ msg_short .short_description = 'Description '
60
93
61
94
def msg (self , obj ):
62
95
if obj .action == 2 :
63
96
return '' # delete
64
97
changes = json .loads (obj .changes )
65
- msg = '<table><tr><th># </th><th>Field</th><th>From</th><th>To</th></tr>'
98
+ msg = '<table><tr><th>No. </th><th>Field</th><th>From</th><th>To</th></tr>'
66
99
for i , field in enumerate (sorted (changes ), 1 ):
67
- value = [i , field ] + (['***' , '***' ] if field == 'password' else changes [field ])
68
- msg += format_html ('<tr><td>{}</td><td>{}</td><td>{}</td><td>{}</td></tr>' , * value )
69
-
100
+ value = [i , field ] + (['***' , '***' ] if field == 'password' or field == 'kaseya_password' or field == 'webroot_password' or field == 'db_password' or field == 'nlm_server_password' else changes [field ]) #to display values of password fields as ****
101
+ #changes for last_login field in logging entries
102
+ if field == 'last_login' :
103
+ for i in range (len (changes [field ])): #iterating list
104
+ ologin_date = changes [field ][0 ] #storing value at index 0 as old login date
105
+ ologin_date = datetime .datetime .strptime (ologin_date , '%Y-%m-%d %H:%M:%S.%f' ) #converting string into datetime obj
106
+ ologin_date = ologin_date .strftime ("%m/%d/%Y %I:%M %p" ) #converting date into string type
107
+ nlogin_date = changes [field ][1 ] #storing value at index 1 as new login date
108
+ nlogin_date = datetime .datetime .strptime (nlogin_date , '%Y-%m-%d %H:%M:%S.%f' ) #converting string into datetime obj
109
+ nlogin_date = nlogin_date .strftime ("%m/%d/%Y %I:%M %p" ) #converting date into string type
110
+ value = [i ,field ] + [ologin_date ,nlogin_date ]
111
+ msg += '<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>' % tuple (value )
70
112
msg += '</table>'
71
- return mark_safe (msg )
113
+ return mark_safe (msg ) #mark_safe is used to return html code in Python
114
+ msg .allow_tags = True
72
115
msg .short_description = 'Changes'
0 commit comments