2024-06-12 16:44:05 +05:30
from django . db import models
from django . urls import reverse
from django . utils . translation import gettext_lazy as _trans
2024-06-13 11:22:59 +05:30
2024-11-26 14:24:55 +05:30
from base . methods import eval_validate
2024-08-05 14:22:44 +05:30
from base . models import HorillaMailTemplate
2024-12-09 11:12:17 +05:30
from employee . models import Employee
2024-06-13 11:22:59 +05:30
from horilla . models import HorillaModel
2024-06-12 16:44:05 +05:30
from horilla_views . cbv_methods import render_template
MODEL_CHOICES = [ ]
CONDITIONS = [
( " equal " , _trans ( " Equal (==) " ) ) ,
( " notequal " , _trans ( " Not Equal (!=) " ) ) ,
( " lt " , _trans ( " Less Than (<) " ) ) ,
( " gt " , _trans ( " Greater Than (>) " ) ) ,
( " le " , _trans ( " Less Than or Equal To (<=) " ) ) ,
( " ge " , _trans ( " Greater Than or Equal To (>=) " ) ) ,
( " icontains " , _trans ( " Contains " ) ) ,
]
class MailAutomation ( HorillaModel ) :
"""
MailAutoMation
"""
choices = [
( " on_create " , " On Create " ) ,
( " on_update " , " On Update " ) ,
( " on_delete " , " On Delete " ) ,
]
2025-04-28 14:28:01 +05:30
SEND_OPTIONS = [
( " email " , " Send as Email " ) ,
( " notification " , " Send as Notification " ) ,
( " both " , " Send as Email and Notification " ) ,
]
2024-11-06 09:31:40 +05:30
title = models . CharField ( max_length = 256 , unique = True )
2024-06-12 16:44:05 +05:30
method_title = models . CharField ( max_length = 50 , editable = False )
model = models . CharField ( max_length = 100 , choices = MODEL_CHOICES , null = False )
2025-05-09 11:44:07 +05:30
mail_to = models . TextField ( verbose_name = " Mail to/Notify to " )
2024-06-12 16:44:05 +05:30
mail_details = models . CharField (
max_length = 250 ,
2024-12-09 11:12:17 +05:30
help_text = _trans (
2025-03-10 13:57:45 +05:30
" Fill mail template details(reciever/instance, `self` will be the person who trigger the automation), `As template` option will sent instead of the mail template "
2024-12-09 11:12:17 +05:30
) ,
2024-06-12 16:44:05 +05:30
)
mail_detail_choice = models . TextField ( default = " " , editable = False )
trigger = models . CharField ( max_length = 10 , choices = choices )
# udpate the on_update logic to if and only if when
# changes in the previous and current value
2025-03-10 13:57:45 +05:30
mail_template = models . ForeignKey (
HorillaMailTemplate , on_delete = models . CASCADE , null = True , blank = True
)
2024-12-09 11:12:17 +05:30
also_sent_to = models . ManyToManyField (
Employee ,
blank = True ,
verbose_name = _trans ( " Also Send to " ) ,
)
2025-04-28 14:28:01 +05:30
delivary_channel = models . CharField (
default = " email " ,
max_length = 50 ,
choices = SEND_OPTIONS ,
verbose_name = _trans ( " Choose Delivary Channel " ) ,
)
2024-06-12 16:44:05 +05:30
template_attachments = models . ManyToManyField (
2024-08-05 14:22:44 +05:30
HorillaMailTemplate ,
2024-06-12 16:44:05 +05:30
related_name = " template_attachment " ,
blank = True ,
)
condition_html = models . TextField ( null = True , editable = False )
condition_querystring = models . TextField ( null = True , editable = False )
condition = models . TextField ( )
2025-05-19 10:41:07 +05:30
xss_exempt_fields = [
" condition_html " ,
" condition " ,
" condition_querystring " ,
]
2024-06-12 16:44:05 +05:30
def save ( self , * args , * * kwargs ) :
if not self . pk :
self . method_title = self . title . replace ( " " , " _ " ) . lower ( )
return super ( ) . save ( * args , * * kwargs )
def __str__ ( self ) - > str :
return self . title
def get_avatar ( self ) :
"""
Method will retun the api to the avatar or path to the profile image
"""
url = f " https://ui-avatars.com/api/?name= { self . title } &background=random "
return url
def get_mail_to_display ( self ) :
"""
method that returns the display value for ` mail_to `
field
"""
2024-11-26 14:24:55 +05:30
mail_to = eval_validate ( self . mail_to )
2024-06-12 16:44:05 +05:30
mappings = [ ]
for mapping in mail_to :
mapping = mapping . split ( " __ " )
display = " "
for split in mapping :
split = split . replace ( " _id " , " " ) . replace ( " _ " , " " )
split = split . capitalize ( )
display = display + f " { split } > "
display = display [ : - 1 ]
mappings . append ( display )
return render_template (
" horilla_automations/mail_to.html " , { " instance " : self , " mappings " : mappings }
)
2024-12-09 11:12:17 +05:30
def get_mail_cc_display ( self ) :
employees = self . also_sent_to . all ( )
return render_template (
" horilla_automations/mail_cc.html " , { " employees " : employees }
)
2024-06-12 16:44:05 +05:30
def detailed_url ( self ) :
return reverse ( " automation-detailed-view " , kwargs = { " pk " : self . pk } )
def conditions ( self ) :
return render_template (
" horilla_automations/conditions.html " , { " instance " : self }
)
def delete_url ( self ) :
return reverse ( " delete-automation " , kwargs = { " pk " : self . pk } )
def edit_url ( self ) :
"""
Edit url
"""
return reverse ( " update-automation " , kwargs = { " pk " : self . pk } )
def trigger_display ( self ) :
""" """
return self . get_trigger_display ( )