Last login patch for Trac UserManager plugin

I just have added a visit motivation feature to the Trac Usermanager plugin. With the included patch set you will get an additional column Last Login in the “UserProfilesList” macro.

This patch applies to UserManager plugin rev 5520  🙂

--- macros.py	(revision 300)
+++ macros.py	(revision 301)
@@ -10,6 +10,8 @@
 from trac.wiki.macros import WikiMacroBase
 from trac.wiki.formatter import wiki_to_html
 from trac.web.chrome import Chrome, add_stylesheet, add_script
-from trac.util.datefmt import format_datetime
+from trac.util.datefmt import format_datetime, to_datetime
+from datetime import datetime

 from tracusermanager.api import UserManager, User
 from tracusermanager.profile.api import UserProfileManager, IUserProfilesListMacroCellContributor
@@ -72,9 +73,29 @@
             data['user_profiles'] = UserManager(self.env).search_users(user_profile_templates)
         else:
             data['user_profiles'] = UserManager(self.env).get_active_users()
-
+
+        db = self.env.get_db_cnx()
+        cursor = db.cursor()
+        cursor.execute("SELECT sid,last_visit FROM session WHERE authenticated=1")
+        last_visits={}
+        for username, last_visit in cursor:
+          for user in  data['user_profiles']:
+              date_diff = datetime.now().date()  - datetime.date(to_datetime(last_visit))
+              if date_diff.days > 1:
+                user['last_visit'] = "%d days ago" % date_diff.days
+              elif date_diff.days == 1:
+                  user['last_visit'] = "%d day ago" % date_diff.days
+              else:
+                  user['last_visit'] = "today"
+
+              #user['last_visit'] = format_datetime(last_visit,"%b %d.%m.%Y %H:%M:%S")
+
+
         data['cells']=list(self._get_cells(data['user_profiles']))
-
+
         # add stylesheet&script
         add_stylesheet(formatter.req,'tracusermanager/css/macros_um_profile.css')
         add_script(formatter.req,'tracusermanager/js/macros_um_profile.js')
@@ -102,8 +113,9 @@
         yield ('name', _('Name'),0)
         yield ('email', _('Email'),1)
         yield ('role', _('Role'),2)
+        yield ('last_visit', _('Last login'),3)
     def render_userlistmacro_cell(self, cell_name, user):
-        """Should render user cell"""
+        """Should render user cell"""
         return user[cell_name]

 class TeamRosterMacro(UserProfilesListMacro):

have fun

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.