from models import EmailTemplate, User, db def create_default_templates(): """Create default email templates if they don't exist.""" # Get the first admin user to be the creator admin = User.query.filter_by(is_admin=True).first() if not admin: print("No admin user found. Please create an admin user first.") return # Define default templates for each notification type default_templates = [ { 'name': 'Account Created', 'subject': 'Welcome to DocuPulse!', 'body': '''

Welcome to DocuPulse!

Dear {{ user.username }},

Your account has been successfully created. You can now log in and start using DocuPulse.

If you have any questions, please don't hesitate to contact our support team.

Best regards,
The DocuPulse Team

''' }, { 'name': 'Password Reset', 'subject': 'Password Reset Request', 'body': '''

Password Reset Request

Dear {{ user.username }},

We received a request to reset your password. Click the link below to set a new password:

Reset Password

If you didn't request this, please ignore this email or contact support if you have concerns.

Best regards,
The DocuPulse Team

''' }, { 'name': 'Account Deleted', 'subject': 'Your DocuPulse Account Has Been Deleted', 'body': '''

Account Deletion Confirmation

Dear {{ user.username }},

Your DocuPulse account has been successfully deleted as requested.

We're sorry to see you go. If you change your mind, you can create a new account at any time.

Best regards,
The DocuPulse Team

''' }, { 'name': 'Account Updated', 'subject': 'Your DocuPulse Account Has Been Updated', 'body': '''

Account Update Confirmation

Dear {{ user.username }},

Your DocuPulse account has been successfully updated.

If you didn't make these changes, please contact our support team immediately.

Best regards,
The DocuPulse Team

''' }, { 'name': 'Room Invite', 'subject': 'You\'ve Been Invited to a Room', 'body': '''

Room Invitation

Dear {{ user.username }},

{{ sender.username }} has invited you to join the room "{{ room_name }}".

Click the link below to view the room:

View Room

Best regards,
The DocuPulse Team

''' }, { 'name': 'Room Invite Removed', 'subject': 'Room Invitation Removed', 'body': '''

Room Invitation Removed

Dear {{ user.username }},

Your invitation to the room "{{ room_name }}" has been removed.

If you believe this is an error, please contact the room administrator.

Best regards,
The DocuPulse Team

''' }, { 'name': 'Conversation Invite', 'subject': 'You\'ve Been Invited to a Conversation', 'body': '''

Conversation Invitation

Dear {{ user.username }},

{{ sender.username }} has invited you to join a conversation.

Click the link below to view the conversation:

View Conversation

Best regards,
The DocuPulse Team

''' }, { 'name': 'Conversation Invite Removed', 'subject': 'Conversation Invitation Removed', 'body': '''

Conversation Invitation Removed

Dear {{ user.username }},

Your invitation to the conversation has been removed.

If you believe this is an error, please contact the conversation administrator.

Best regards,
The DocuPulse Team

''' }, { 'name': 'Conversation Message', 'subject': 'New Message in Conversation', 'body': '''

New Message

Dear {{ user.username }},

{{ sender.username }} has sent a new message in your conversation.

Click the link below to view the message:

View Message

Best regards,
The DocuPulse Team

''' } ] # Create templates if they don't exist for template_data in default_templates: existing_template = EmailTemplate.query.filter_by(name=template_data['name']).first() if not existing_template: template = EmailTemplate( name=template_data['name'], subject=template_data['subject'], body=template_data['body'], created_by=admin.id, is_active=True ) db.session.add(template) print(f"Created template: {template_data['name']}") else: print(f"Template already exists: {template_data['name']}") try: db.session.commit() print("All default templates have been created successfully!") except Exception as e: db.session.rollback() print(f"Error creating templates: {str(e)}")