Ticket #4087: spamfilter_log-base64content-r5329.diff
| File spamfilter_log-base64content-r5329.diff, 5.7 kB (added by cboos, 16 months ago) |
|---|
-
tracspamfilter/model.py
11 11 # individuals. For the exact contribution history, see the revision 12 12 # history and logs, available at http://projects.edgewall.com/trac/. 13 13 14 import binascii 15 14 16 from datetime import datetime, timedelta 15 17 from time import mktime 16 18 17 19 from trac.db import Column, Index, Table 20 from trac.util.text import to_unicode 18 21 19 22 __all__ = ['LogEntry'] 20 23 … … 66 69 exists = property(fget=lambda self: self.id is not None, 67 70 doc='Whether this log entry exists in the database') 68 71 72 def _encode_content(cls, content): 73 """Take a `basestring` content and return a plain text encoding.""" 74 return to_unicode(content).encode('utf-8').encode('base64') 75 76 _encode_content = classmethod(_encode_content) 77 78 def _decode_content(cls, content): 79 """Revert the encoding done by `_encode_content` and return an unicode 80 string""" 81 try: 82 return to_unicode(content.decode('base64')) 83 except (UnicodeEncodeError, binascii.Error): 84 # cope with legacy content (stored before base64 encoding) 85 return to_unicode(content) 86 87 _decode_content = classmethod(_decode_content) 88 69 89 def get_next(self, db=None): 70 90 """Return the next log entry in reverse chronological order (i.e. the 71 91 next older entry.)""" … … 79 99 row = cursor.fetchone() 80 100 if not row: 81 101 return None 102 return self.__class__._from_db(self.env, row) 82 103 83 obj = self.__class__(self.env, *row[1:])84 obj.id = row[0]85 return obj86 87 104 def get_previous(self, db=None): 88 """Return the previous log entry in reverse chronological order (i.e. the89 next younger entry.)"""105 """Return the previous log entry in reverse chronological order 106 (i.e. the next younger entry.)""" 90 107 if not db: 91 108 db = self.env.get_db_cnx() 92 109 … … 97 114 row = cursor.fetchone() 98 115 if not row: 99 116 return None 117 return self.__class__._from_db(self.env, row) 100 118 101 obj = self.__class__(self.env, *row[1:])102 obj.id = row[0]103 return obj104 105 119 def insert(self, db=None): 106 120 """Insert a new log entry into the database.""" 107 121 if not db: … … 112 126 113 127 assert not self.exists, 'Cannot insert existing log entry' 114 128 129 content = self._encode_content(self.content) 130 115 131 cursor = db.cursor() 116 132 cursor.execute("INSERT INTO spamfilter_log (time,path,author," 117 133 "authenticated,ipnr,headers,content,rejected," 118 134 "karma,reasons) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s," 119 135 "%s)", (int(self.time), self.path, self.author, 120 136 int(bool(self.authenticated)), self.ipnr, self.headers, 121 self.content, int(bool(self.rejected)), int(self.karma),137 content, int(bool(self.rejected)), int(self.karma), 122 138 '\n'.join(self.reasons))) 123 139 self.id = db.get_last_id(cursor, 'spamfilter_log') 124 140 if handle_ta: … … 138 154 if hasattr(self, name): 139 155 setattr(self, name, value) 140 156 157 content = self._encode_content(self.content) 158 141 159 cursor = db.cursor() 142 160 cursor.execute("UPDATE spamfilter_log SET time=%s,path=%s,author=%s," 143 161 "authenticated=%s,ipnr=%s,headers=%s,content=%s," 144 162 "rejected=%s,karma=%s,reasons=%s WHERE id=%s", ( 145 163 int(self.time), self.path, self.author, 146 164 int(bool(self.authenticated)), self.ipnr, self.headers, 147 self.content, int(bool(self.rejected)), int(self.karma),165 content, int(bool(self.rejected)), int(self.karma), 148 166 '\n'.join(self.reasons), self.id)) 149 167 if handle_ta: 150 168 db.commit() … … 170 188 db = env.get_db_cnx() 171 189 172 190 cursor = db.cursor() 173 cursor.execute("SELECT time,path,author,authenticated,ipnr,headers,"191 cursor.execute("SELECT id,time,path,author,authenticated,ipnr,headers," 174 192 "content,rejected,karma,reasons FROM spamfilter_log " 175 193 "WHERE id=%s", (int(id),)) 176 194 row = cursor.fetchone() 177 195 if not row: 178 196 return None 197 return cls._from_db(env, row) 179 198 180 obj = cls(env, *row)181 obj.id = id182 return obj183 184 199 fetch = classmethod(fetch) 185 200 186 201 def count(cls, env, db=None): … … 247 262 "content,rejected,karma,reasons FROM spamfilter_log " 248 263 "%s ORDER BY time DESC %s" % (where, extra), params) 249 264 for row in cursor: 250 obj = cls(env, *row[1:]) 251 obj.id = row[0] 252 yield obj 265 yield cls._from_db(env, row) 253 266 254 267 select = classmethod(select) 255 268 269 def _from_db(cls, env, row): 270 """Create a new LogEntry from a row from the `spamfilter_log` table.""" 271 fields = list(row[1:]) 272 fields[6] = cls._decode_content(fields[6]) 273 obj = cls(env, *fields) 274 obj.id = row[0] 275 return obj 256 276 277 _from_db = classmethod(_from_db) 278 257 279 class Bayes(object): 258 280 259 281 table = Table('spamfilter_bayes', key='word')[ -
setup.py
15 15 from setuptools import setup, find_packages 16 16 17 17 PACKAGE = 'TracSpamFilter' 18 VERSION = '0.2 '18 VERSION = '0.2.1' 19 19 20 20 setup( 21 21 name = PACKAGE,
