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.

62 lines
2.0 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
  1. import MySQLdb
  2. import member
  3. import group
  4. from exceptions import MemberNotFoundException
  5. class Admidio:
  6. """Creates the Admidio Class"""
  7. def __init__(self, mysql_host, mysql_user, mysql_pass, mysql_db):
  8. self.db = MySQLdb.connect(
  9. host=mysql_host, user=mysql_user, passwd=mysql_pass, db=mysql_db)
  10. self.cursor = self.db.cursor()
  11. self.members = dict()
  12. self.groups = dict()
  13. self.events = dict()
  14. sql = "SELECT usf_id,usf_name_intern FROM `adm_user_fields`"
  15. self.cursor.execute(sql)
  16. self.userfields = dict(self.cursor.fetchall())
  17. sql = """SELECT usr_id, usr_login_name, usr_password
  18. FROM adm_users WHERE usr_valid = 1"""
  19. self.cursor.execute(sql)
  20. self.initMembers()
  21. self.initGroups()
  22. def __del__(self):
  23. self.db.close()
  24. def initMembers(self):
  25. """Initialises all members in the Admidio installation"""
  26. sql = """SELECT usr_id, usr_login_name, usr_password
  27. FROM adm_users WHERE usr_valid = 1"""
  28. self.cursor.execute(sql)
  29. for row in self.cursor.fetchall():
  30. self.members[row[0]] = member.Member(
  31. self.db, row[0], row[1],
  32. row[2], self.userfields, self)
  33. def initGroups(self):
  34. """Initialises all groups in the Admidio installation"""
  35. sql = """SELECT rol_id, rol_name, rol_description
  36. FROM adm_roles
  37. WHERE rol_visible=1 AND rol_valid=1"""
  38. self.cursor.execute(sql)
  39. for row in self.cursor.fetchall():
  40. self.groups[row[0]] = group.Group(self.db, row[0], row[1], self)
  41. def getMemberFromID(self, user_id):
  42. """
  43. Returns Member object if user_id exists
  44. otherwise throws MemberNotFoundException
  45. """
  46. if user_id in self.members:
  47. return self.members[user_id]
  48. else:
  49. raise MemberNotFoundException(
  50. f"Member with id {user_id} not found")