<s> """<STR_LIT>""" <EOL> from django . db import models <EOL> from django . db . models . manager import Manager <EOL> from django . utils . translation import ugettext as _ , ugettext_lazy <EOL> from reversion import revisions as reversion <EOL> from modoboa . admin import models as admin_models <EOL> from modoboa . lib import parameters <EOL> from modoboa . lib . exceptions import BadRequest <EOL> class ServiceManager ( Manager ) : <EOL> def load_from_master_cf ( self ) : <EOL> """<STR_LIT>""" <EOL> with open ( parameters . get_admin ( '<STR_LIT>' ) ) as fp : <EOL> content = fp . read ( ) <EOL> services = [ ] <EOL> for line in content . split ( '<STR_LIT:\n>' ) : <EOL> if not line or line . startswith ( '<STR_LIT:#>' ) : <EOL> continue <EOL> parts = line . strip ( ) . split ( ) <EOL> if len ( parts ) != <NUM_LIT:8> : <EOL> continue <EOL> if parts [ <NUM_LIT:7> ] != '<STR_LIT>' : <EOL> continue <EOL> srv , created = self . get_or_create ( name = parts [ <NUM_LIT:0> ] ) <EOL> services . append ( parts [ <NUM_LIT:0> ] ) <EOL> to_delete = [ ] <EOL> for service in self . all ( ) : <EOL> if service . name not in services : <EOL> to_delete . append ( service . name ) <EOL> Service . objects . filter ( name__in = to_delete ) . delete ( ) <EOL> class Service ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( <EOL> ugettext_lazy ( '<STR_LIT:name>' ) , max_length = <NUM_LIT:100> , unique = True , <EOL> help_text = ugettext_lazy ( '<STR_LIT>' ) <EOL> ) <EOL> objects = ServiceManager ( ) <EOL> class Meta : <EOL> db_table = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> class RelayDomain ( admin_models . AdminObject ) : <EOL> """<STR_LIT>""" <EOL> domain = models . OneToOneField ( "<STR_LIT>" , null = True ) <EOL> target_host = models . CharField ( <EOL> ugettext_lazy ( '<STR_LIT>' ) , max_length = <NUM_LIT:255> , <EOL> help_text = ugettext_lazy ( '<STR_LIT>' ) <EOL> ) <EOL> service = models . ForeignKey ( Service ) <EOL> verify_recipients = models . BooleanField ( <EOL> ugettext_lazy ( '<STR_LIT>' ) , <EOL> help_text = ugettext_lazy ( '<STR_LIT>' ) , <EOL> default = False <EOL> ) <EOL> class Meta : <EOL> ordering = [ "<STR_LIT>" ] <EOL> db_table = "<STR_LIT>" <EOL> @ property <EOL> def tags ( self ) : <EOL> return [ <EOL> { "<STR_LIT:name>" : "<STR_LIT>" , "<STR_LIT:label>" : _ ( "<STR_LIT>" ) , "<STR_LIT:type>" : "<STR_LIT>" } , <EOL> { "<STR_LIT:name>" : self . service . name , "<STR_LIT:label>" : "<STR_LIT>" % self . service . name , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT:info>" } <EOL> ] <EOL> def __str__ ( self ) : <EOL> return self . domain . name <EOL> def to_csv ( self , csvwriter ) : <EOL> """<STR_LIT>""" <EOL> csvwriter . writerow ( <EOL> [ "<STR_LIT>" , self . domain . name , self . target_host , <EOL> self . service . name , self . domain . enabled , self . verify_recipients ] <EOL> ) <EOL> for dalias in self . domain . domainalias_set . all ( ) : <EOL> dalias . to_csv ( csvwriter ) <EOL> def from_csv ( self , user , row ) : <EOL> """<STR_LIT>""" <EOL> if len ( row ) != <NUM_LIT:6> : <EOL> raise BadRequest ( _ ( "<STR_LIT>" ) ) <EOL> self . domain = admin_models . Domain ( <EOL> name = row [ <NUM_LIT:1> ] . strip ( ) , type = "<STR_LIT>" , <EOL> enabled = ( row [ <NUM_LIT:4> ] . strip ( ) in [ "<STR_LIT:True>" , "<STR_LIT:1>" , "<STR_LIT:yes>" , "<STR_LIT:y>" ] ) <EOL> ) <EOL> self . domain . save ( creator = user ) <EOL> self . target_host = row [ <NUM_LIT:2> ] . strip ( ) <EOL> self . service , created = Service . objects . get_or_create ( <EOL> name = row [ <NUM_LIT:3> ] . strip ( ) ) <EOL> self . verify_recipients = ( row [ <NUM_LIT:5> ] . strip ( ) in [ "<STR_LIT:True>" , "<STR_LIT:1>" , "<STR_LIT:yes>" , "<STR_LIT:y>" ] ) <EOL> self . save ( creator = user ) <EOL> reversion . register ( RelayDomain ) </s>
