diff --git a/plugins/admidio_events/admidio_events.py b/plugins/admidio_events/admidio_events.py
index d7a70ab..dba3d09 100644
--- a/plugins/admidio_events/admidio_events.py
+++ b/plugins/admidio_events/admidio_events.py
@@ -11,11 +11,18 @@ import hjson
import time
import sqlite3
import threading
+import importlib
+import sys
import datetime as dt
import MySQLdb as mysql
from matrix_bot_api.mregex_handler import MRegexHandler
+sys.path.insert(0, os.path.dirname(__file__))
+
+import admidio_python_api.admidio as admidio
+import admidio_python_api.group as group
+
MESSAGES_DIR = os.path.join(os.path.dirname(__file__), 'messages')
DATA_DIR = os.path.join(os.path.dirname(__file__),'../../data/events')
CONFIG_LOCATION = os.path.join(os.path.dirname(__file__), 'config.hjson')
@@ -37,6 +44,13 @@ class Plugin:
with open(MESSAGES_LOCATION) as hjson_data:
self.messages = hjson.load(hjson_data)
+ self.adm = admidio.Admidio(
+ self.config['database_credentials']['host'],
+ self.config['database_credentials']['username'],
+ self.config['database_credentials']['password'],
+ self.config['database_credentials']['database'],
+ self.config['database_credentials']['adm_prefix'])
+
# Define sensitivity
self.handler = []
@@ -64,7 +78,6 @@ class Plugin:
database = self.config['database_credentials']['database'],
host = self.config['database_credentials']['host'])
-
def check_new_event_thread(self):
while not self.bot.cancel:
self.check_new_event()
@@ -150,7 +163,8 @@ class Plugin:
if room.display_name in mapping[1]:
room.send_html(
self.messages["new_event"].format(
- row[3].strftime("%d-%m-%Y om %H:%M"),
+ row[3].strftime("%d-%m-%Y"),
+ row[3].strftime("%H:%M"),
row[2],
view_link,
attend_link,
@@ -183,42 +197,33 @@ class Plugin:
room.send_text(self.messages['list_event_num_err'])
number_events = 10
- datetime_now = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- mysql_db = self.connect_database()
- mysql_cursor = mysql_db.cursor()
+ base_date_url = "{}{}".format(
+ self.config['base_url_adm'],
+ "/adm_program/modules/dates/")
- select_sql = f"""SELECT dat_id, dat_cat_id,
- dat_headline, dat_begin, dat_end
- FROM adm_dates
- WHERE dat_begin >'{datetime_now}'
- ORDER BY dat_begin
- LIMIT {number_events}"""
+ function_link = "{}dates_function.php?mode={}&dat_id={}&"
- mysql_cursor.execute(select_sql)
-
- # Fetch events
- results = mysql_cursor.fetchall()
+ now = dt.datetime.now()
+ events = (x for (k,x) in self.adm.events.items() if x.start_time > now)
# Set header
html_message = self.messages['list_events_head'].format(number_events)
# Loop through fetched events
html_message += "
"
- for row in results:
- base_date_url = "{}{}".format(
- self.config['base_url_adm'],
- "/adm_program/modules/dates/")
+ for (i, event) in enumerate(events):
+ if (i >= number_events):
+ break;
- function_link = "{}dates_function.php?mode={}&dat_id={}&"
-
- attend_link = function_link.format(base_date_url, '3', row[0])
+ attend_link = function_link.format(base_date_url, '3', event.id)
html_message += "- "
html_message += self.messages['list_event_item'].format(
- row[3].strftime("%d-%m-%Y om %H:%M"),
- row[2],
- row[0],
+ event.start_time.strftime("%d-%m-%Y"),
+ event.start_time.strftime("%H:%M"),
+ event.name,
+ event.id,
attend_link)
html_message += "
"
@@ -227,9 +232,6 @@ class Plugin:
# Send message
room.send_html(html_message)
- # Close DB connection
- mysql_db.close()
-
def info_callback(self, room, event):
id_event = False
@@ -244,39 +246,89 @@ class Plugin:
room.send_html(self.messages['info_event_id_err'])
return
- mysql_db = self.connect_database()
- mysql_cursor = mysql_db.cursor()
+ try:
+ date_url = "{}{}{}".format(
+ self.config['base_url_adm'],
+ "/adm_program/modules/dates/dates.php?id=",
+ self.adm.events[id_event].id)
- select_sql = f"""SELECT dat_id, dat_headline, dat_description,
- dat_begin, dat_end
- FROM adm_dates
- WHERE dat_id = {id_event}"""
+ html_message = self.messages['info_event'].format(
+ date_url,
+ self.adm.events[id_event].name,
+ self.adm.events[id_event].start_time.strftime("%d-%m-%Y"),
+ self.adm.events[id_event].start_time.strftime("%H:%M"),
+ self.adm.events[id_event].end_time.strftime("%d-%m-%Y"),
+ self.adm.events[id_event].end_time.strftime("%H:%M"),
+ self.adm.events[id_event].description)
- mysql_cursor.execute(select_sql)
+ # Send message
+ room.send_html(html_message)
- # Fetch events
- row = mysql_cursor.fetchall()
+ except KeyError:
+ room.send_html(self.messages['unknown_event'])
- date_url = "{}{}{}".format(
- self.config['base_url_adm'],
- "/adm_program/modules/dates/dates.php?id=",
- row[0][0])
-
- html_message = self.messages['info_event'].format(
- date_url,
- row[0][1],
- row[0][3].strftime("%d-%m-%Y om %H:%M"),
- row[0][4].strftime("%d-%m-%Y om %H:%M"),
- row[0][2])
-
- # Send message
- room.send_html(html_message)
-
- # Close DB connection
- mysql_db.close()
def participants_callback(self, room, event):
- room.send_text("Deelnemers")
+ id_event = False
+
+ for word in event['content']['body'].split():
+ try:
+ id_event = int(word)
+ break
+ except:
+ pass
+
+ if not id_event or id_event < 0:
+ room.send_html(self.messages['info_event_id_err'])
+ return
+
+ # Set header
+ html_message = self.messages['event_participants_head'].format(
+ len(self.adm.events[id_event].getAllAttend()),
+ self.adm.events[id_event].number_of_guests,
+ self.adm.events[id_event].name)
+
+ # Attend
+ html_message += "
"
+ html_message += self.messages['event_participants_attend'];
+
+ # Loop through fetched participants
+ html_message += ""
+
+ for member in self.adm.events[id_event].getAllAttend():
+ html_message += "- "
+ html_message += member[1].username
+ html_message += "
"
+
+ html_message += "
"
+
+ # Maybe
+ html_message += self.messages['event_participants_maybe'];
+
+ # Loop through fetched participants
+ html_message += ""
+
+ for member in self.adm.events[id_event].getAllMaybe():
+ html_message += "- "
+ html_message += member[1].username
+ html_message += "
"
+
+ html_message += "
"
+
+ # Not attend
+ html_message += self.messages['event_participants_not_attend'];
+
+ # Loop through fetched participants
+ html_message += ""
+
+ for member in self.adm.events[id_event].getAllNotAttend():
+ html_message += "- "
+ html_message += member[1].username
+ html_message += "
"
+
+ html_message += "
"
+
+ room.send_html(html_message)
def chat_callback(self, room, event):
room.send_text("Chat")
diff --git a/plugins/admidio_events/config.hjson.example b/plugins/admidio_events/config.hjson.example
index b6f939e..5efc8e0 100644
--- a/plugins/admidio_events/config.hjson.example
+++ b/plugins/admidio_events/config.hjson.example
@@ -3,7 +3,8 @@
"username": "",
"password": "",
"database": "",
- "host": ""
+ "host": "",
+ "adm_prefix": "adm"
},
"cat_id_room_mapping": [
[[], ""],
diff --git a/plugins/admidio_events/messages/messages.dutch.hjson b/plugins/admidio_events/messages/messages.dutch.hjson
index 118df48..fb16c3e 100644
--- a/plugins/admidio_events/messages/messages.dutch.hjson
+++ b/plugins/admidio_events/messages/messages.dutch.hjson
@@ -2,7 +2,7 @@
"new_event":
'''
@room er is zojuist een nieuw evenement aangemaakt!
- Op {} zal "{}" plaatsvinden.
+ Op {} om {} zal "{}" plaatsvinden.
@@ -24,7 +24,7 @@
"list_event_item":
'''
- {}: {} (ID: {}, aanmelden)
+ {} om {}: {} (ID: {}, aanmelden)
''',
"list_event_num_err":
@@ -44,11 +44,38 @@
"info_event":
'''
Je hebt informatie opgevraagd over het evenement: "{}".
- Het evenement begint op {} en eindigt op {}. De maker van het
+ Het evenement begint op {} om {} en eindigt op {} om {}. De maker van het
evenement heeft deze beschrijving toegevoegd:
{}
+ ''',
+
+ "unknown_event":
'''
+ Sorry, ik ken helaas geen evenement met dit ID. Probeer het alsjeblieft met
+ een ander ID.
+ ''',
+
+ "event_participants_head":
+ '''
+ Er nemen {} leden en {} gasten deel aan "{}". De aangemelde leden zijn:
+ ''',
+
+ "event_participants_attend":
+ '''
+ Aanwezig:
+ ''',
+
+ "event_participants_maybe":
+ '''
+ Maybe:
+ ''',
+
+ "event_participants_not_attend":
+ '''
+ Afwezig:
+ ''',
+
}
diff --git a/plugins/admidio_events/requirements.txt b/plugins/admidio_events/requirements.txt
index 18c098a..6d95b89 100644
--- a/plugins/admidio_events/requirements.txt
+++ b/plugins/admidio_events/requirements.txt
@@ -1 +1,2 @@
mysqlclient
+python-dateutil