U
    hh9                     @   sR   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 e
eZdd ZdS )	a  
Django Signals Module for European Channel Zones Creation

This module contains Django signals that automatically create default European
channel zones after database migrations. It ensures that all necessary
broadcasting regions (France, Spain, Netherlands, Germany, Portugal) are
available immediately after deployment or migration runs.

Author: Development Team
Created: 2025
Purpose: Automated setup of European broadcasting zones for channel management
    N)post_migrate)receiver)apps)settingsc                 K   sJ  | j dkrdS ttdr,tjr,td dS |drJ|dj dkrJdS ztdd}dd	d
dddddddddddddddddddddddddddg}d}d}|D ]$}z|j	j
|d |d \}}|r|d!7 }td"|j  d#|j d$ nd%}	| D ]4\}
}|
dkrt||
|krt||
| d}	q|	rp|  |d!7 }td&|j  d#|j d$ ntd'|j  d#|j d$ W q tk
r } z&td(|d  d)|  W Y qW 5 d}~X Y qX q|dks|dkrtd*| d+|  n
td, W n4 tk
rD } ztd-|  W 5 d}~X Y nX dS ).u  
    Django signal handler to create default European channel zones after migrations.
    
    This function is automatically triggered after Django runs any migration.
    It ensures that the 5 required European broadcasting zones are always
    available in the database without manual intervention.
    
    Signal: post_migrate
    Trigger: After any Django migration completes
    Scope: Only runs for the specific app containing ChannelZone model
    
    Args:
        sender: The AppConfig instance of the app that just ran migrations
        **kwargs: Additional signal arguments (app_config, verbosity, etc.)
        
    Returns:
        None: This function performs database operations and logging only
        
    Zones Created:
        - France (FR) - Europe/Paris timezone
        - Spain (ES) - Europe/Madrid timezone  
        - Netherlands (NL) - Europe/Amsterdam timezone
        - Germany (DE) - Europe/Berlin timezone
        - Portugal (PT) - Europe/Lisbon timezone
        
    Example:
        This function runs automatically, but the equivalent manual operation would be:
        >>> ChannelZone.objects.get_or_create(
        ...     code='FR',
        ...     defaults={
        ...         'name': 'France',
        ...         'description': 'France - République française',
        ...         'timezone': 'Europe/Paris',
        ...         'is_active': True
        ...     }
        ... )
    zapps.channelsNTESTINGz#Skipping zone creation during tests
app_configchannelsChannelZoneZFranceZFRu   France - République françaisezEurope/ParisT)namecodedescriptiontimezone	is_activeZSpainZESu   Spain - Reino de EspañazEurope/MadridZNetherlandsNLzNetherlands - NederlandzEurope/AmsterdamZGermanyZDEz$Germany - Bundesrepublik DeutschlandzEurope/BerlinPortugalZPTu    Portugal - República PortuguesazEurope/Lisbonr   r   )r   defaults   zCreated zone: z ()FzUpdated zone: zZone unchanged: zError processing zone z: z)European zones setup complete - Created: z, Updated: z3All European zones already exist and are up to datez)Error setting up European channel zones: )r
   hasattrr   r   loggerinfogetr   	get_modelobjectsget_or_creater   itemsgetattrsetattrsavedebug	Exceptionerror)senderkwargsr	   Zdefault_zonesZcreated_countZupdated_countZ	zone_datazonecreatedupdatedkeyvalueZ
zone_errore r*   0/var/www/html/Focus/src/apps/channels/signals.pycreate_default_channel_zones   s    *





-


 r,   )__doc__loggingZdjango.db.models.signalsr   django.dispatchr   django.appsr   django.confr   	getLogger__name__r   r,   r*   r*   r*   r+   <module>   s   
 K