Skip to content

Commit

Permalink
feat(notify): added notification configuration to GUI (#463)
Browse files Browse the repository at this point in the history
* added notifications to GUI

* added tabstops

* Update tools/gui/qtkontakt.py

Co-authored-by: Florian Glaser <58706771+Floskinner@users.noreply.github.com>

Co-authored-by: Florian Glaser <58706771+Floskinner@users.noreply.github.com>
  • Loading branch information
marcorombach and Floskinner committed Jun 14, 2021
1 parent 5bb928e commit 04bdbe2
Show file tree
Hide file tree
Showing 2 changed files with 222 additions and 5 deletions.
117 changes: 117 additions & 0 deletions tools/gui/kontaktdaten.ui
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,119 @@
</item>
</layout>
</widget>
<widget class="QWidget" name="notifications_tab">
<attribute name="title">
<string>Benachrichtigungen</string>
</attribute>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="pushover_label">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>PushOver</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="app_token_label">
<property name="text">
<string>App-Token:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="i_app_token">
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="user_key_label">
<property name="text">
<string>User-Key:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="i_user_key">
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="b_test_pushover">
<property name="text">
<string>Pushover Testen</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="spacer_label">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string></string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="telegram_label">
<property name="font">
<font>
<pointsize>12</pointsize>
</font>
</property>
<property name="text">
<string>Telegram</string>
</property>
</widget>
</item>
<item row="6" column="0">
<widget class="QLabel" name="api_token_label">
<property name="text">
<string>Api-Token:</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QLineEdit" name="i_api_token">
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="7" column="0">
<widget class="QLabel" name="chat_id_label">
<property name="text">
<string>Chat-ID:</string>
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QLineEdit" name="i_chat_id">
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="8" column="0">
<widget class="QPushButton" name="b_test_telegram">
<property name="text">
<string>Telegram Testen</string>
</property>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
Expand Down Expand Up @@ -938,6 +1051,10 @@
<tabstop>i_start_datum_qdate</tabstop>
<tabstop>i_start_time_qtime</tabstop>
<tabstop>i_end_time_qtime</tabstop>
<tabstop>i_app_token</tabstop>
<tabstop>i_user_key</tabstop>
<tabstop>i_api_token</tabstop>
<tabstop>i_chat_id</tabstop>
</tabstops>
<resources/>
<connections/>
Expand Down
110 changes: 105 additions & 5 deletions tools/gui/qtkontakt.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from tools.gui.qtimpfzentren import QtImpfzentren
from tools import kontaktdaten as kontakt_tools
from tools import Modus
from tools.exceptions import ValidationError, MissingValuesError
from tools.exceptions import ValidationError, MissingValuesError, PushoverNotificationError, TelegramNotificationError
from tools.utils import pushover_notification, telegram_notification

# Folgende Widgets stehen zur Verfügung:

Expand All @@ -27,6 +28,10 @@
# i_plz_wohnort
# i_telefon
# i_mail
# i_app_token
# i_user_key
# i_api_token
# i_chat_id

### Checkboxes ###
# i_mo_check_box
Expand Down Expand Up @@ -59,9 +64,12 @@
# kontaktdaten_tab
# zeitrahmen_tab
# vermittlungscodes_tab
# notifications_tab

### Buttons ###
# b_impfzentren_waehlen
# b_test_pushover
# b_test_telegram

PATH = os.path.dirname(os.path.realpath(__file__))

Expand Down Expand Up @@ -89,6 +97,8 @@ def __init__(self, parent: QtWidgets.QWidget, modus: Modus, standard_speicherpfa

# Funktion vom Button zuordnen
self.b_impfzentren_waehlen.clicked.connect(self.__open_impfzentren)
self.b_test_pushover.clicked.connect(self.__test_pushover)
self.b_test_telegram.clicked.connect(self.__test_telegram)

# Versuche Kontakdaten zu laden
self.__lade_alle_werte()
Expand Down Expand Up @@ -165,7 +175,7 @@ def bestaetigt(self):

# Daten speichern
speicherpfad = self.speicher_einstellungen(data)
QtWidgets.QMessageBox.information(self, "Gepseichert", "Daten erfolgreich gespeichert")
QtWidgets.QMessageBox.information(self, "Gespeichert", "Daten erfolgreich gespeichert")

# Neuer Pfad in der Main GUI übernehmen
self.update_path.emit(speicherpfad)
Expand Down Expand Up @@ -239,10 +249,12 @@ def __button_box_clicked(self, button: QtWidgets.QPushButton):
self.__reset_vermittlungscodes()
self.__reset_kontakdaten()
self.__reset_zeitrahmen()
self.__reset_notifications()
elif clicked_button == QtWidgets.QDialogButtonBox.Open:
self.__reset_vermittlungscodes()
self.__reset_kontakdaten()
self.__reset_zeitrahmen()
self.__reset_notifications()
self.__lade_einstellungen()
elif clicked_button == QtWidgets.QDialogButtonBox.Cancel:
self.reject()
Expand All @@ -266,6 +278,7 @@ def __get_alle_werte(self) -> dict:
plz_wohnort = self.i_plz_wohnort.text().strip()
telefon = self.i_telefon.text().strip()
mail = self.i_mail.text().strip()
notifications = self.__get_notifications()

# PLZ der Zentren in Liste und "strippen"
plz_zentren = plz_zentrum_raw.split(",")
Expand All @@ -286,6 +299,7 @@ def __get_alle_werte(self) -> dict:
"notificationChannel": "email",
"notificationReceiver": mail
},
"notifications": notifications,
"zeitrahmen": self.__get_zeitrahmen()
}

