Changeset 2335

Show
Ignore:
Timestamp:
06/25/07 16:55:40 (1 year ago)
Author:
coling
Message:

WorkLogPlugin:

Refactored some code:

  • Moved the current task info function to util.py
  • Moved the 'liberated' Trac pretty_timedelta function to util.py
  • Add time spent display to the Timeline
  • Remove some manual SQL in favour of utility function
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • worklogplugin/0.10/worklog/dbhelper.py

    r2330 r2335  
    8484    return ResultSet(get_all(db, sql, *params)) 
    8585 
    86 def get_active_task(env, authname): 
    87     sql = "SELECT MAX(lastchange) FROM work_log WHERE user='%s'" % (authname) 
    88     lastchange = get_scalar(env.get_db_cnx(), sql) 
    89     if not lastchange: 
    90         return None 
    91  
    92     sql = "SELECT user,ticket,starttime,endtime FROM work_log WHERE user='%s' AND lastchange=%s" % (authname, lastchange) 
    93     data = get_all(env.get_db_cnx(), sql)[1] 
    94     task = {} 
    95     for row in data: 
    96         task["user"] = row[0] 
    97         task["ticket"] = row[1] 
    98         task["starttime"] = row[2] 
    99         task["endtime"] = row[3] 
    100     return task 
    101  
    102  
    10386class ResultSet: 
    10487    """ the result of calling getResultSet """ 
  • worklogplugin/0.10/worklog/timeline_hook.py

    r2334 r2335  
    11import re 
    2 import dbhelper 
     2from util import * 
    33from trac.log import logger_factory 
    44from trac.core import * 
     
    2828            cursor = db.cursor() 
    2929 
    30             cursor.execute("""SELECT wl.user,wl.ticket,wl.time,wl.kind,wl.humankind,t.summary 
     30            cursor.execute("""SELECT wl.user,wl.ticket,wl.time,wl.starttime,wl.kind,wl.humankind,t.summary 
    3131                             FROM ( 
    3232                              
    33                              SELECT user, ticket, starttime AS time, 'workstart' AS kind, 'started' AS humankind 
     33                             SELECT user, ticket, starttime AS time, starttime, 'workstart' AS kind, 'started' AS humankind 
    3434                             FROM work_log 
    3535 
    3636                             UNION 
    3737 
    38                              SELECT user, ticket, endtime AS time, 'workstop' AS kind, 'stopped' AS humankind 
     38                             SELECT user, ticket, endtime AS time, starttime, 'workstop' AS kind, 'stopped' AS humankind 
    3939                             FROM work_log 
    4040 
     
    4545                           % (start, stop)) 
    4646            previous_update = None 
    47             for user,ticket,time,kind,humankind,summary in cursor: 
     47            for user,ticket,time,starttime,kind,humankind,summary in cursor: 
    4848                ticket_href = href.ticket(ticket) 
    4949                if format == 'rss': 
     
    5454                                   (user, humankind, summary, ticket)) 
    5555                message = '' 
     56                if kind == 'workstop': 
     57                    started = datetime.fromtimestamp(starttime) 
     58                    finished = datetime.fromtimestamp(time) 
     59                    message = 'Time spent: ~' + pretty_timedelta(started, finished) 
    5660                yield kind, ticket_href, title, time, user, message 
    5761 
  • worklogplugin/0.10/worklog/uihooks_ticket.py

    r2330 r2335  
    11import re 
    2 import dbhelper 
     2from util import * 
    33from trac.log import logger_factory 
    44from trac.core import * 
     
    6666         
    6767    def get_navigation_items(self, req): 
     68        if req.authname == 'anonymous': 
     69            return 
     70         
    6871        match = re.match(r'/ticket/([0-9]+)$', req.path_info) 
    6972        if match: 
     
    7174 
    7275            # Check to see if y 
    73             workingon = dbhelper.get_active_task(self.env, req.authname) 
    74             if not workingon: 
    75                 workingon = { 'starttime': 1, 
    76                               'endtime': 1, 
    77                               'ticket': 0} 
     76            task = get_latest_task(self.env.get_db_cnx(), req.authname) 
    7877 
    79             # Check state 
    80             if not workingon['endtime'] == 0: 
    81                 # Should display a "Work on Link" button. 
     78            # If we are not working on anything or our latest task is finished we 
     79            # can start working on this task. 
     80            if not task or not task['endtime'] == 0: 
     81                # Display a "Work on Link" button. 
    8282                yield 'mainnav', "ticket-addon", self.get_javascript(req, ticket, 0) 
    8383                return 
    84  
     84             
    8585            # We are working on SOMETHING, but not this ticket... 
    86             if not workingon['ticket'] == ticket: 
    87                 return 
    88  
    89             yield 'mainnav', "ticket-addon", self.get_javascript(req, ticket, 1) 
     86            if task and task['ticket'] == ticket: 
     87                yield 'mainnav', "ticket-addon", self.get_javascript(req, ticket, 1) 
  • worklogplugin/0.10/worklog/webui.py

    r2330 r2335  
    11import re 
    22import dbhelper 
     3from util import * 
    34from time import time 
    45from datetime import tzinfo, timedelta, datetime 
     
    3435                  Markup('<a href="%s">%s</a>' % \ 
    3536                         (url , "Work Log")) 
    36  
    37     # Stolen from Trac trunk :) 
    38     def pretty_timedelta(self, time1, time2=None, resolution=None): 
    39         """Calculate time delta (inaccurately, only for decorative purposes ;-) for 
    40         prettyprinting. If time1 is None, the current time is used.""" 
    41         if not time1: time1 = datetime.now() 
    42         if not time2: time2 = datetime.now() 
    43         if time1 > time2: 
    44             time2, time1 = time1, time2 
    45         units = ((3600 * 24 * 365, 'year',   'years'), 
    46                  (3600 * 24 * 30,  'month',  'months'), 
    47                  (3600 * 24 * 7,   'week',   'weeks'), 
    48                  (3600 * 24,       'day',    'days'), 
    49                  (3600,            'hour',   'hours'), 
    50                  (60,              'minute', 'minutes')) 
    51         diff = time2 - time1 
    52         age_s = int(diff.days * 86400 + diff.seconds) 
    53         if resolution and age_s < resolution: 
    54             return '' 
    55         if age_s < 60: 
    56             return '%i second%s' % (age_s, age_s != 1 and 's' or '') 
    57         for u, unit, unit_plural in units: 
    58             r = float(age_s) / float(u) 
    59             if r >= 0.9: 
    60                 r = int(round(r)) 
    61                 return '%d %s' % (r, r == 1 and unit or unit_plural) 
    62         return '' 
    6337 
    6438    # IRequestHandler methods 
     
    10175                          "started": started, 
    10276                          "delta": 'Started ' + format_date(starttime) + " " + format_time(starttime) + \ 
    103                                    " (" + self.pretty_timedelta(started) + " ago)" 
     77                                   " (" + pretty_timedelta(started) + " ago)" 
    10478                          } 
    10579 
     
    10882                log[user]["finished"] = finished 
    10983                log[user]["delta"] = 'Worked on from ' + format_date(starttime) + " " + format_time(starttime) + " - " + format_date(endtime) + " " + format_time(endtime) + \ 
    110                                    " (" + self.pretty_timedelta(started, finished) + ")" 
     84                                   " (" + pretty_timedelta(started, finished) + ")" 
    11185 
    11286                 
     
    130104            tckt_link = Markup('#' + ticket) 
    131105 
    132             sql = "SELECT MAX(lastchange) FROM work_log WHERE user='%s'" % (authname) 
    133             lastchange = dbhelper.get_scalar(self.env.get_db_cnx(), sql) 
    134             if lastchange: 
    135                 sql = "SELECT endtime FROM work_log WHERE user='%s' AND lastchange=%s" % (authname, lastchange) 
    136                 endtime = dbhelper.get_scalar(self.env.get_db_cnx(), sql) 
    137                 if endtime == 0: 
    138                     addMessage("You cannot work on ticket " + tckt_link + " as you seem to already be working on another ticket.") 
    139                     return 
    140              
     106            task = get_latest_task(self.env.get_db_cnx(), authname) 
     107            if task and task['endtime'] == 0: 
     108                addMessage("You cannot work on ticket " + tckt_link + " as you seem to already be working on another ticket.") 
     109                return 
     110 
    141111            if not authname == tckt['owner']: 
    142112                addMessage("You cannot work on ticket " + tckt_link + " as you are not the owner.") 
     
    151121            sql = "INSERT INTO work_log (user, ticket, lastchange, starttime, endtime) VALUES ('%s', %s, %s, %s, %s)" % \ 
    152122                  (authname, ticket, now, now, 0) 
    153             dbhelper.execute_non_query(self.env.get_db_cnx(), sql) 
     123            db = self.env.get_db_cnx(); 
     124            cursor = db.cursor() 
     125            cursor.execute(sql) 
    154126            addMessage("You are now working on ticket " + tckt_link + ".") 
    155127 
     
    160132            tckt_link = Markup('#' + ticket) 
    161133 
    162             sql = "SELECT MAX(lastchange) FROM work_log WHERE user='%s'" % (authname) 
    163             lastchange = dbhelper.get_scalar(self.env.get_db_cnx(), sql) 
    164             if not lastchange: 
     134            task = get_latest_task(self.env.get_db_cnx(), authname) 
     135            if not task: 
    165136                addMessage("You cannot stop working on ticket " + tckt_link + " as it apears you've not started working on anything yet!") 
    166137                return 
    167138             
    168             sql = "SELECT ticket,endtime FROM work_log WHERE user='%s' AND lastchange=%s" % (authname, lastchange) 
    169             data = dbhelper.get_all(self.env.get_db_cnx(), sql)[1] 
    170             if not data: 
     139            if not task['endtime'] == 0: 
    171140                addMessage("You cannot stop working on ticket " + tckt_link + " as it appears you've not started working on it yet!") 
    172141                return 
    173             for (activeticket, endtime) in data: 
    174                 if not activeticket == int(ticket): 
    175                     addMessage("You cannot stop working on ticket " + tckt_link + " as it appears you've not started working on it yet! " + str(activeticket) + " " + str(endtime)) 
    176                     return 
    177                 if not endtime == 0: 
    178                     addMessage("You cannot stop working on ticket " + tckt_link + " as it apears you've already finished working!") 
    179                     return 
     142            if not task['ticket'] == int(ticket): 
     143                addMessage("You cannot stop working on ticket " + tckt_link + " as it appears you're working on something else!") 
     144                return 
    180145             
    181146            # Here we should calculate times and automatically add hours to ticket etc. 
    182147            now = int(time()); 
    183148            sql = "UPDATE work_log SET endtime=%s, lastchange=%s WHERE user='%s' AND lastchange=%s AND endtime=0" % \ 
    184                   (now, now, authname, lastchange) 
    185             dbhelper.execute_non_query(self.env.get_db_cnx(), sql) 
     149                  (now, now, authname, task['lastchange']) 
     150            db = self.env.get_db_cnx(); 
     151            cursor = db.cursor() 
     152            cursor.execute(sql) 
    186153            addMessage("You are no longer working on ticket " + tckt_link + ".") 
    187154