SSO and messages
* adds message for checking of intervention * refactors sso message sending to handle an iterable of users * adds specific method for sending object checking related messages * adds label support for modal form for more simple clicking of checkboxes
This commit is contained in:
parent
1a4a4a16c6
commit
833483b810
@ -19,6 +19,7 @@ from intervention.models import Intervention, Revocation
|
||||
from konova.forms import BaseForm, BaseModalForm
|
||||
from konova.models import Document
|
||||
from konova.settings import DEFAULT_LAT, DEFAULT_LON, DEFAULT_ZOOM, ZB_GROUP, ETS_GROUP
|
||||
from konova.utils.messenger import Messenger
|
||||
from konova.utils.user_checks import in_group
|
||||
from organisation.models import Organisation
|
||||
from user.models import UserActionLogEntry, UserAction
|
||||
@ -429,6 +430,16 @@ class RunCheckForm(BaseModalForm):
|
||||
self.instance.log.add(user_action)
|
||||
self.instance.save()
|
||||
|
||||
# Send message to the SSO server
|
||||
messenger = Messenger(
|
||||
self.instance.users.all(),
|
||||
type="INFO",
|
||||
)
|
||||
messenger.send_object_checked(
|
||||
self.instance.identifier,
|
||||
self.user,
|
||||
)
|
||||
|
||||
|
||||
class NewWithdrawForm(BaseModalForm):
|
||||
""" Form for creating new withdraws
|
||||
|
@ -5,10 +5,11 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 17.08.21
|
||||
|
||||
"""
|
||||
import json
|
||||
from collections import Iterable
|
||||
|
||||
import requests
|
||||
from django.contrib.auth.models import User
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from konova.settings import SSO_SERVER_BASE, SSO_PUBLIC_KEY
|
||||
from konova.sub_settings.context_settings import BASE_TITLE_SHORT
|
||||
@ -24,17 +25,15 @@ class Messenger:
|
||||
"""
|
||||
server_url = "{}communication/message/".format(SSO_SERVER_BASE)
|
||||
|
||||
def __init__(self, user: User, subject: str = None, body: str = None, type: str = None):
|
||||
self.user = user
|
||||
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) -> bool:
|
||||
""" Sends the message
|
||||
def send(self):
|
||||
""" Sends a message
|
||||
|
||||
Returns:
|
||||
result (bool): True if successfully send, False otherwise
|
||||
"""
|
||||
if self.msg_body is None or len(self.msg_body) == 0:
|
||||
raise AttributeError("No message body set")
|
||||
@ -42,21 +41,37 @@ class Messenger:
|
||||
headers = {
|
||||
"x-services-public-key": SSO_PUBLIC_KEY
|
||||
}
|
||||
for user in self.users:
|
||||
data = {
|
||||
"type": self.msg_type,
|
||||
"sender": BASE_TITLE_SHORT,
|
||||
"receiver": self.user.username,
|
||||
"receiver": user.username,
|
||||
"subject": self.msg_subject,
|
||||
"body": self.msg_body,
|
||||
}
|
||||
result = requests.post(
|
||||
requests.post(
|
||||
self.server_url,
|
||||
data=data,
|
||||
headers=headers
|
||||
)
|
||||
if result.status_code == 200:
|
||||
result_content = json.loads(result.content)
|
||||
success = result_content.get("success")
|
||||
return success
|
||||
else:
|
||||
return False
|
||||
|
||||
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()
|
||||
|
@ -5,7 +5,6 @@ Contact: michel.peltriaux@sgdnord.rlp.de
|
||||
Created on: 16.11.20
|
||||
|
||||
"""
|
||||
from django.contrib import messages
|
||||
from django.contrib.auth import logout
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.http import HttpRequest, FileResponse
|
||||
|
@ -4,7 +4,7 @@
|
||||
{% for field in form %}
|
||||
<tr title="{{ field.help_text }}" class="{% if field.errors %}error{% endif %}">
|
||||
<th scope="row" class="col-sm-3">
|
||||
<div>{{ field.label }}<span class="label-required">{% if field.field.required %}*{% endif %}</span></div>
|
||||
<label for="id_{{ field.name }}">{{ field.label }}<span class="label-required">{% if field.field.required %}*{% endif %}</span></label>
|
||||
<small>{{ field.help_text }}</small>
|
||||
</th>
|
||||
<td class="col-sm-9">
|
||||
|
Loading…
Reference in New Issue
Block a user