Browse Source

Adding events to the API

master
Rick 6 years ago
parent
commit
beb43dcd12
2 changed files with 73 additions and 4 deletions
  1. +33
    -4
      admidio.py
  2. +40
    -0
      event.py

+ 33
- 4
admidio.py View File

@ -1,7 +1,10 @@
import MySQLdb import MySQLdb
import member import member
import group import group
import event
from exceptions import MemberNotFoundException from exceptions import MemberNotFoundException
from datetime import datetime
from dateutil.relativedelta import relativedelta
class Admidio: class Admidio:
@ -14,15 +17,22 @@ class Admidio:
self.members = dict() self.members = dict()
self.groups = dict() self.groups = dict()
self.events = dict() self.events = dict()
self.event_confirmation_id = None
sql = "SELECT usf_id,usf_name_intern FROM `adm_user_fields`" sql = "SELECT usf_id,usf_name_intern FROM `adm_user_fields`"
self.cursor.execute(sql) self.cursor.execute(sql)
self.userfields = dict(self.cursor.fetchall()) self.userfields = dict(self.cursor.fetchall())
sql = """SELECT usr_id, usr_login_name, usr_password
FROM adm_users WHERE usr_valid = 1"""
# Get the id for event confirmation role.
# So it can exclude them from Groups
sql = """SELECT cat_id
FROM adm_categories
WHERE cat_name_intern = 'CONFIRMATION_OF_PARTICIPATION'"""
self.cursor.execute(sql) self.cursor.execute(sql)
self.event_confirmation_id = self.cursor.fetchone()[0]
self.initMembers() self.initMembers()
self.initGroups() self.initGroups()
self.initEvents()
def __del__(self): def __del__(self):
self.db.close() self.db.close()
@ -42,14 +52,33 @@ class Admidio:
def initGroups(self): def initGroups(self):
"""Initialises all groups in the Admidio installation""" """Initialises all groups in the Admidio installation"""
sql = """SELECT rol_id, rol_name, rol_description
sql = f"""SELECT rol_id, rol_name, rol_description
FROM adm_roles FROM adm_roles
WHERE rol_visible=1 AND rol_valid=1"""
WHERE rol_valid = 1
AND rol_cat_id != {self.event_confirmation_id}"""
self.cursor.execute(sql) self.cursor.execute(sql)
for row in self.cursor.fetchall(): for row in self.cursor.fetchall():
self.groups[row[0]] = group.Group(self.db, row[0], row[1], self) self.groups[row[0]] = group.Group(self.db, row[0], row[1], self)
def initEvents(self):
"""Initialises all comming events and passed events <3 months"""
datestring = datetime.now() + relativedelta(months=-6)
datestring = datestring.strftime("%Y-%m-%d %H:%M:%S")
sql = f"""SELECT dat_id, dat_cat_id, dat_begin, dat_end,
dat_headline, dat_description,
dat_location, dat_country, dat_rol_id
FROM adm_dates
WHERE dat_begin >'{datestring}'"""
self.cursor.execute(sql)
for row in self.cursor.fetchall():
self.events[row[0]] = event.Event(
self.db, row[0], row[8], row[4], row[2], row[3], row[5],
row[6], row[7], self)
def getMemberFromID(self, user_id): def getMemberFromID(self, user_id):
""" """
Returns Member object if user_id exists Returns Member object if user_id exists


+ 40
- 0
event.py View File

@ -0,0 +1,40 @@
from exceptions import MemberNotFoundException
class Event():
def __init__(self, db, event_id, rol_id, name, start_time, end_time,
description, location, country, admidio):
self.__c = db.cursor()
self.id = event_id
self.rol_id = rol_id
self.name = name
self.start_time = start_time
self.end_time = end_time
self.description = description
self.location = location
self.country = country
self.admidio = admidio
self.participants = list()
self.leaders = list()
self.number_of_guests = 0
sql = ("""SELECT mem_usr_id, mem_leader, mem_count_guests
FROM adm_members"""
f""" WHERE mem_rol_id = {self.rol_id}""")
self.__c.execute(sql)
for row in self.__c.fetchall():
try:
self.number_of_guests += row[2]
if row[1] == 1:
self.leaders.append(self.admidio.getMemberFromID(row[0]))
else:
self.participants.append(
self.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)

Loading…
Cancel
Save