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.

126 lines
3.5 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, Tuple
  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(registrationCode, 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. registration_code: Optional[Tuple] = _query_db(
  36. "SELECT * FROM registration_codes WHERE code = ?", [code], one=True)
  37. if registration_code is not None:
  38. return RegistrationCode.from_db(registration_code)
  39. return None
  40. def add_registration_code(
  41. expiration_time: datetime = datetime.now(),
  42. max_usages: int = 1) -> RegistrationCode:
  43. code = RegistrationCode(
  44. uuid.uuid4().__str__(), expiration_time=expiration_time, max_usages=max_usages)
  45. db = get_db()
  46. db.execute(
  47. REGISTRATION_CODE_INSERT_SQL,
  48. [code.code,
  49. code.creation_time,
  50. code.expiration_time,
  51. code.usages,
  52. code.max_usages]
  53. )
  54. db.commit()
  55. return get_registration_code(code.code)
  56. def update_registration_code(code: RegistrationCode) -> RegistrationCode:
  57. db = get_db()
  58. db.execute(REGISTRATION_CODE_UPDATE_SQL,
  59. [code.expiration_time, code.usages, code.max_usages, code.code]
  60. )
  61. db.commit()
  62. return get_registration_code(code.code)
  63. def expire_registration_code(code: str):
  64. db = get_db()
  65. db.execute(
  66. "UPDATE registration_codes SET expirationTime = ? WHERE code = ?",
  67. [datetime.now(), code]
  68. )
  69. db.commit()
  70. def delete_registration_code(code:str):
  71. db = get_db()
  72. db.execute(
  73. "DELETE FROM registration_codes WHERE code = ?",
  74. [code]
  75. )
  76. db.commit()
  77. def get_registered_users() -> [RegisteredUser]:
  78. registered_users = []
  79. for registered_user in _query_db("SELECT * FROM registered_users"):
  80. registered_users.append(RegisteredUser.from_db(registered_user))
  81. return registered_users
  82. def get_registered_user(username: str) -> Optional[RegisteredUser]:
  83. registered_user: Optional[Tuple] = _query_db(
  84. "SELECT * FROM registered_users WHERE username = ?", [username], one=True)
  85. if registered_user is not None:
  86. return RegisteredUser.from_db(registered_user)
  87. return None
  88. def add_registered_user(code: str, username: str) -> RegisteredUser:
  89. registration_code = get_registration_code(code)
  90. registration_code.usages += 1
  91. update_registration_code(registration_code)
  92. db = get_db()
  93. db.execute(REGISTERED_USER_INSERT_SQL, [code, username, datetime.now()])
  94. db.commit()
  95. return get_registered_user(username)