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.

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