Tag Archives: trac

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  🙂

01--- macros.py   (revision 300)
02+++ macros.py   (revision 301)
03@@ -10,6 +10,8 @@
04 from trac.wiki.macros import WikiMacroBase
05 from trac.wiki.formatter import wiki_to_html
06 from trac.web.chrome import Chrome, add_stylesheet, add_script
07-from trac.util.datefmt import format_datetime
08+from trac.util.datefmt import format_datetime, to_datetime
09+from datetime import datetime
10 
11 from tracusermanager.api import UserManager, User
12 from tracusermanager.profile.api import UserProfileManager, IUserProfilesListMacroCellContributor
13@@ -72,9 +73,29 @@
14             data['user_profiles'] = UserManager(self.env).search_users(user_profile_templates)
15         else:
16             data['user_profiles'] = UserManager(self.env).get_active_users()
17-
18+
19+        db = self.env.get_db_cnx()
20+        cursor = db.cursor()
21+        cursor.execute("SELECT sid,last_visit FROM session WHERE authenticated=1")
22+        last_visits={}
23+        for username, last_visit in cursor:
24+          for user in  data['user_profiles']:
25+              date_diff = datetime.now().date()  - datetime.date(to_datetime(last_visit))
26+              if date_diff.days > 1:
27+                user['last_visit'] = "%d days ago" % date_diff.days
28+              elif date_diff.days == 1:
29+                  user['last_visit'] = "%d day ago" % date_diff.days
30+              else:
31+                  user['last_visit'] = "today"
32+
33+              #user['last_visit'] = format_datetime(last_visit,"%b %d.%m.%Y %H:%M:%S")
34+
35+
36         data['cells']=list(self._get_cells(data['user_profiles']))
37-
38+
39         # add stylesheet&script
40         add_stylesheet(formatter.req,'tracusermanager/css/macros_um_profile.css')
41         add_script(formatter.req,'tracusermanager/js/macros_um_profile.js')
42@@ -102,8 +113,9 @@
43         yield ('name', _('Name'),0)
44         yield ('email', _('Email'),1)
45         yield ('role', _('Role'),2)
46+        yield ('last_visit', _('Last login'),3)
47     def render_userlistmacro_cell(self, cell_name, user):
48-        """Should render user cell"""
49+        """Should render user cell"""
50         return user[cell_name]
51 
52 class TeamRosterMacro(UserProfilesListMacro):

have fun

Trac, Watchlist plugin support for trac 0.12 microsecond feature

This is a patch for getting the WatchPlugin working with trac 0.12.

01+++ ./plugin.py    Sat Apr 03 16:56:17 2010
02@@ -26,6 +26,7 @@
03 
04 from  trac.env         import  IEnvironmentSetupParticipant
05 from  trac.util        import  format_datetime, pretty_timedelta
06+from trac.util.datefmt import  from_utimestamp, to_utimestamp
07 from  trac.web.chrome  import  INavigationContributor
08 from  trac.web.api     import  IRequestFilter, IRequestHandler, RequestDone
09 from  trac.web.chrome  import  ITemplateProvider, add_ctxtnav, add_link, add_script, add_notice
10@@ -38,6 +39,7 @@
11 from  trac.wiki.model  import  WikiPage
12 from  trac.ticket.model import Ticket
13 
14+
15 __DB_VERSION__ = 3
16 
17 class WatchlistError(TracError):
18@@ -347,9 +349,9 @@
19 'name' : name,
20 'author' : author,
21 'version' : version,
22-                        'datetime' : format_datetime( time ),
23-                        'timedelta' : pretty_timedelta( time ),
24-                        'timeline_link' : timeline_link( time ),
25+                        'datetime' : from_utimestamp( time ),
26+                        'timedelta' : pretty_timedelta( from_utimestamp(time) ),
27+                        'timeline_link' : timeline_link( from_utimestamp(time) ),
28 'comment' : comment,
29 'notify'  : notify,
30 })
31@@ -426,9 +428,9 @@
32 'author' : author,
33 'commentnum': to_unicode(self.commentnum),
34 'comment' : len(self.comment) <= 250 and self.comment or self.comment[:250] + '...',
35-                        'datetime' : format_datetime( changetime ),
36-                        'timedelta' : pretty_timedelta( changetime ),
37-                        'timeline_link' : timeline_link( changetime ),
38+                        'datetime' : from_utimestamp( changetime ),
39+                        'timedelta' : pretty_timedelta( from_utimestamp( changetime ) ),
40+                        'timeline_link' : timeline_link( from_utimestamp( changetime ) ),
41 'changes' : changes,
42 'summary' : summary,
43 'notify'  : notify,

This patch applies to rev 7710, you can download the patch from trac-Hacks

Have fun 🙂