Expand Down Expand Up @@ -345,6 +359,8 @@ def __lade_alle_werte(self):
self.i_plz_wohnort.setText(kontaktdaten["kontakt"]["plz"])
self.i_wohnort.setText(kontaktdaten["kontakt"]["ort"])

self.__set_notifications(kontaktdaten['notifications'])

try:
self.__set_zeitrahmen(kontaktdaten["zeitrahmen"])
# Subkeys von "zeitrahmen" brauchen nicht gecheckt werden, da
Expand All @@ -362,6 +378,7 @@ def __lade_alle_werte(self):
self.__reset_vermittlungscodes()
self.__reset_kontakdaten()
self.__reset_zeitrahmen()
self.__reset_notifications()
self.__oeffne_error(title="Kontaktdaten", text="Falsches Format",
info= "Die von Ihnen gewählte Datei hat ein falsches Format. "
"Laden Sie eine andere Datei oder überschreiben Sie die "
Expand All @@ -371,6 +388,7 @@ def __lade_alle_werte(self):
self.__reset_vermittlungscodes()
self.__reset_kontakdaten()
self.__reset_zeitrahmen()
self.__reset_notifications()
self.__oeffne_error(title="Kontaktdaten", text="Falsches Format",
info= "Die von Ihnen gewählte Datei hat ein falsches Format. "
"Laden Sie eine andere Datei oder überschreiben Sie die "
Expand Down Expand Up @@ -707,8 +725,6 @@ def __set_start_datum(self, von_datum: str):
assert(QDate.isValid(datum))
self.i_start_datum_qdate.setDate(datum)



def __set_uhrzeit_datum(self, uhrzeit: str, widget: QtWidgets.QTimeEdit):
"""
Setzt die Uhrzeit in einem QTimeEdit in der GUI.
Expand All @@ -725,6 +741,90 @@ def __set_uhrzeit_datum(self, uhrzeit: str, widget: QtWidgets.QTimeEdit):
assert(QTime.isValid(time))
widget.setTime(time)

##############################
# Notifications #
##############################

def __get_notifications(self) -> dict:
"""
Ruft Werte für die Benachrichtigungen aus der GUI ab
und gibt diese als Dict zurück.
Returns:
Dict mit den notifications Werten
"""

# Pushover
app_token = self.i_app_token.text().strip()
user_key = self.i_user_key.text().strip()

# Telegram
api_token = self.i_api_token.text().strip()
chat_id = self.i_chat_id.text().strip()

notifications = {}

if app_token != "" and user_key != "":
notifications['pushover'] = {}
notifications['pushover']['app_token'] = app_token
notifications['pushover']['user_key'] = user_key

if api_token != "" and chat_id != "":
notifications['telegram'] = {}
notifications['telegram']['api_token'] = api_token
notifications['telegram']['chat_id'] = chat_id

return notifications

def __set_notifications(self, notifications: dict):
"""
Werte aus übergebenem Dict werden in die passenden QLineEdits geschrieben
Args:
notifications (dict): Enthält pushover und telegram spezifische Werte
"""

if 'pushover' in notifications:
self.i_app_token.setText(notifications['pushover']['app_token'])
self.i_user_key.setText(notifications['pushover']['user_key'])

if 'telegram' in notifications:
self.i_api_token.setText(notifications['telegram']['api_token'])
self.i_chat_id.setText(notifications['telegram']['chat_id'])

def __reset_notifications(self):
"""
Setzt alle Werte für die Benachrichtigungen (notifications) in der GUI zurück
"""

for line_edit in self.notifications_tab.findChildren(QtWidgets.QLineEdit):
line_edit.setText("")

def __test_pushover(self):
"""
Benutzt die Werte aus der GUI um eine Test-Benachrichtigung mit Pushover zu senden
"""

notifications = {'app_token': self.i_app_token.text().strip(), 'user_key': self.i_user_key.text().strip()}
try:
pushover_notification(notifications, "Vaccipy", "Die Benachrichtigungsfunktion funktioniert!")
except PushoverNotificationError as error:
self.__oeffne_error("Pushover Fehler",
"Vermutlich sind die Daten nicht korrekt, versuchen Sie es erneut.",
str(error))

def __test_telegram(self):
"""
Benutzt die Werte aus der GUI um eine Test-Benachrichtigung mit Telegram zu senden
"""

notifications = {'api_token': self.i_api_token.text().strip(), 'chat_id': self.i_chat_id.text().strip()}
try:
telegram_notification(notifications, "Vaccipy - Die Benachrichtigungsfunktion funktioniert!")
except TelegramNotificationError as error:
self.__oeffne_error("Telegram Fehler",
"Vermutlich sind die Daten nicht korrekt, versuchen Sie es erneut.",
str(error))

def __oeffne_error(self, title: str, text: str, info: str):
"""
Expand All @@ -744,4 +844,4 @@ def __oeffne_error(self, title: str, text: str, info: str):
msg.addButton(msg.Close)
msg.exec_()
except Exception as error:
pass
pass

0 comments on commit 04bdbe2

Please sign in to comment.