diff --git a/__pycache__/app.cpython-313.pyc b/__pycache__/app.cpython-313.pyc index af63a51..fcbc680 100644 Binary files a/__pycache__/app.cpython-313.pyc and b/__pycache__/app.cpython-313.pyc differ diff --git a/__pycache__/create_default_templates.cpython-313.pyc b/__pycache__/create_default_templates.cpython-313.pyc new file mode 100644 index 0000000..c92cfcf Binary files /dev/null and b/__pycache__/create_default_templates.cpython-313.pyc differ diff --git a/app.py b/app.py index e9d58fa..14980b1 100644 --- a/app.py +++ b/app.py @@ -13,6 +13,7 @@ from tasks import cleanup_trash import click from utils import timeago from extensions import db, login_manager, csrf +from utils.email_templates import create_default_templates # Load environment variables load_dotenv() @@ -85,6 +86,10 @@ def create_app(): # Register custom filters app.jinja_env.filters['timeago'] = timeago + # Create default email templates if they don't exist + with app.app_context(): + create_default_templates() + return app app = create_app() diff --git a/routes/__init__.py b/routes/__init__.py index f9eea3f..539d15b 100644 --- a/routes/__init__.py +++ b/routes/__init__.py @@ -15,6 +15,7 @@ def init_app(app: Flask): from .rooms import rooms_bp as rooms_routes from .conversations import conversations_bp as conversations_routes from .admin import admin as admin_routes + from .email_templates import email_templates as email_templates_routes # Initialize routes init_main_routes(main_bp) @@ -33,6 +34,7 @@ def init_app(app: Flask): app.register_blueprint(contacts_routes) app.register_blueprint(conversations_routes) app.register_blueprint(admin_routes) + app.register_blueprint(email_templates_routes) @app.route('/rooms//trash') @login_required diff --git a/routes/__pycache__/__init__.cpython-313.pyc b/routes/__pycache__/__init__.cpython-313.pyc index 64feb4a..7c496fc 100644 Binary files a/routes/__pycache__/__init__.cpython-313.pyc and b/routes/__pycache__/__init__.cpython-313.pyc differ diff --git a/routes/__pycache__/email_templates.cpython-313.pyc b/routes/__pycache__/email_templates.cpython-313.pyc new file mode 100644 index 0000000..a6ccd06 Binary files /dev/null and b/routes/__pycache__/email_templates.cpython-313.pyc differ diff --git a/routes/__pycache__/main.cpython-313.pyc b/routes/__pycache__/main.cpython-313.pyc index 82be012..9c60ea4 100644 Binary files a/routes/__pycache__/main.cpython-313.pyc and b/routes/__pycache__/main.cpython-313.pyc differ diff --git a/routes/main.py b/routes/main.py index 32575f3..bf8534d 100644 --- a/routes/main.py +++ b/routes/main.py @@ -651,9 +651,7 @@ def init_routes(main_bp): users = {user.id: user for user in User.query.filter(User.id.in_(user_ids)).all()} # Get email templates for the email templates tab - email_templates = [] - if active_tab == 'email_templates': - email_templates = EmailTemplate.query.filter_by(is_active=True).all() + email_templates = EmailTemplate.query.filter_by(is_active=True).all() if request.method == 'GET': company_form.company_name.data = site_settings.company_name diff --git a/utils/__init__.py b/utils/__init__.py index abae6e6..a80b752 100644 --- a/utils/__init__.py +++ b/utils/__init__.py @@ -4,6 +4,18 @@ from .event_logger import log_event, get_user_events, get_room_events, get_recen from .notification import create_notification, get_user_notifications, mark_notification_read, mark_all_notifications_read, get_unread_count, delete_notification, delete_old_notifications from .path_utils import clean_path, secure_file_path from .time_utils import timeago, format_datetime, parse_datetime +from functools import wraps +from flask import flash, redirect, url_for +from flask_login import current_user + +def admin_required(f): + @wraps(f) + def decorated_function(*args, **kwargs): + if not current_user.is_authenticated or not current_user.is_admin: + flash('You do not have permission to access this page.', 'error') + return redirect(url_for('main.dashboard')) + return f(*args, **kwargs) + return decorated_function __all__ = [ 'user_has_permission', diff --git a/utils/__pycache__/__init__.cpython-313.pyc b/utils/__pycache__/__init__.cpython-313.pyc index 7b7779a..0005d16 100644 Binary files a/utils/__pycache__/__init__.cpython-313.pyc and b/utils/__pycache__/__init__.cpython-313.pyc differ diff --git a/utils/__pycache__/email_templates.cpython-313.pyc b/utils/__pycache__/email_templates.cpython-313.pyc new file mode 100644 index 0000000..8f5e9ae Binary files /dev/null and b/utils/__pycache__/email_templates.cpython-313.pyc differ diff --git a/utils/email_templates.py b/utils/email_templates.py new file mode 100644 index 0000000..ae02547 --- /dev/null +++ b/utils/email_templates.py @@ -0,0 +1,139 @@ +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)}") \ No newline at end of file