Admidio event plugin now makes full use of Admidio Python API

Since the API is now able to refresh the data, it is possible for the plugin to use the API in the monitoring thread as well.

This commit also includes some minor make up improvements of messages.
This commit is contained in:
Dennis Potter 2019-02-03 13:35:16 +01:00
parent ad99432335
commit c8b5926d78
Signed by: Dennis
GPG Key ID: 186A8AD440942BAF
3 changed files with 75 additions and 62 deletions

View File

@ -14,7 +14,6 @@ import threading
import importlib
import sys
import datetime as dt
import MySQLdb as mysql
from matrix_bot_api.mregex_handler import MRegexHandler
@ -70,13 +69,9 @@ class Plugin:
self.event_thread = threading.Thread(target=self.check_new_event_thread)
self.event_thread.start()
def connect_database(self):
# Connect to database
return mysql.connect(
user = self.config['database_credentials']['username'],
password = self.config['database_credentials']['password'],
database = self.config['database_credentials']['database'],
host = self.config['database_credentials']['host'])
def ready_block(self):
while (not self.adm.ready):
time.sleep(1)
def check_new_event_thread(self):
while not self.bot.cancel:
@ -92,24 +87,11 @@ class Plugin:
saved in an SQLite database.
"""
mysql_db = self.connect_database()
mysql_cursor = mysql_db.cursor()
# Grab all events from database that start in the future
select_sql = """SELECT dat_id, dat_cat_id,
dat_headline, dat_begin, dat_end
FROM adm_dates
WHERE dat_begin >'{}'""".format(
dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
mysql_cursor.execute(select_sql)
# Fetch events
results = mysql_cursor.fetchall()
# Refresh everything
self.adm.refresh()
# Open SQLite database
sqlite_db = sqlite3.connect(DATA_LOCATION)
sqlite_cursor = sqlite_db.cursor()
# Define query to SELECT event from SQLite DB
@ -121,9 +103,12 @@ class Plugin:
VALUES(:dat_id, :datetime_posted)"""
# Loop through event
for row in results:
now = dt.datetime.now()
events = (x for (k,x) in self.adm.events.items() if x.start_time > now)
for (i, event) in enumerate(events):
# First, check if a message on this event was already sent
sqlite_cursor.execute(select_sql.format(row[0]))
sqlite_cursor.execute(select_sql.format(event.id))
if sqlite_cursor.fetchall():
# Do nothing. This event was already processed
@ -132,56 +117,64 @@ class Plugin:
# This appears to be a new event. Process it!
# Generate links
base_date_url = "{}{}".format(
self.config['base_url_adm'],
"/adm_program/modules/dates/")
view_link = "{}dates.php?id={}&view_mode=html&"
view_link += "view=detail&headline={}"
view_link = view_link.format(base_date_url, row[0], row[2])
function_link = "{}dates_function.php?mode={}&dat_id={}&"
attend_link = function_link.format(base_date_url, '3', row[0])
maybe_link = function_link.format(base_date_url, '7', row[0])
cancel_link = function_link.format(base_date_url, '4', row[0])
links = self.generate_links(event.id, event.name)
# First, write to SQLite database that it is processed
sqlite_cursor.execute(
insert_sql, {
'dat_id':row[0],
'dat_id':event.id,
'datetime_posted':
dt.datetime.now().strftime("%Y-%m-%d %H:%M:%S")})
# Check role ID of event and check if config defines
# where it should be shared.
for mapping in self.config['cat_id_room_mapping']:
if row[1] in mapping[0]:
if event.cat_id in mapping[0]:
# We got a winner!
for i, room in self.bot.client.rooms.items():
if room.display_name in mapping[1]:
room.send_html(
self.messages["new_event"].format(
row[3].strftime("%d-%m-%Y"),
row[3].strftime("%H:%M"),
row[2],
view_link,
attend_link,
maybe_link,
cancel_link))
event.start_time.strftime("%d-%m-%Y"),
event.start_time.strftime("%H:%M"),
event.name,
links['view'],
links['attend'],
links['maybe'],
links['cancel']))
# Commit and close queries
sqlite_db.commit()
sqlite_db.close()
# Close MySQL
mysql_db.close()
# Sleep
time.sleep(self.config['update_time_span'] * 60)
def generate_links(self, event_id, event_name):
links = dict()
# Define the base URL for users to attend
base_dat_url = "{}{}".format(
self.config['base_url_adm'],
"/adm_program/modules/dates/")
links['view'] = "{}dates.php?id={}&view_mode=html&"
links['view'] += "view=detail&headline={}"
links['view'] = links['view'].format(base_dat_url, event_id, event_name)
function_link = "{}dates_function.php?mode={}&dat_id={}&"
links['attend'] = function_link.format(base_dat_url, '3', event_id)
links['maybe'] = function_link.format(base_dat_url, '7', event_id)
links['cancel'] = function_link.format(base_dat_url, '4', event_id)
return links
def list_callback(self, room, event):
self.ready_block()
number_events = False
for word in event['content']['body'].split():
@ -216,15 +209,16 @@ class Plugin:
if (i >= number_events):
break;
attend_link = function_link.format(base_date_url, '3', event.id)
# Generate links
links = self.generate_links(event.id, event.name)
html_message += "<li>"
html_message += self.messages['list_event_item'].format(
event.start_time.strftime("%d-%m-%Y"),
event.start_time.strftime("%H:%M"),
links['view'],
event.name,
event.id,
attend_link)
event.id)
html_message += "</li>"
html_message += "</ul>"
@ -233,6 +227,8 @@ class Plugin:
room.send_html(html_message)
def info_callback(self, room, event):
self.ready_block()
id_event = False
for word in event['content']['body'].split():
@ -247,19 +243,23 @@ class Plugin:
return
try:
date_url = "{}{}{}".format(
self.config['base_url_adm'],
"/adm_program/modules/dates/dates.php?id=",
self.adm.events[id_event].id)
# Generate links
links = self.generate_links(
self.adm.events[id_event].id,
self.adm.events[id_event].name)
# Assemble message
html_message = self.messages['info_event'].format(
date_url,
links['view'],
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)
self.adm.events[id_event].description,
links['attend'],
links['maybe'],
links['cancel'])
# Send message
room.send_html(html_message)
@ -269,6 +269,8 @@ class Plugin:
def participants_callback(self, room, event):
self.ready_block()
id_event = False
for word in event['content']['body'].split():

