Changeset 3510
- Timestamp:
- 04/13/08 14:00:34 (8 months ago)
- Files:
-
- trashtalkplugin/0.11/trashtalk/model.py (deleted)
- trashtalkplugin/0.11/trashtalk/trashtalk.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trashtalkplugin/0.11/trashtalk/trashtalk.py
r3509 r3510 28 28 about each ticket. 29 29 30 Some functions below were legally pilferred from the tracpasteplugin:30 Some methods below were legally pilferred from the tracpasteplugin: 31 31 http://www.trac-hacks.org/wiki/TracPastePlugind 32 32 """ … … 42 42 from trac.web.api import * 43 43 from trac.ticket import Ticket 44 from trac.util.datefmt import utc, to_timestamp 45 from datetime import datetime 44 46 45 class TrashTalkBackend(Component): 46 implements() 47 def sql_escape_percent(sql): 48 import re 49 return re.sub("'((?:[^']|(?:''))*)'", lambda m: m.group(0).replace('%', '%%'), sql) 50 51 def 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 47 66 48 67 class TrashTalkPlugin(Component): … … 54 73 Column('ticket'), # The id of the ticket that has been linked to. 55 74 Column('external_url'), # The url linking to the ticket. 56 Column('c ount'), # 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. 57 76 Column('first', type='int'), # The first time the external_url linked to the ticket. 58 77 Column('most_recent', type='int') # The most recent time the external_url link to the ticket. 59 78 ] 60 79 ] 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 } 61 89 62 90 TICKET_URI_PATTERN = re.compile("^/ticket/(\d+)(?:$|.*$)", re.VERBOSE) … … 69 97 cursor = db.cursor() 70 98 try: 71 cursor.execute('select count(*) from trashtalk')99 cursor.execute('select count(*) from incoming_links') 72 100 cursor.fetchone() 73 101 return False … … 97 125 def _talk_about(self, req): 98 126 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. 101 129 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 103 135 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() 106 158 107 159
