@ -0,0 +1,52 @@ | |||||
import MySQLdb | |||||
import member | |||||
import group | |||||
from exceptions import MemberNotFoundException | |||||
class Admidio: | |||||
"""Creates the Admidio Class""" | |||||
def __init__(self, mysql_host, mysql_user, mysql_pass, mysql_db): | |||||
self.db = MySQLdb.connect( | |||||
host=mysql_host, user=mysql_user, passwd=mysql_pass, db=mysql_db) | |||||
self.members = dict() | |||||
self.groups = dict() | |||||
self.userfields = None | |||||
self.initAll() | |||||
def __del__(self): | |||||
self.db.close() | |||||
def initAll(self): | |||||
"""initializes all Members and Groups""" | |||||
c = self.db.cursor() | |||||
# Initializing member. Get ID and Name for the user fields. | |||||
sql = "SELECT usf_id,usf_name_intern FROM `adm_user_fields`" | |||||
c.execute(sql) | |||||
self.userfields = dict(c.fetchall()) | |||||
sql = """SELECT usr_id, usr_login_name, usr_password | |||||
FROM adm_users WHERE usr_valid = 1""" | |||||
c.execute(sql) | |||||
for row in c.fetchall(): | |||||
self.members[row[0]] = member.Member( | |||||
self.db, row[0], row[1], | |||||
row[2], self.userfields, self) | |||||
print(self.members[row[0]]) | |||||
sql = """SELECT rol_id, rol_name, rol_description FROM adm_roles | |||||
WHERE rol_visible=1 AND rol_valid=1""" | |||||
c.execute(sql) | |||||
for row in c.fetchall(): | |||||
self.groups[row[0]] = group.Group(self.db, row[0], row[1], self) | |||||
def getMemberFromID(self, user_id): | |||||
"""Returns Member object if user_id exists,""" \ | |||||
""" otherwise throws MemberNotFoundException""" | |||||
if user_id in self.members: | |||||
return self.members[user_id] | |||||
else: | |||||
raise MemberNotFoundException( | |||||
f"Member with id {user_id} not found") |
@ -0,0 +1,3 @@ | |||||
class MemberNotFoundException(Exception): | |||||
"""Exception raised when Member ID can't be found""" | |||||
pass |
@ -0,0 +1,29 @@ | |||||
from exceptions import MemberNotFoundException | |||||
class Group: | |||||
def __init__(self, db, group_id, name, admidio): | |||||
self.__c = db.cursor() | |||||
self.id = group_id | |||||
self.name = name | |||||
self.admidio = admidio | |||||
self.members = list() | |||||
self.leaders = list() | |||||
sql = ("""SELECT mem_usr_id, mem_leader FROM adm_members""" | |||||
f""" WHERE mem_rol_id = {self.id}""") | |||||
self.__c.execute(sql) | |||||
for row in self.__c.fetchall(): | |||||
print(row) | |||||
try: | |||||
if row[1] == 1: | |||||
self.leaders.append(admidio.getMemberFromID(row[0])) | |||||
else: | |||||
self.members.append(admidio.getMemberFromID(row[0])) | |||||
except MemberNotFoundException: | |||||
print( | |||||
f"Member with id {row[0]} not found, group id: {self.id}") | |||||
def getAllMembers(self): | |||||
"""Return leaders and members in group, combined""" | |||||
return self.leaders.append(self.members) |
@ -0,0 +1,6 @@ | |||||
from admidio import Admidio | |||||
import config | |||||
adm = Admidio( | |||||
config.DATABASE['host'], config.DATABASE['user'], config.DATABASE['pass'], | |||||
config.DATABASE['db']) |
@ -0,0 +1,47 @@ | |||||
import pprint | |||||
class Member(): | |||||
"""Member class for admidio, here all member data will be stored.""" | |||||
def __init__(self, db, user_id, username, password, userfields, admidio): | |||||
self.__c = db.cursor() | |||||
self.user_id = user_id | |||||
self.username = username | |||||
self.password = password | |||||
self.userdata = dict() | |||||
self.admidio = admidio | |||||
sql = f"""SELECT usd_usf_id, usd_value FROM adm_user_data | |||||
WHERE usd_usr_id = {self.user_id!s}""" | |||||
self.__c.execute(sql) | |||||
for row in self.__c.fetchall(): | |||||
# makes dict with {usf_name_intern:usd_value} | |||||
self.userdata[userfields[row[0]]] = row[1] | |||||
def __hash__(self): | |||||
"""Not sure if needed""" | |||||
return hash((self.user_id, self.username)) | |||||
def __str__(self): | |||||
return (f"""Userid: {self.user_id}, Username: {self.username}, """ | |||||
f"""Userdata: {pprint.pformat(self.userdata)}""") | |||||
"""@property | |||||
def email(self): | |||||
return self.userdata['EMAIL'] | |||||
@property | |||||
def firstName(self): | |||||
return self.userdata['FIRST_NAME'] | |||||
@property | |||||
def lastName(self): | |||||
return self.userdata['LAST_NAME'] | |||||
@property | |||||
def username(self): | |||||
self.username | |||||
@property | |||||
def password(self): | |||||
return self.password""" |