Changeset 3510

Show
Ignore:
Timestamp:
04/13/08 14:00:34 (8 months ago)
Author:
tcoulter
Message:

TrashTalk? now seems to be recording data properly! All that's next is to display it.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trashtalkplugin/0.11/trashtalk/trashtalk.py

    r3509 r3510  
    2828about each ticket.  
    2929 
    30 Some functions below were legally pilferred from the tracpasteplugin: 
     30Some methods below were legally pilferred from the tracpasteplugin: 
    3131http://www.trac-hacks.org/wiki/TracPastePlugind 
    3232""" 
     
    4242from trac.web.api import * 
    4343from trac.ticket import Ticket 
     44from trac.util.datefmt import utc, to_timestamp 
     45from datetime import datetime 
    4446 
    45 class TrashTalkBackend(Component): 
    46     implements() 
     47def sql_escape_percent(sql): 
     48    import re 
     49    return re.sub("'((?:[^']|(?:''))*)'", lambda m: m.group(0).replace('%', '%%'), sql) 
     50 
     51def get_incoming_links_for_ticket(env, ticket, db=None): 
     52    """Return all incoming links for the given ticket.""" 
     53    cursor = (db or env.get_db_cnx()).cursor() 
     54    cursor.execute('select * from incoming_links where id=%s order by first desc', (ticket,)) 
     55    result = [] 
     56    for row in cursor: 
     57        result.append({ 
     58            'id':                   row[0], 
     59            'ticket':               row[1], 
     60            'external_url':         row[2], 
     61            'click_count':          row[3], 
     62            'first':                datetime.fromtimestamp(row[4], utc), 
     63            'most_recent':          datetime.fromtimestamp(row[5], utc) 
     64        }) 
     65    return result 
    4766 
    4867class TrashTalkPlugin(Component): 
     
    5473            Column('ticket'),                  # The id of the ticket that has been linked to. 
    5574            Column('external_url'),            # The url linking to the ticket. 
    56             Column('count'),                   # The number of times the external url referred to the ticket
     75            Column('click_count'),             # The number of times the external url has been clicked on
    5776            Column('first', type='int'),       # The first time the external_url linked to the ticket. 
    5877            Column('most_recent', type='int')  # The most recent time the external_url link to the ticket. 
    5978        ] 
    6079    ] 
     80     
     81    SCHEMA_LOOKUP = { 
     82        'id':           0, 
     83        'ticket':       1, 
     84        'external_url': 2, 
     85        'click_count':  3, 
     86        'first':        4, 
     87        'most_recent':  5 
     88    } 
    6189 
    6290    TICKET_URI_PATTERN = re.compile("^/ticket/(\d+)(?:$|.*$)", re.VERBOSE) 
     
    6997        cursor = db.cursor() 
    7098        try: 
    71             cursor.execute('select count(*) from trashtalk') 
     99            cursor.execute('select count(*) from incoming_links') 
    72100            cursor.fetchone() 
    73101            return False 
     
    97125    def _talk_about(self, req): 
    98126         
    99         ticket_id = int(self.TICKET_URI_PATTERN.findall(req.path_info)[0]) 
    100         ticket = Ticket(self.env, ticket_id) 
     127        # Eventually, some kind of model should be created for incoming links. 
     128        # Right now though, for a first implementation, this will suffice. 
    101129         
    102         ticket.save_changes("tcoulter", req.get_header("referer")) 
     130        ticket = int(self.TICKET_URI_PATTERN.findall(req.path_info)[0]) 
     131        external_url = req.get_header("referer") 
     132        click_count = 1 
     133        most_recent = datetime.now(utc) 
     134        first = most_recent 
    103135         
    104         print "::: " + req.get_header("referer") 
    105         print "--> " + req.abs_href() + req.path_info 
     136        # See if the current link is already in the database for this ticket. 
     137        db = self.env.get_db_cnx() 
     138        cursor = db.cursor() 
     139         
     140        cursor.execute('select * from incoming_links where ticket = %s and external_url = %s limit 1', (ticket, external_url)) 
     141         
     142        row = cursor.fetchone() 
     143         
     144        # Have we recorded the external_url for this ticket before? 
     145        if row: 
     146            # Yes. Let's update the click count and the most_recent timestamp. 
     147            id = row[self.SCHEMA_LOOKUP['id']] 
     148            click_count = int(row[self.SCHEMA_LOOKUP['click_count']]) + 1 
     149             
     150            cursor.execute('update incoming_links set click_count = %s, most_recent = %s where id = %s', (click_count, most_recent, id))  
     151        else: 
     152            # No. Let's create a new row. 
     153            cursor.execute('insert into incoming_links (ticket, external_url, click_count, first, most_recent) values (%s, %s, %s, %s, %s)', 
     154                           (ticket, external_url, click_count, first, most_recent)) 
     155 
     156        # Commit changes... 
     157        db.commit() 
    106158         
    107159