Tour operator signals
Tour operator signals (English fallback)
Aug. 17, 2025
Posted by admin
Nhom |
Notes |
|
I'll explain the tour operators signals.py file in detail, including its purpose, functionality, and how it maintains data consistency across the system. Tour Operators Signals System - Detailed Analysis File Overview Purpose: Automatic data consistency maintenance and statistics updates Location: signals.py Pattern: Django signals with receivers for real-time data synchronization Imports and Dependencies from django.db.models.signals import post_save, post_delete from django.dispatch import receiver from django.db.models import Avg, Count from .models import TourBooking, Tour, TourOperator Signal Types Used:
Models Involved:
|
|
Signal Handlers Detailed Analysis
|
|
1. Booking Statistics Handler (post_save) @receiver(post_save, sender=TourBooking) def update_tour_booking_stats(sender, instance, created, **kwargs): """Update tour booking statistics when a booking is created or updated""" tour = instance.tour
# Update total bookings count tour.total_bookings = TourBooking.objects.filter( tour=tour, booking_status__in=['confirmed', 'paid', 'completed'] ).count()
tour.save(update_fields=['total_bookings'])
# Update tour operator statistics operator = tour.tour_operator operator.total_tours = Tour.objects.filter(tour_operator=operator, is_active=True).count() operator.save(update_fields=['total_tours']) Trigger Conditions:
Functionality Breakdown:
Business Impact:
|
|
2. Booking Deletion Handler (post_delete) @receiver(post_delete, sender=TourBooking) def update_tour_booking_stats_on_delete(sender, instance, **kwargs): """Update tour booking statistics when a booking is deleted""" tour = instance.tour
# Update total bookings count tour.total_bookings = TourBooking.objects.filter( tour=tour, booking_status__in=['confirmed', 'paid', 'completed'] ).count()
tour.save(update_fields=['total_bookings']) Trigger Conditions:
Functionality:
Use Cases:
|
|
3. Tour Count Handler (post_save) @receiver(post_save, sender=Tour) def update_operator_tour_count(sender, instance, created, **kwargs): """Update tour operator's total tours count when a tour is created/updated""" operator = instance.tour_operator operator.total_tours = Tour.objects.filter(tour_operator=operator, is_active=True).count() operator.save(update_fields=['total_tours']) Trigger Conditions:
Functionality:
Business Scenarios:
|
|
4. Tour Deletion Handler (post_delete) @receiver(post_delete, sender=Tour) def update_operator_tour_count_on_delete(sender, instance, **kwargs): """Update tour operator's total tours count when a tour is deleted""" operator = instance.tour_operator operator.total_tours = Tour.objects.filter(tour_operator=operator, is_active=True).count() operator.save(update_fields=['total_tours']) Trigger Conditions:
Functionality:
Signal System Architecture Benefits Data Consistency
Performance Optimization
Business Intelligence
|
|
Data Flow Example Scenario: Customer books a tour
TourBooking.objects.create( tour=tour_instance, booking_status='confirmed', customer=user )
Error Handling Considerations Potential Issues:
Current Protections:
Integration with Business Logic Template Usage:
Admin Interface:
API Endpoints:
|
|
Recommendations for Enhancement
import logging logger = logging.getLogger(__name__) try: tour.save(update_fields=['total_bookings']) except Exception as e: logger.error(f"Failed to update tour stats: {e}")
# Add to booking stats handler tour.average_rating = TourBooking.objects.filter( tour=tour, rating__isnull=False ).aggregate(avg_rating=Avg('rating'))['avg_rating'] or 0
This signals system provides robust, automatic data consistency maintenance that's essential for a professional tour booking platform, ensuring accurate statistics and seamless user experience
|
Nhom |
Notes |
|
I'll explain the tour operators signals.py file in detail, including its purpose, functionality, and how it maintains data consistency across the system. Tour Operators Signals System - Detailed Analysis File Overview Purpose: Automatic data consistency maintenance and statistics updates Location: signals.py Pattern: Django signals with receivers for real-time data synchronization Imports and Dependencies from django.db.models.signals import post_save, post_delete from django.dispatch import receiver from django.db.models import Avg, Count from .models import TourBooking, Tour, TourOperator Signal Types Used:
Models Involved:
|
|
Signal Handlers Detailed Analysis
|
|
1. Booking Statistics Handler (post_save) @receiver(post_save, sender=TourBooking) def update_tour_booking_stats(sender, instance, created, **kwargs): """Update tour booking statistics when a booking is created or updated""" tour = instance.tour
# Update total bookings count tour.total_bookings = TourBooking.objects.filter( tour=tour, booking_status__in=['confirmed', 'paid', 'completed'] ).count()
tour.save(update_fields=['total_bookings'])
# Update tour operator statistics operator = tour.tour_operator operator.total_tours = Tour.objects.filter(tour_operator=operator, is_active=True).count() operator.save(update_fields=['total_tours']) Trigger Conditions:
Functionality Breakdown:
Business Impact:
|
|
2. Booking Deletion Handler (post_delete) @receiver(post_delete, sender=TourBooking) def update_tour_booking_stats_on_delete(sender, instance, **kwargs): """Update tour booking statistics when a booking is deleted""" tour = instance.tour
# Update total bookings count tour.total_bookings = TourBooking.objects.filter( tour=tour, booking_status__in=['confirmed', 'paid', 'completed'] ).count()
tour.save(update_fields=['total_bookings']) Trigger Conditions:
Functionality:
Use Cases:
|
|
3. Tour Count Handler (post_save) @receiver(post_save, sender=Tour) def update_operator_tour_count(sender, instance, created, **kwargs): """Update tour operator's total tours count when a tour is created/updated""" operator = instance.tour_operator operator.total_tours = Tour.objects.filter(tour_operator=operator, is_active=True).count() operator.save(update_fields=['total_tours']) Trigger Conditions:
Functionality:
Business Scenarios:
|
|
4. Tour Deletion Handler (post_delete) @receiver(post_delete, sender=Tour) def update_operator_tour_count_on_delete(sender, instance, **kwargs): """Update tour operator's total tours count when a tour is deleted""" operator = instance.tour_operator operator.total_tours = Tour.objects.filter(tour_operator=operator, is_active=True).count() operator.save(update_fields=['total_tours']) Trigger Conditions:
Functionality:
Signal System Architecture Benefits Data Consistency
Performance Optimization
Business Intelligence
|
|
Data Flow Example Scenario: Customer books a tour
TourBooking.objects.create( tour=tour_instance, booking_status='confirmed', customer=user )
Error Handling Considerations Potential Issues:
Current Protections:
Integration with Business Logic Template Usage:
Admin Interface:
API Endpoints:
|
|
Recommendations for Enhancement
import logging logger = logging.getLogger(__name__) try: tour.save(update_fields=['total_bookings']) except Exception as e: logger.error(f"Failed to update tour stats: {e}")
# Add to booking stats handler tour.average_rating = TourBooking.objects.filter( tour=tour, rating__isnull=False ).aggregate(avg_rating=Avg('rating'))['avg_rating'] or 0
This signals system provides robust, automatic data consistency maintenance that's essential for a professional tour booking platform, ensuring accurate statistics and seamless user experience
|
Attached Files
You are viewing this article in public mode. Some features may be limited.