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.

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