<s> from django . db import models <EOL> from django . core . exceptions import ValidationError , ObjectDoesNotExist <EOL> from mozdns . domain . models import Domain <EOL> from mozdns . address_record . models import AddressRecord <EOL> from mozdns . validation import validate_label , validate_name <EOL> from mozdns . models import MozdnsRecord <EOL> from mozdns . view . models import View <EOL> from core . registration . static . models import StaticReg <EOL> import reversion <EOL> from string import Template <EOL> class Nameserver ( MozdnsRecord ) : <EOL> """<STR_LIT>""" <EOL> id = models . AutoField ( primary_key = True ) <EOL> domain = models . ForeignKey ( <EOL> Domain , null = False , help_text = "<STR_LIT>" <EOL> ) <EOL> server = models . CharField ( <EOL> max_length = <NUM_LIT:255> , validators = [ validate_name ] , help_text = "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> addr_glue = models . ForeignKey ( AddressRecord , null = True , blank = True , <EOL> related_name = "<STR_LIT>" ) <EOL> sreg_glue = models . ForeignKey ( StaticReg , null = True , blank = True , <EOL> related_name = "<STR_LIT>" ) <EOL> template = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> search_fields = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return self . bind_render_record ( ) <EOL> class Meta : <EOL> db_table = "<STR_LIT>" <EOL> unique_together = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> @ classmethod <EOL> def get_api_fields ( cls ) : <EOL> return [ '<STR_LIT>' , '<STR_LIT:description>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> @ property <EOL> def rdtype ( self ) : <EOL> return '<STR_LIT>' <EOL> def bind_render_record ( self , pk = False , show_ttl = False , ** kwargs ) : <EOL> if show_ttl : <EOL> ttl_ = self . ttl <EOL> else : <EOL> ttl_ = '<STR_LIT>' if self . ttl is None else self . ttl <EOL> template = Template ( self . template ) . substitute ( ** self . justs ) <EOL> return template . format ( <EOL> rdtype = self . rdtype , rdclass = '<STR_LIT>' , bind_name = self . domain . name + '<STR_LIT:.>' , <EOL> ttl_ = ttl_ , ** vars ( self ) <EOL> ) <EOL> def details ( self ) : <EOL> return ( <EOL> ( "<STR_LIT>" , self . server ) , <EOL> ( "<STR_LIT>" , self . domain . name ) , <EOL> ( "<STR_LIT>" , self . get_glue ( ) ) , <EOL> ) <EOL> def save ( self , * args , ** kwargs ) : <EOL> no_build = kwargs . pop ( '<STR_LIT>' , False ) <EOL> super ( Nameserver , self ) . save ( * args , ** kwargs ) <EOL> if not no_build : <EOL> if self . domain . soa : <EOL> self . domain . soa . schedule_full_rebuild ( ) <EOL> def get_glue ( self ) : <EOL> if self . addr_glue : <EOL> return self . addr_glue <EOL> elif self . sreg_glue : <EOL> return self . sreg_glue <EOL> else : <EOL> return None <EOL> def set_glue ( self , glue ) : <EOL> if isinstance ( glue , AddressRecord ) : <EOL> self . addr_glue = glue <EOL> self . sreg_glue = None <EOL> elif isinstance ( glue , StaticReg ) : <EOL> self . addr_glue = None <EOL> self . sreg_glue = glue <EOL> elif isinstance ( glue , type ( None ) ) : <EOL> self . addr_glue = None <EOL> self . sreg_glue = None <EOL> else : <EOL> raise ValueError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( glue ) <EOL> ) <EOL> def del_glue ( self ) : <EOL> if self . addr_glue : <EOL> self . addr_glue . delete ( ) <EOL> elif self . sreg_glue : <EOL> self . sreg_glue . delete ( ) <EOL> else : <EOL> raise AttributeError ( "<STR_LIT>" ) <EOL> glue = property ( get_glue , set_glue , del_glue , "<STR_LIT>" ) <EOL> def delete ( self , * args , ** kwargs ) : <EOL> no_build = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if self . domain . soa : <EOL> soa = self . domain . soa <EOL> else : <EOL> soa = None <EOL> self . check_no_ns_soa_condition ( self . domain ) <EOL> super ( Nameserver , self ) . delete ( * args , ** kwargs ) <EOL> if not no_build and soa : <EOL> soa . schedule_full_rebuild ( ) <EOL> def clean ( self ) : <EOL> self . check_for_cname ( ) <EOL> self . check_for_illegal_rr_ttl ( <EOL> field_name = '<STR_LIT>' , rr_value = self . domain . name <EOL> ) <EOL> if not self . needs_glue ( ) : <EOL> self . glue = None <EOL> else : <EOL> glue_label = self . server . split ( '<STR_LIT:.>' ) [ <NUM_LIT:0> ] <EOL> if not ( self . glue and self . glue . label == glue_label and <EOL> self . glue . domain == self . domain ) : <EOL> addr_glue = AddressRecord . objects . filter ( <EOL> label = glue_label , domain = self . domain <EOL> ) <EOL> sreg_glue = StaticReg . objects . filter ( <EOL> label = glue_label , domain = self . domain <EOL> ) <EOL> if not ( addr_glue or sreg_glue ) : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> else : <EOL> if addr_glue : <EOL> self . glue = addr_glue [ <NUM_LIT:0> ] <EOL> else : <EOL> self . glue = sreg_glue [ <NUM_LIT:0> ] <EOL> def clean_views ( self , views ) : <EOL> removed_views = set ( View . objects . all ( ) ) - set ( views ) <EOL> for view in removed_views : <EOL> if ( self . domain . soa and <EOL> self . domain . soa . root_domain == self . domain and <EOL> self . domain . nameserver_set . filter ( views = view ) . count ( ) == <NUM_LIT:1> and <EOL> self . domain . soa . has_record_set ( exclude_ns = True , <EOL> view = view ) ) : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" . format ( view ) <EOL> ) <EOL> def check_no_ns_soa_condition ( self , domain ) : <EOL> if ( domain . soa and <EOL> domain . soa . root_domain == domain and <EOL> domain . nameserver_set . count ( ) == <NUM_LIT:1> and <EOL> domain . soa . has_record_set ( exclude_ns = True ) ) : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> def needs_glue ( self ) : <EOL> try : <EOL> possible_label = self . server . replace ( "<STR_LIT:.>" + self . domain . name , "<STR_LIT>" ) <EOL> except ObjectDoesNotExist : <EOL> return False <EOL> if possible_label == self . server : <EOL> return False <EOL> try : <EOL> validate_label ( possible_label ) <EOL> except ValidationError : <EOL> return False <EOL> return True <EOL> reversion . register ( Nameserver ) </s>
