Most of the Admidio event plugins now makes use of the Admidio Python API

This commit also adds the function which enables users to request participants from the bot.
This commit is contained in:
Dennis Potter 2019-02-02 16:16:37 +01:00
parent a3a39f8b8f
commit 2b488ee1f8
Signed by: Dennis
GPG Key ID: 186A8AD440942BAF
4 changed files with 140 additions and 59 deletions

View File

@ -11,11 +11,18 @@ import hjson
import time import time
import sqlite3 import sqlite3
import threading import threading
import importlib
import sys
import datetime as dt import datetime as dt
import MySQLdb as mysql import MySQLdb as mysql
from matrix_bot_api.mregex_handler import MRegexHandler 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') MESSAGES_DIR = os.path.join(os.path.dirname(__file__), 'messages')
DATA_DIR = os.path.join(os.path.dirname(__file__),'../../data/events') DATA_DIR = os.path.join(os.path.dirname(__file__),'../../data/events')
CONFIG_LOCATION = os.path.join(os.path.dirname(__file__), 'config.hjson') CONFIG_LOCATION = os.path.join(os.path.dirname(__file__), 'config.hjson')
@ -37,6 +44,13 @@ class Plugin:
with open(MESSAGES_LOCATION) as hjson_data: with open(MESSAGES_LOCATION) as hjson_data:
self.messages = hjson.load(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 # Define sensitivity
self.handler = [] self.handler = []
@ -64,7 +78,6 @@ class Plugin:
database = self.config['database_credentials']['database'], database = self.config['database_credentials']['database'],
host = self.config['database_credentials']['host']) host = self.config['database_credentials']['host'])
def check_new_event_thread(self): def check_new_event_thread(self):
while not self.bot.cancel: while not self.bot.cancel:
self.check_new_event() self.check_new_event()
@ -150,7 +163,8 @@ class Plugin:
if room.display_name in mapping[1]: if room.display_name in mapping[1]:
room.send_html( room.send_html(
self.messages["new_event"].format( 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], row[2],
view_link, view_link,
attend_link, attend_link,
@ -183,42 +197,33 @@ class Plugin:
room.send_text(self.messages['list_event_num_err']) room.send_text(self.messages['list_event_num_err'])
number_events = 10 number_events = 10
datetime_now = dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
mysql_db = self.connect_database() base_date_url = "{}{}".format(
mysql_cursor = mysql_db.cursor() self.config['base_url_adm'],
"/adm_program/modules/dates/")
select_sql = f"""SELECT dat_id, dat_cat_id, function_link = "{}dates_function.php?mode={}&dat_id={}&"
dat_headline, dat_begin, dat_end
FROM adm_dates
WHERE dat_begin >'{datetime_now}'
ORDER BY dat_begin
LIMIT {number_events}"""
mysql_cursor.execute(select_sql) now = dt.datetime.now()
events = (x for (k,x) in self.adm.events.items() if x.start_time > now)
# Fetch events
results = mysql_cursor.fetchall()
# Set header # Set header
html_message = self.messages['list_events_head'].format(number_events) html_message = self.messages['list_events_head'].format(number_events)
# Loop through fetched events # Loop through fetched events
html_message += "<br /><ul>" html_message += "<br /><ul>"
for row in results: for (i, event) in enumerate(events):
base_date_url = "{}{}".format( if (i >= number_events):
self.config['base_url_adm'], break;
"/adm_program/modules/dates/")
function_link = "{}dates_function.php?mode={}&dat_id={}&" attend_link = function_link.format(base_date_url, '3', event.id)
attend_link = function_link.format(base_date_url, '3', row[0])
html_message += "<li>" html_message += "<li>"
html_message += self.messages['list_event_item'].format( html_message += self.messages['list_event_item'].format(
row[3].strftime("%d-%m-%Y om %H:%M"), event.start_time.strftime("%d-%m-%Y"),
row[2], event.start_time.strftime("%H:%M"),
row[0], event.name,
event.id,
attend_link) attend_link)
html_message += "</li>" html_message += "</li>"
@ -227,9 +232,6 @@ class Plugin:
# Send message # Send message
room.send_html(html_message) room.send_html(html_message)
# Close DB connection
mysql_db.close()
def info_callback(self, room, event): def info_callback(self, room, event):
id_event = False id_event = False
@ -244,39 +246,89 @@ class Plugin:
room.send_html(self.messages['info_event_id_err']) room.send_html(self.messages['info_event_id_err'])
return return
mysql_db = self.connect_database() try:
mysql_cursor = mysql_db.cursor() 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, html_message = self.messages['info_event'].format(
dat_begin, dat_end date_url,
FROM adm_dates self.adm.events[id_event].name,
WHERE dat_id = {id_event}""" 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 except KeyError:
row = mysql_cursor.fetchall() 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): 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 += "<br />"
html_message += self.messages['event_participants_attend'];
# Loop through fetched participants
html_message += "<ul>"
for member in self.adm.events[id_event].getAllAttend():
html_message += "<li>"
html_message += member[1].username
html_message += "</li>"
html_message += "</ul>"
# Maybe
html_message += self.messages['event_participants_maybe'];
# Loop through fetched participants
html_message += "<ul>"
for member in self.adm.events[id_event].getAllMaybe():
html_message += "<li>"
html_message += member[1].username
html_message += "</li>"
html_message += "</ul>"
# Not attend
html_message += self.messages['event_participants_not_attend'];
# Loop through fetched participants
html_message += "<ul>"
for member in self.adm.events[id_event].getAllNotAttend():
html_message += "<li>"
html_message += member[1].username
html_message += "</li>"
html_message += "</ul>"
room.send_html(html_message)
def chat_callback(self, room, event): def chat_callback(self, room, event):
room.send_text("Chat") room.send_text("Chat")

View File

@ -3,7 +3,8 @@
"username": "", "username": "",
"password": "", "password": "",
"database": "", "database": "",
"host": "" "host": "",
"adm_prefix": "adm"
}, },
"cat_id_room_mapping": [ "cat_id_room_mapping": [
[[], ""], [[], ""],

View File

@ -2,7 +2,7 @@
"new_event": "new_event":
''' '''
<strong>@room er is zojuist een nieuw evenement aangemaakt! <strong>@room er is zojuist een nieuw evenement aangemaakt!
Op {} zal "{}" plaatsvinden.</strong> Op {} om {} zal "{}" plaatsvinden.</strong>
<br /><br /> <br /><br />
@ -24,7 +24,7 @@
"list_event_item": "list_event_item":
''' '''
{}: <strong>{}</strong> (ID: {}, <a href='{}'>aanmelden</a>) {} om {}: <strong>{}</strong> (ID: {}, <a href='{}'>aanmelden</a>)
''', ''',
"list_event_num_err": "list_event_num_err":
@ -44,11 +44,38 @@
"info_event": "info_event":
''' '''
Je hebt informatie opgevraagd over het evenement: "<a href="{}">{}</a>". Je hebt informatie opgevraagd over het evenement: "<a href="{}">{}</a>".
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: evenement heeft deze beschrijving toegevoegd:
<br /><br /> <br /><br />
<blockquote>{}</blockquote> <blockquote>{}</blockquote>
''',
"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":
'''
<h5>Aanwezig:</h5>
''',
"event_participants_maybe":
'''
<h5>Maybe:</h5>
''',
"event_participants_not_attend":
'''
<h5>Afwezig:</h5>
''',
} }

View File

@ -1 +1,2 @@
mysqlclient mysqlclient
python-dateutil