<s> """<STR_LIT>""" <EOL> from django . db . models import signals <EOL> from django . dispatch import receiver <EOL> from modoboa . admin import models as admin_models <EOL> from modoboa . admin . signals import use_external_recipients <EOL> from modoboa . lib . email_utils import split_mailbox <EOL> from . import models <EOL> @ receiver ( use_external_recipients ) <EOL> def check_relaydomain_alias ( sender , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> recipient = kwargs . get ( "<STR_LIT>" ) <EOL> if not recipient : <EOL> return <EOL> localpart , domain = split_mailbox ( recipient ) <EOL> if not ( models . RelayDomain . objects . select_related ( ) . filter ( <EOL> domain__name = domain ) . exists ( ) ) : <EOL> return False <EOL> if ( admin_models . Mailbox . objects . select_related ( "<STR_LIT>" ) . filter ( <EOL> domain__name = domain , address = localpart ) . exists ( ) ) : <EOL> return False <EOL> return True <EOL> @ receiver ( signals . post_save , sender = admin_models . Domain ) <EOL> def clean_domain ( sender , instance , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs . get ( "<STR_LIT>" ) : <EOL> return <EOL> has_relaydom = hasattr ( instance , "<STR_LIT>" ) <EOL> if instance . type == "<STR_LIT>" and has_relaydom : <EOL> models . RelayDomain . objects . filter ( domain = instance ) . delete ( ) <EOL> elif instance . type == "<STR_LIT>" and not has_relaydom : <EOL> models . RelayDomain . objects . create ( <EOL> domain = instance , service = models . Service . objects . first ( ) ) </s>
