U
    wh0                     @   sP   d Z ddlmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ G dd deZdS )	z
Channel Relationship Models

This module contains models that define relationships between channels
and other entities, particularly zone-specific configurations.

Models:
    - ChannelZoneRelation: Through model for Channel-Zone relationships
    )models)gettext_lazy)	BaseModel)ChannelChannelZoneChannelCodecc                	   @   s  e Zd ZdZdedfdedfdedfded	fgZejeej	d
ededdZ
ejeej	dededdZejeejddd
ededdZejdededdZejdededdZejddededdZejddededdZejdded ed!dZejd"ejddd#ed$ed%dZejd&ejddd#ed'ed(dZejdded)ed*dZejd+ded,ed-dZejdded.ed/dZejd0ed1ed2d3Zejd4dd5ed6ed7d8Zejded9ed:d3Z ejd;eded<ed=d>Z!ejdded?ed@dZ"ejd+dedAedBdZ#ejddedCedDdZ$ejdedEedFd3Z%ejdGedHedId3Z&G dJdK dKZ'dLdM Z(dNdO Z)dPdQ Z*dRdS Z+dTdU Z,dVdW Z-dXdY Z.dZS )[ChannelZoneRelationah  
    Through model for Channel-Zone Many-to-Many relationship.
    
    Contains zone-specific configuration for each channel, including
    custom streaming URLs, codec overrides, VPN settings, and FTP configurations.
    This allows channels to have different technical configurations
    for different geographic zones.
    
    Attributes:
        channel (Channel): The channel this configuration applies to
        zone (ChannelZone): The zone this configuration is for
        codec (ChannelCodec): Zone-specific codec override
        stream_url (str): Zone-specific streaming URL
        is_active (bool): Whether channel is active in this zone
        priority (int): Priority level for this zone configuration
        
    VPN Configuration:
        vpn_type (str): Type of VPN connection
        vpn_server_address (str): VPN server address
        vpn_username/password (str): VPN credentials
        
    IPSec Configuration:
        ipsec_preshared_key (str): IPSec pre-shared key
        ipsec_local_subnet/remote_subnet (str): Network subnets
        
    OpenVPN Configuration:
        openvpn_config_file (FileField): Configuration file
        openvpn_ca_cert/client_cert/client_key (str): Certificates
        
    WireGuard Configuration:
        wireguard_private_key/public_key (str): Cryptographic keys
        wireguard_endpoint (str): Server endpoint
        wireguard_allowed_ips (str): Allowed IP ranges
        
    FTP Configuration:
        ftp_host/username/password (str): FTP server details
        ftp_port (int): FTP server port
        ftp_root_directory (str): Root directory path
        ftp_use_passive (bool): Passive mode setting
        
    Relationships:
        - channel: Many-to-one with Channel
        - zone: Many-to-one with ChannelZone  
        - codec: Many-to-one with ChannelCodec (optional)
        
    Example:
        >>> relation = ChannelZoneRelation.objects.create(
        ...     channel=my_channel,
        ...     zone=europe_zone,
        ...     stream_url="https://eu-stream.example.com/channel1",
        ...     vpn_type="openvpn",
        ...     priority=10
        ... )
    nonezNo VPNipseczIPSec TunnelopenvpnZOpenVPN	wireguardZ	WireGuardzone_relationsr   z)The channel this configuration applies to)	on_deleterelated_nameverbose_name	help_textchannel_relationsZZonez-The geographic zone this configuration is forTzZone-specific Codecz*Override codec configuration for this zone)r   nullblankr   r   r   zZone-specific Stream URLz"Custom streaming URL for this zone)r   r   r   zZone-specific Backup Stream URLz$Secondary URL for failover streaming   z	Zone NamezEName of the zone for FTP configuration example: 2005, 2008, 2010, etc)