@ -1 +1 @@
Subproject commit eaa46ba42dc9c07f15d7af0c96169ce644a010f9
Subproject commit 292b046b629ae9ad0be0e5f460aa02901cd69083

View File

@ -19,12 +19,15 @@
"list_events_head":
'''
De aankomende {} evenementen zijn:
Alsjeblieft, een lijst met de de aankomende {} evenementen. De naam
van het evenement linkt meteen door naar de database. Tussen haakjes
achter het evenement staat het identificatienummer (ID) van het evenement.
Dit kun je gebruiken met commando's zoals "Peter evenement info <ID>".
''',
"list_event_item":
'''
{} om {}: <strong>{}</strong> (ID: {}, <a href='{}'>aanmelden</a>)
{} om {}: <strong><a href='{}'>{}</a></strong> ({})
''',
"list_event_num_err":
@ -50,6 +53,14 @@
<br /><br />
<blockquote>{}</blockquote>
Je kunt gebruik maken van onderstaande links als je aangemeld bent op
de database in je browser. Klik hier
<ul>
<li>&nbsp;<a href='{}'>om je op aanwezig te zetten</a> ✅</li>
<li>&nbsp;<a href='{}'>om je op misschien te zetten</a> 🤷</li>
<li>&nbsp;<a href='{}'>om je op afwezig te zetten</a> ⛔</li>
</ul>
''',
"unknown_event":