nordabiz/blueprints/__init__.py
Maciej Pienczyn 55718ed211 refactor: Extract chat blueprint (Phase 5)
- Create blueprints/chat/ with 9 routes:
  - chat, chat_settings, chat_start, chat_send_message
  - chat_get_history, chat_list_conversations, chat_delete_conversation
  - chat_feedback, chat_analytics
- Register chat blueprint with backward-compatible aliases
- Remove dead code from app.py (-458 lines)
- app.py: 13,058 → 12,600 lines (-3.5%)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-31 07:54:08 +01:00

231 lines
9.3 KiB
Python

"""
Blueprints Package
==================
Central registration of all Flask blueprints.
"""
import logging
logger = logging.getLogger(__name__)
def register_blueprints(app):
"""
Register all blueprints with the Flask application.
Args:
app: Flask application instance
"""
# Phase 1: Low-risk modules
# Reports blueprint
try:
from blueprints.reports import bp as reports_bp
app.register_blueprint(reports_bp)
logger.info("Registered blueprint: reports")
except ImportError as e:
logger.debug(f"Blueprint reports not yet available: {e}")
# Community blueprints - register directly (not nested)
# to preserve endpoint names like 'calendar_index' instead of 'community.calendar.calendar_index'
try:
from blueprints.community.contacts import bp as contacts_bp
app.register_blueprint(contacts_bp)
logger.info("Registered blueprint: contacts")
except ImportError as e:
logger.debug(f"Blueprint contacts not yet available: {e}")
try:
from blueprints.community.classifieds import bp as classifieds_bp
app.register_blueprint(classifieds_bp)
logger.info("Registered blueprint: classifieds")
except ImportError as e:
logger.debug(f"Blueprint classifieds not yet available: {e}")
try:
from blueprints.community.calendar import bp as calendar_bp
app.register_blueprint(calendar_bp)
logger.info("Registered blueprint: calendar")
except ImportError as e:
logger.debug(f"Blueprint calendar not yet available: {e}")
# Education blueprint
try:
from blueprints.education import bp as education_bp
app.register_blueprint(education_bp)
logger.info("Registered blueprint: education")
except ImportError as e:
logger.debug(f"Blueprint education not yet available: {e}")
# Phase 2: Auth + Public blueprints (with backward-compatible aliases)
try:
from blueprints.auth import bp as auth_bp
app.register_blueprint(auth_bp)
logger.info("Registered blueprint: auth")
# Create aliases for backward compatibility
# Old url_for('login') will still work alongside url_for('auth.login')
_create_endpoint_aliases(app, auth_bp, {
'register': 'auth.register',
'login': 'auth.login',
'logout': 'auth.logout',
'verify_2fa': 'auth.verify_2fa',
'settings_2fa': 'auth.settings_2fa',
'settings_privacy': 'auth.settings_privacy',
'settings_blocks': 'auth.settings_blocks',
'settings_blocks_add': 'auth.settings_blocks_add',
'settings_blocks_remove': 'auth.settings_blocks_remove',
'forgot_password': 'auth.forgot_password',
'reset_password': 'auth.reset_password',
'verify_email': 'auth.verify_email',
'resend_verification': 'auth.resend_verification',
# Account routes (konto)
'konto_dane': 'auth.konto_dane',
'konto_dane_save': 'auth.konto_dane_post',
'konto_prywatnosc': 'auth.konto_prywatnosc',
'konto_bezpieczenstwo': 'auth.konto_bezpieczenstwo',
'konto_blokady': 'auth.konto_blokady',
'konto_blokady_dodaj': 'auth.konto_blokady_dodaj',
'konto_blokady_usun': 'auth.konto_blokady_usun',
})
logger.info("Created auth endpoint aliases")
except ImportError as e:
logger.debug(f"Blueprint auth not yet available: {e}")
except Exception as e:
logger.error(f"Error registering auth blueprint: {e}")
try:
from blueprints.public import bp as public_bp
app.register_blueprint(public_bp)
logger.info("Registered blueprint: public")
# Create aliases for backward compatibility
_create_endpoint_aliases(app, public_bp, {
'index': 'public.index',
'company_detail': 'public.company_detail',
'company_detail_by_slug': 'public.company_detail_by_slug',
'person_detail': 'public.person_detail',
'company_recommend': 'public.company_recommend',
'search': 'public.search',
'events': 'public.events',
'new_members': 'public.new_members',
'connections_map': 'public.connections_map',
'dashboard': 'public.dashboard',
'release_notes': 'public.release_notes',
})
logger.info("Created public endpoint aliases")
except ImportError as e:
logger.debug(f"Blueprint public not yet available: {e}")
except Exception as e:
logger.error(f"Error registering public blueprint: {e}")
# Phase 3: Forum blueprint
try:
from blueprints.forum import bp as forum_bp
app.register_blueprint(forum_bp)
logger.info("Registered blueprint: forum")
# Create aliases for backward compatibility
_create_endpoint_aliases(app, forum_bp, {
'forum_index': 'forum.forum_index',
'forum_new_topic': 'forum.forum_new_topic',
'forum_topic': 'forum.forum_topic',
'forum_reply': 'forum.forum_reply',
'admin_forum': 'forum.admin_forum',
'admin_forum_pin': 'forum.admin_forum_pin',
'admin_forum_lock': 'forum.admin_forum_lock',
'admin_forum_delete_topic': 'forum.admin_forum_delete_topic',
'admin_forum_delete_reply': 'forum.admin_forum_delete_reply',
'admin_forum_change_status': 'forum.admin_forum_change_status',
})
logger.info("Created forum endpoint aliases")
except ImportError as e:
logger.debug(f"Blueprint forum not yet available: {e}")
except Exception as e:
logger.error(f"Error registering forum blueprint: {e}")
# Phase 4: Messages + Notifications blueprint
try:
from blueprints.messages import bp as messages_bp
app.register_blueprint(messages_bp)
logger.info("Registered blueprint: messages")
# Create aliases for backward compatibility
_create_endpoint_aliases(app, messages_bp, {
'messages_inbox': 'messages.messages_inbox',
'messages_sent': 'messages.messages_sent',
'messages_new': 'messages.messages_new',
'messages_send': 'messages.messages_send',
'messages_view': 'messages.messages_view',
'messages_reply': 'messages.messages_reply',
'api_unread_count': 'messages.api_unread_count',
'api_notifications': 'messages.api_notifications',
'api_notification_mark_read': 'messages.api_notification_mark_read',
'api_notifications_mark_all_read': 'messages.api_notifications_mark_all_read',
'api_notifications_unread_count': 'messages.api_notifications_unread_count',
})
logger.info("Created messages endpoint aliases")
except ImportError as e:
logger.debug(f"Blueprint messages not yet available: {e}")
except Exception as e:
logger.error(f"Error registering messages blueprint: {e}")
# Phase 5: Chat blueprint
try:
from blueprints.chat import bp as chat_bp
app.register_blueprint(chat_bp)
logger.info("Registered blueprint: chat")
# Create aliases for backward compatibility
_create_endpoint_aliases(app, chat_bp, {
'chat': 'chat.chat',
'chat_settings': 'chat.chat_settings',
'chat_start': 'chat.chat_start',
'chat_send_message': 'chat.chat_send_message',
'chat_get_history': 'chat.chat_get_history',
'chat_list_conversations': 'chat.chat_list_conversations',
'chat_delete_conversation': 'chat.chat_delete_conversation',
'chat_feedback': 'chat.chat_feedback',
'chat_analytics': 'chat.chat_analytics',
})
logger.info("Created chat endpoint aliases")
except ImportError as e:
logger.debug(f"Blueprint chat not yet available: {e}")
except Exception as e:
logger.error(f"Error registering chat blueprint: {e}")
# Phase 6-10: Future blueprints will be added here
def _create_endpoint_aliases(app, blueprint, aliases):
"""
Create backward-compatible endpoint aliases.
This allows old code using url_for('login') to work alongside
new code using url_for('auth.login').
Args:
app: Flask application instance
blueprint: The blueprint that was just registered
aliases: Dict mapping old_name -> new_name (blueprint.endpoint)
"""
for old_name, new_name in aliases.items():
if new_name in app.view_functions:
# Find the URL rule for the new endpoint
for rule in app.url_map.iter_rules():
if rule.endpoint == new_name:
try:
# Register the same view function under the old name
app.add_url_rule(
rule.rule,
old_name,
app.view_functions[new_name],
methods=list(rule.methods - {'OPTIONS', 'HEAD'})
)
logger.debug(f"Created alias: {old_name} -> {new_name}")
except AssertionError:
# Endpoint already exists (e.g., still in app.py)
logger.debug(f"Alias {old_name} already exists, skipping")
break