max_lengthr   r   r   zVerification NumberzDVerification number for FTP configuration example: 00001, 00002, etczPlatform NamezEName of the platform for FTP configuration example: CJI, TDF, 2M, etcStandaloneVPNConfigurationchannel_zone_relationszStandalone VPN ConfigurationzKReference to a standalone VPN configuration (overrides inline VPN settings)StandaloneFTPConfigurationzStandalone FTP ConfigurationzKReference to a standalone FTP configuration (overrides inline FTP settings)zFTP Hostz_FTP server hostname or IP address for content delivery (legacy - use standalone config instead)d   zFTP UsernamezUsername for FTP authenticationzFTP PasswordzPassword for FTP authentication   zFTP Portz$FTP server port number (default: 21))defaultr   r   i  /zFTP Root Directoryz%Root directory path on the FTP server)r   r   r   r   r   zUse FTP Passive Modez/Whether to use passive mode for FTP connections   zVPN Typez+Type of VPN connection for secure streaming)r   choicesr   r   r   zVPN Server Addressz!VPN server IP address or hostnamezVPN UsernamezUsername for VPN authenticationzVPN PasswordzPassword for VPN authenticationzActive in Zonez+Whether this channel is active in this zone   Priorityz>Priority level for this zone (higher number = higher priority)c                   @   sb   e Zd ZdZddgZdddgZedZedZe	j
ddgde	j
ddgde	j
ddd	gdgZd
S )zChannelZoneRelation.Metar   channelzonez	-priorityzChannel Zone RelationzChannel Zone Relations)fields	is_activepriorityN)__name__
__module____qualname__db_tableunique_togetherordering_r   verbose_name_pluralr   Indexindexes r1   r1   A/var/www/html/Focus/src/apps/channels/models/channel_relations.pyMeta   s   
r3   c                 C   s   | j j d| jj S )Nz in )r"   namer#   selfr1   r1   r2   __str__   s    zChannelZoneRelation.__str__c                 C   s   | j p
| jS )z
        Get the effective stream URL, falling back to channel default.
        
        Returns:
            str: Stream URL to use for this zone
        )
stream_urlbackup_stream_urlr5   r1   r1   r2   get_effective_stream_url   s    z,ChannelZoneRelation.get_effective_stream_urlc                 C   s   | j S )z
        Get the effective codec, falling back to channel default.
        
        Returns:
            ChannelCodec: Codec configuration to use for this zone
        )codecr5   r1   r1   r2   get_effective_codec  s    z'ChannelZoneRelation.get_effective_codecc                 C   s:   | j | j| j| j| j| j| jd| j| j| j	| j
ddS )z
        Get comprehensive connection information for this zone.
        
        Returns:
            dict: Connection information including stream, FTP, and VPN details
        )hostusernamepasswordportroot_directoryZuse_passive)typeserver_addressr>   r?   )r8   ftpZvpn)r8   ftp_hostftp_usernameftp_passwordftp_portftp_root_directoryftp_use_passivevpn_typevpn_server_addressvpn_usernamevpn_passwordr5   r1   r1   r2   get_connection_info  s    	z'ChannelZoneRelation.get_connection_infoc                 C   s^   | j r| j S | jdkr&t| dr&| jS | jdkr@t| dr@| jS | jdkrZt| drZ| jS dS )z
        Get the active VPN configuration, prioritizing standalone config over inline config.
        Returns the appropriate VPN configuration object or None.
        r
   ipsecconfig_configr   openvpnconfig_configr   wireguardconfig_configN)standalone_vpn_configrK   hasattrrP   rQ   rR   r5   r1   r1   r2   get_active_vpn_configuration&  s    z0ChannelZoneRelation.get_active_vpn_configurationc              	   C   sh   | j r<| j j| j j| j j| j j| j j| j j| j j| j jdS | j	rd| j	| j
| j| j| j| jdddS dS )z
        Get the active FTP configuration, prioritizing standalone config over inline config.
        Returns a dict with FTP configuration or None.
        )r=   r>   r?   r@   rA   passive_modessl_enabledtimeoutF   N)standalone_ftp_configr=   r>   r?   r@   rA   rV   rW   rX   rE   rF   rG   rH   rI   rJ   r5   r1   r1   r2   get_active_ftp_configuration8  s*    
z0ChannelZoneRelation.get_active_ftp_configurationc                 C   s   t | jo| jS )z
        Check if FTP configuration is complete.
        
        Returns:
            bool: True if all required FTP fields are configured
        )boolrE   rF   r5   r1   r1   r2   has_ftp_configurationX  s    z)ChannelZoneRelation.has_ftp_configurationN)/r'   r(   r)   __doc__r-   Z	VPN_TYPESr   
ForeignKeyr   CASCADEr"   r   r#   r   SET_NULLr;   URLFieldr8   r9   	CharFieldZftp_zone_nameZftp_verife_numberZftp_platform_namerS   rZ   rE   rF   rG   PositiveIntegerFieldrH   rI   BooleanFieldrJ   rK   rL   rM   rN   r%   r&   r3   r7   r:   r<   rO   rU   r[   r]   r1   r1   r1   r2   r      s2  8



				 r   N)r^   	django.dbr   django.utils.translationr   r-   apps.common.modelsr   apps.channels.models.channelsr   r   r   r   r1   r1   r1   r2   <module>   s
   
