The objective of this interface is the ability to get data from an Admidio installation and use them in python. Currently Users, groups and events are integrated
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.

127 lines
3.8 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. import MySQLdb
  2. from .member import Member as member
  3. from .group import Group as group
  4. from .event import Event as event
  5. from .exceptions import MemberNotFoundException
  6. from datetime import datetime
  7. from dateutil.relativedelta import relativedelta
  8. class Admidio:
  9. """Creates the Admidio Class"""
  10. def __init__(self, mysql_host, mysql_user, mysql_pass, mysql_db,
  11. db_prefix):
  12. self.mysql_host = mysql_host
  13. self.mysql_user = mysql_user
  14. self.mysql_pass = mysql_pass
  15. self.mysql_db = mysql_db
  16. self.prefix = db_prefix
  17. self.members = dict()
  18. self.groups = dict()
  19. self.events = dict()
  20. self.event_confirmation_id = None
  21. self.connect_db()
  22. sql = f"SELECT usf_id,usf_name_intern FROM {self.prefix}_user_fields"
  23. self.cursor.execute(sql)
  24. self.userfields = dict(self.cursor.fetchall())
  25. # Get the id for event confirmation role.
  26. # So it can exclude them from Groups
  27. sql = f"""SELECT cat_id
  28. FROM {self.prefix}_categories
  29. WHERE cat_name_intern = 'EVENTS'"""
  30. self.cursor.execute(sql)
  31. self.event_confirmation_id = self.cursor.fetchone()[0]
  32. self.db.close()
  33. self.refresh()
  34. def __del__(self):
  35. pass
  36. def connect_db(self):
  37. self.db = MySQLdb.connect(
  38. host=self.mysql_host,
  39. user=self.mysql_user,
  40. passwd=self.mysql_pass,
  41. db=self.mysql_db)
  42. self.cursor = self.db.cursor()
  43. def refresh(self):
  44. """Refresh all users, groups and events that are stored"""
  45. self.connect_db()
  46. self.ready = False
  47. self.members.clear()
  48. self.groups.clear()
  49. self.events.clear()
  50. self.initMembers()
  51. self.initGroups()
  52. self.initEvents()
  53. self.ready = True
  54. self.db.close()
  55. def initMembers(self):
  56. """Initialises all members in the Admidio installation"""
  57. sql = f"""SELECT usr_id, usr_login_name, usr_password
  58. FROM {self.prefix}_users WHERE usr_valid = 1"""
  59. self.cursor.execute(sql)
  60. for row in self.cursor.fetchall():
  61. self.members[row[0]] = member(
  62. self.db, row[0], row[1],
  63. row[2], self.userfields, self)
  64. def initGroups(self):
  65. """Initialises all groups in the Admidio installation"""
  66. sql = f"""SELECT rol_id, rol_name, rol_description
  67. FROM {self.prefix}_roles
  68. WHERE rol_valid = 1
  69. AND rol_cat_id != {self.event_confirmation_id}"""
  70. self.cursor.execute(sql)
  71. for row in self.cursor.fetchall():
  72. self.groups[row[0]] = group(row[0], row[1], self)
  73. def initEvents(self):
  74. """Initialises all comming events and passed events <3 months"""
  75. datestring = datetime.now() + relativedelta(months=-6)
  76. datestring = datestring.strftime("%Y-%m-%d %H:%M:%S")
  77. sql = f"""SELECT dat_id, dat_cat_id, dat_begin, dat_end,
  78. dat_headline, dat_description,
  79. dat_location, dat_country, dat_rol_id
  80. FROM {self.prefix}_dates
  81. WHERE dat_begin >'{datestring}'
  82. ORDER BY dat_begin"""
  83. self.cursor.execute(sql)
  84. for row in self.cursor.fetchall():
  85. self.events[row[0]] = event(
  86. row[0], row[8], row[4], row[2], row[3], row[5],
  87. row[6], row[7], row[1], self)
  88. def getMemberFromID(self, user_id):
  89. """
  90. Returns Member object if user_id exists
  91. otherwise throws MemberNotFoundException
  92. """
  93. if user_id in self.members:
  94. return self.members[user_id]
  95. else:
  96. raise MemberNotFoundException(
  97. f"Member with id {user_id} not found")