A simple web application that allows invitation based registration to a matrix instance
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
  1. import os
  2. import sqlite3
  3. import uuid
  4. from datetime import datetime
  5. from typing import Optional
  6. from flask import g
  7. from models import RegistrationCode, RegisteredUser
  8. DATABASE = os.getenv("DATA_DIRECTORY", ".") + "/data.db"
  9. REGISTRATION_CODE_INSERT_SQL = """INSERT INTO
  10. registration_codes(code, creationTime, expirationTime, usages, maxUsages)
  11. VALUES(?, ?, ?, ?, ?)"""
  12. REGISTRATION_CODE_UPDATE_SQL = """UPDATE registration_codes
  13. SET expirationTime = ?, usages = ?, maxUsages = ?
  14. WHERE code = ?"""
  15. REGISTERED_USER_INSERT_SQL = """INSERT INTO
  16. registered_users(username, registeredTime)
  17. VALUES(?, ?)
  18. """
  19. def get_db():
  20. db = getattr(g, '_database', None)
  21. if db is None:
  22. db = g._database = sqlite3.connect(DATABASE)
  23. return db
  24. def _query_db(query, args=(), one=False):
  25. cur = get_db().execute(query, args)
  26. rv = cur.fetchall()
  27. cur.close()
  28. return (rv[0] if rv else None) if one else rv
  29. def get_registration_codes() -> [RegistrationCode]:
  30. registration_codes = []
  31. for registration_code in _query_db("SELECT * FROM registration_codes"):
  32. registration_codes.append(RegistrationCode.from_db(registration_code))
  33. return registration_codes
  34. def get_registration_code(code: str) -> Optional[RegistrationCode]:
  35. return RegistrationCode.from_db(
  36. _query_db("SELECT * FROM registration_codes WHERE code = ?", [code], one=True)
  37. )
  38. def add_registration_code(
  39. expiration_time: datetime = datetime.now(),
  40. max_usages: int = 1) -> RegistrationCode:
  41. code = RegistrationCode(
  42. uuid.uuid4().__str__(), expiration_time=expiration_time, max_usages=max_usages)
  43. db = get_db()
  44. db.execute(
  45. REGISTRATION_CODE_INSERT_SQL,
  46. [code.code,
  47. code.creation_time,
  48. code.expiration_time,
  49. code.usages,
  50. code.max_usages]
  51. )
  52. db.commit()
  53. return get_registration_code(code.code)
  54. def update_registration_code(code: RegistrationCode) -> RegistrationCode:
  55. db = get_db()
  56. db.execute(REGISTRATION_CODE_UPDATE_SQL,
  57. [code.expiration_time, code.usages, code.max_usages, code.code]
  58. )
  59. db.commit()
  60. return get_registration_code(code.code)
  61. def expire_registration_code(code: str):
  62. db = get_db()
  63. db.execute(
  64. "UPDATE registration_codes SET expirationTime = ? WHERE code = ?",
  65. [datetime.now(), code]
  66. )
  67. db.commit()
  68. def delete_registration_code(code:str):
  69. db = get_db()
  70. db.execute(
  71. "DELETE FROM registration_codes WHERE code = ?",
  72. [code]
  73. )
  74. db.commit()
  75. def get_registered_users() -> [RegisteredUser]:
  76. registered_users = []
  77. for registered_user in _query_db("SELECT * FROM registered_users"):
  78. registered_users.append(RegisteredUser.from_db(registered_user))
  79. return registered_users
  80. def get_registered_user(username: str) -> Optional[RegisteredUser]:
  81. return RegisteredUser.from_db(
  82. _query_db("SELECT * FROM registered_users WHERE username = ?", username, one=True)
  83. )
  84. def add_registered_user(code: str, username: str) -> RegisteredUser:
  85. registration_code = get_registration_code(code)
  86. registration_code.usages += 1
  87. update_registration_code(registration_code)
  88. db = get_db()
  89. db.execute(REGISTERED_USER_INSERT_SQL, [username, datetime.now()])
  90. db.commit()
  91. return get_registered_user(username)