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.

52 lines
1.7 KiB

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.members = dict()
  11. self.groups = dict()
  12. self.userfields = None
  13. self.initAll()
  14. def __del__(self):
  15. self.db.close()
  16. def initAll(self):
  17. """initializes all Members and Groups"""
  18. c = self.db.cursor()
  19. # Initializing member. Get ID and Name for the user fields.
  20. sql = "SELECT usf_id,usf_name_intern FROM `adm_user_fields`"
  21. c.execute(sql)
  22. self.userfields = dict(c.fetchall())
  23. sql = """SELECT usr_id, usr_login_name, usr_password
  24. FROM adm_users WHERE usr_valid = 1"""
  25. c.execute(sql)
  26. for row in c.fetchall():
  27. self.members[row[0]] = member.Member(
  28. self.db, row[0], row[1],
  29. row[2], self.userfields, self)
  30. print(self.members[row[0]])
  31. sql = """SELECT rol_id, rol_name, rol_description FROM adm_roles
  32. WHERE rol_visible=1 AND rol_valid=1"""
  33. c.execute(sql)
  34. for row in c.fetchall():
  35. self.groups[row[0]] = group.Group(self.db, row[0], row[1], self)
  36. def getMemberFromID(self, user_id):
  37. """Returns Member object if user_id exists,""" \
  38. """ otherwise throws MemberNotFoundException"""
  39. if user_id in self.members:
  40. return self.members[user_id]
  41. else:
  42. raise MemberNotFoundException(
  43. f"Member with id {user_id} not found")