Fork of elimage with specific modifications for my instance. https://i.catgirlsin.space
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

89 行
1.9KB

  1. #!/usr/bin/env python3
  2. # vim: set fileencoding=utf-8:
  3. import sqlite3
  4. import logging
  5. import time
  6. from config import DB
  7. #
  8. __all__ = ['model']
  9. #
  10. def log(fn):
  11. def wrapped(*args, **kwds):
  12. try:
  13. return fn(*args, **kwds)
  14. except Exception as e:
  15. logging.warning(e)
  16. return wrapped
  17. def dict_factory(cursor, row):
  18. d = {}
  19. for idx, col in enumerate(cursor.description):
  20. d[col[0]] = row[idx]
  21. return d
  22. #
  23. class Model:
  24. def __init__(self, db):
  25. self.conn = sqlite3.connect(db)
  26. self.conn.row_factory = dict_factory
  27. self.cur = self.conn.cursor()
  28. @log
  29. def get_user_by_id(self, id):
  30. row = self.cur.execute('select * from user where id=?', (id,))
  31. return row.fetchone()
  32. @log
  33. def get_user_by_ip(self, ip):
  34. row = self.cur.execute('select * from user where ip=?', (ip,))
  35. return row.fetchone()
  36. @log
  37. def get_image_by_name(self, name):
  38. row = self.cur.execute('select * from image where name=?', (name,))
  39. return row.fetchone()
  40. @log
  41. def get_image_by_uid(self, uid):
  42. row = self.cur.execute('select * from image where uid=?', (uid,))
  43. return row.fetchall()
  44. @log
  45. def add_user(self, ip):
  46. self.cur.execute('insert into user(ip) values (?)', (ip,))
  47. self.conn.commit()
  48. return self.cur.lastrowid
  49. @log
  50. def add_image(self, uid, fname, filename, size):
  51. self.cur.execute('''insert into image (uid, name, time, filename, size)
  52. values (?, ?, ?, ?, ?)''',
  53. (uid, fname, int(time.time()), filename, size))
  54. self.conn.commit()
  55. @log
  56. def block_user(self, id, block=1):
  57. self.cur.execute('update user set blocked=? where id=?', (block, id))
  58. self.conn.commit()
  59. unblock_user = lambda self, id: self.block_user(id, block=0)
  60. def isBlocked(self, type, data):
  61. method = getattr(self, 'get_user_by_'+type, None)
  62. if callable(method):
  63. row = method(data)
  64. return row['blocked'] == 1
  65. else:
  66. return None
  67. #
  68. model = Model(DB)