* refactors django User model to custom User model to provide further attributes and methods directly on the user model
79 lines
2.6 KiB
Python
79 lines
2.6 KiB
Python
"""
|
|
Author: Michel Peltriaux
|
|
Organization: Struktur- und Genehmigungsdirektion Nord, Rhineland-Palatinate, Germany
|
|
Contact: michel.peltriaux@sgdnord.rlp.de
|
|
Created on: 17.08.21
|
|
|
|
"""
|
|
from collections import Iterable
|
|
|
|
import requests
|
|
from user.models import User
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from konova.settings import SSO_SERVER_BASE, SSO_PUBLIC_KEY, PROXIES
|
|
from konova.sub_settings.context_settings import BASE_TITLE_SHORT
|
|
|
|
|
|
class Messenger:
|
|
""" Used to send messages to the SSO server.
|
|
|
|
Messages can be seen by the user the next time they login on their SSO dashboard.
|
|
Documentation for SSO Server-Client communication can be found here:
|
|
https://git.naturschutz.rlp.de/SGD-Nord/arnova/wiki/Messages
|
|
|
|
"""
|
|
server_url = "{}communication/message/".format(SSO_SERVER_BASE)
|
|
|
|
def __init__(self, users: Iterable, subject: str = None, body: str = None, type: str = None):
|
|
self.users = users
|
|
self.msg_subject = subject
|
|
self.msg_body = body
|
|
self.msg_type = type
|
|
|
|
def send(self):
|
|
""" Sends a message
|
|
|
|
"""
|
|
if self.msg_body is None or len(self.msg_body) == 0:
|
|
raise AttributeError("No message body set")
|
|
|
|
headers = {
|
|
"x-services-public-key": SSO_PUBLIC_KEY
|
|
}
|
|
for user in self.users:
|
|
data = {
|
|
"type": self.msg_type,
|
|
"sender": BASE_TITLE_SHORT,
|
|
"receiver": user.username,
|
|
"subject": self.msg_subject,
|
|
"body": self.msg_body,
|
|
}
|
|
requests.post(
|
|
self.server_url,
|
|
data=data,
|
|
headers=headers,
|
|
proxies=PROXIES
|
|
)
|
|
|
|
def send_object_checked(self, obj_identifier: str, performing_user: User, detail_view_url: str = ""):
|
|
""" Wraps sending of a message related to the checking of an object, like an intervention
|
|
|
|
Args:
|
|
obj_identifier (str): The object's identifier (e.g. 'EIV-123'
|
|
performing_user (User): The user who performed the checking
|
|
detail_view_url (str): If a direct link to the object shall be added to the message, it can be provided here
|
|
|
|
Returns:
|
|
|
|
"""
|
|
self.msg_subject = _("{} checked").format(obj_identifier)
|
|
if len(detail_view_url) > 0:
|
|
detail_view_url = _('<a href="{}">Check it out</a>').format(detail_view_url)
|
|
self.msg_body = _("{} has been checked successfully by user {}! {}").format(
|
|
obj_identifier,
|
|
performing_user.username,
|
|
detail_view_url
|
|
)
|
|
self.send()
|