Better fill codes
This commit is contained in:
@@ -32,11 +32,61 @@ def generate_mail_from_notification(notif: Notif) -> Optional[Mail]:
|
||||
try:
|
||||
# Fill in the template with notification details
|
||||
filled_body = template.body
|
||||
|
||||
# Add user information
|
||||
if notif.user:
|
||||
filled_body = filled_body.replace('{{ user.username }}', notif.user.username)
|
||||
filled_body = filled_body.replace('{{ user.username }}', notif.user.username + ' ' + notif.user.last_name)
|
||||
filled_body = filled_body.replace('{{ user.email }}', notif.user.email)
|
||||
if hasattr(notif.user, 'company'):
|
||||
filled_body = filled_body.replace('{{ user.company }}', notif.user.company or '')
|
||||
if hasattr(notif.user, 'position'):
|
||||
filled_body = filled_body.replace('{{ user.position }}', notif.user.position or '')
|
||||
|
||||
# Add sender information if available
|
||||
if notif.sender:
|
||||
filled_body = filled_body.replace('{{ sender.username }}', notif.sender.username + ' ' + notif.sender.last_name)
|
||||
filled_body = filled_body.replace('{{ sender.email }}', notif.sender.email)
|
||||
|
||||
# Add site information
|
||||
from models import SiteSettings
|
||||
site_settings = SiteSettings.query.first()
|
||||
if site_settings:
|
||||
filled_body = filled_body.replace('{{ site.company_name }}', site_settings.company_name or '')
|
||||
filled_body = filled_body.replace('{{ site.company_website }}', site_settings.company_website or '')
|
||||
|
||||
# Add notification details
|
||||
if notif.details:
|
||||
for key, value in notif.details.items():
|
||||
filled_body = filled_body.replace(f'{{{{ {key} }}}}', str(value))
|
||||
# Handle nested keys (e.g., room.name -> room_name)
|
||||
if '.' in key:
|
||||
parts = key.split('.')
|
||||
if len(parts) == 2:
|
||||
obj_name, attr = parts
|
||||
if obj_name in notif.details and isinstance(notif.details[obj_name], dict):
|
||||
if attr in notif.details[obj_name]:
|
||||
filled_body = filled_body.replace(f'{{{{ {key} }}}}', str(notif.details[obj_name][attr]))
|
||||
else:
|
||||
filled_body = filled_body.replace(f'{{{{ {key} }}}}', str(value))
|
||||
|
||||
# Handle special URL variables
|
||||
if 'room_link' in filled_body and 'room_id' in notif.details:
|
||||
from flask import url_for
|
||||
room_link = url_for('rooms.room', room_id=notif.details['room_id'], _external=True)
|
||||
filled_body = filled_body.replace('{{ room_link }}', room_link)
|
||||
|
||||
if 'conversation_link' in filled_body and 'conversation_id' in notif.details:
|
||||
from flask import url_for
|
||||
conversation_link = url_for('conversations.conversation', conversation_id=notif.details['conversation_id'], _external=True)
|
||||
filled_body = filled_body.replace('{{ conversation_link }}', conversation_link)
|
||||
|
||||
# Add timestamps
|
||||
filled_body = filled_body.replace('{{ created_at }}', notif.timestamp.strftime('%Y-%m-%d %H:%M:%S'))
|
||||
if 'updated_at' in filled_body:
|
||||
filled_body = filled_body.replace('{{ updated_at }}', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
|
||||
if 'deleted_at' in filled_body:
|
||||
filled_body = filled_body.replace('{{ deleted_at }}', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
|
||||
if 'removed_at' in filled_body:
|
||||
filled_body = filled_body.replace('{{ removed_at }}', datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
|
||||
|
||||
# Create a new Mail record
|
||||
mail = Mail(
|
||||
|
||||
Reference in New Issue
Block a user