~pound-python/infobat/infobob

« back to all changes in this revision

Viewing changes to infobat/database.py

  • Committer: Aaron Gallagher
  • Date: 2012-09-03 02:55:12 UTC
  • Revision ID: habnabit@gmail.com-20120903025512-uhllhvujpyg4b4vo
Adding a web interface for ban details.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
import datetime
6
6
import sqlite3
7
7
import time
 
8
import uuid
8
9
 
 
10
local = dateutil.tz.tzlocal()
9
11
sqlite3.register_adapter(datetime.datetime,
10
 
    lambda x: time.mktime(x.timetuple()))
 
12
    lambda x: time.mktime(x.astimezone(local).timetuple()))
11
13
sqlite3.register_converter('datetime',
12
14
    lambda x: datetime.datetime.fromtimestamp(float(x)).replace(
13
 
        tzinfo=dateutil.tz.tzlocal()))
 
15
        tzinfo=local))
14
16
 
15
17
def interaction(func):
16
18
    def wrap(self, *a, **kw):
20
22
class TooSoonError(Exception):
21
23
    pass
22
24
 
 
25
class NoSuchBan(Exception):
 
26
    pass
 
27
 
23
28
_ADD_USER_TO_CHANNEL = """
24
29
    REPLACE INTO channel_users
25
30
               (nick, channel)
172
177
        return txn.lastrowid
173
178
 
174
179
    @interaction
 
180
    def add_ban_auth(self, txn, rowid):
 
181
        auth = uuid.uuid4().hex
 
182
        txn.execute("""
 
183
            INSERT INTO ban_authorizations
 
184
                        (ban, code)
 
185
            VALUES (?, ?)
 
186
        """, (rowid, auth))
 
187
        return auth
 
188
 
 
189
    @interaction
175
190
    def remove_ban(self, txn, channel, host, mask, mode):
176
191
        now = time.time()
177
192
        txn.execute("""
240
255
        return txn.fetchall()
241
256
 
242
257
    @interaction
 
258
    def get_ban_with_auth(self, txn, rowid, auth):
 
259
        txn.execute("""
 
260
            SELECT channel, mask, mode, set_at as "set_at [datetime]", set_by, expire_at as "expire_at [datetime]", reason, unset_at as "unset_at [datetime]", unset_by
 
261
            FROM   bans
 
262
            JOIN   ban_authorizations authz
 
263
                   ON bans.rowid = authz.ban
 
264
            WHERE  authz.ban = ?
 
265
                   AND authz.code = ?
 
266
            LIMIT  1
 
267
        """, (rowid, auth))
 
268
        res = txn.fetchall()
 
269
        if not res:
 
270
            raise NoSuchBan()
 
271
        return res[0]
 
272
 
 
273
    @interaction
243
274
    def check_mask(self, txn, channel, mask):
244
275
        txn.execute("""
245
276
            SELECT nick
274
305
                   AND mode = ?
275
306
                   AND unset_at IS NULL
276
307
        """, (reason, channel, mask, mode))
 
308
 
 
309
    @interaction
 
310
    def update_ban_by_rowid(self, txn, rowid, expire_at, reason):
 
311
        print `txn, rowid, expire_at, reason`
 
312
        txn.execute("""
 
313
            UPDATE bans
 
314
            SET    expire_at = ?,
 
315
                   reason = ?
 
316
            WHERE  rowid = ?
 
317
        """, (expire_at, reason, rowid))