@ -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 , user s: Iterable , subject : str = None , body : str = None , type : str = None ) :
self . user s = user s
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
}
data = {
" type " : self . msg_type ,
" sender " : BASE_TITLE_SHORT ,
" receiver " : self . user . username ,
" subject " : self . msg_subject ,
" body " : self . msg_body ,
}
result = requests . post (
self . server_url ,
data = data ,
headers = headers
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
)
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
)
if result . status_code == 200 :
result_content = json . loads ( result . content )
success = result_content . get ( " success " )
return success
else :
return False
self . send ( )