<s> from django . http import Http404 <EOL> from django . core . exceptions import ObjectDoesNotExist , ValidationError <EOL> from django . db . models import Q , F <EOL> from mozdns . domain . models import Domain <EOL> from mozdns . mx . models import MX <EOL> from mozdns . sshfp . models import SSHFP <EOL> from mozdns . txt . models import TXT <EOL> from mozdns . srv . models import SRV <EOL> from mozdns . address_record . models import AddressRecord <EOL> from mozdns . cname . models import CNAME <EOL> from mozdns . view . models import View <EOL> from core . interface . static_intr . models import StaticInterface <EOL> from copy import deepcopy <EOL> import pdb <EOL> def tablefy ( objects , views = True ) : <EOL> """<STR_LIT>""" <EOL> matrix = [ ] <EOL> urls = [ ] <EOL> headers = [ ] <EOL> if not objects : <EOL> return ( None , None , None ) <EOL> for title , value in objects [ <NUM_LIT:0> ] . details ( ) : <EOL> headers . append ( title ) <EOL> if views : <EOL> headers . append ( "<STR_LIT>" ) <EOL> for obj in objects : <EOL> row = [ ] <EOL> urls . append ( obj . get_fancy_edit_url ( ) ) <EOL> for title , value in obj . details ( ) : <EOL> row . append ( value ) <EOL> if views : <EOL> views = "<STR_LIT>" <EOL> if hasattr ( obj , '<STR_LIT>' ) : <EOL> for view in obj . views . all ( ) : <EOL> views += view . name + "<STR_LIT:U+002CU+0020>" <EOL> views = views . strip ( "<STR_LIT:U+002CU+0020>" ) <EOL> row . append ( views ) <EOL> matrix . append ( row ) <EOL> return ( headers , matrix , urls ) <EOL> def slim_form ( domain_pk = None , form = None ) : <EOL> """<STR_LIT>""" <EOL> if not form : <EOL> raise Http404 <EOL> if domain_pk : <EOL> query_set = Domain . objects . filter ( id = domain_pk ) <EOL> if not query_set : <EOL> raise Http404 <EOL> if not form . fields . get ( '<STR_LIT>' , False ) : <EOL> raise Http404 <EOL> form . fields [ '<STR_LIT>' ] . _queryset = query_set <EOL> form . fields [ '<STR_LIT>' ] . widget . choices . queryset = query_set <EOL> form . fields [ '<STR_LIT>' ] . empty_label = None <EOL> return form <EOL> def get_clobbered ( domain_name ) : <EOL> classes = [ MX , AddressRecord , CNAME , TXT , SRV , StaticInterface , SSHFP ] <EOL> clobber_objects = [ ] <EOL> for Klass in classes : <EOL> objs = Klass . objects . filter ( fqdn = domain_name ) <EOL> for obj in objs : <EOL> obj_views = [ view . name for view in obj . views . all ( ) ] <EOL> new_obj = deepcopy ( obj ) <EOL> new_obj . id = None <EOL> new_obj . label = "<STR_LIT>" <EOL> clobber_objects . append ( ( new_obj , obj_views ) ) <EOL> if Klass == AddressRecord : <EOL> kwargs = { "<STR_LIT>" : False , "<STR_LIT>" : False } <EOL> else : <EOL> kwargs = { "<STR_LIT>" : False } <EOL> obj . delete ( ** kwargs ) <EOL> return clobber_objects <EOL> def ensure_domain ( name , purgeable = False , inherit_soa = False , force = False ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> domain = Domain . objects . get ( name = name ) <EOL> return domain <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> parts = list ( reversed ( name . split ( '<STR_LIT:.>' ) ) ) <EOL> if not force : <EOL> domain_name = '<STR_LIT>' <EOL> leaf_domain = None <EOL> for i in range ( len ( parts ) ) : <EOL> domain_name = parts [ i ] + '<STR_LIT:.>' + domain_name <EOL> domain_name = domain_name . strip ( '<STR_LIT:.>' ) <EOL> try : <EOL> tmp_domain = Domain . objects . get ( name = domain_name ) <EOL> leaf_domain = tmp_domain <EOL> except ObjectDoesNotExist : <EOL> continue <EOL> if not leaf_domain : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if leaf_domain . delegated : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if not leaf_domain . soa : <EOL> raise ValidationError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> domain_name = '<STR_LIT>' <EOL> for i in range ( len ( parts ) ) : <EOL> domain_name = parts [ i ] + '<STR_LIT:.>' + domain_name <EOL> domain_name = domain_name . strip ( '<STR_LIT:.>' ) <EOL> clobber_objects = get_clobbered ( domain_name ) <EOL> domain , created = Domain . objects . get_or_create ( name = domain_name ) <EOL> if purgeable and created : <EOL> domain . purgeable = True <EOL> domain . save ( ) <EOL> if ( inherit_soa and created and domain . master_domain and <EOL> domain . master_domain . soa is not None ) : <EOL> domain . soa = domain . master_domain . soa <EOL> domain . save ( ) <EOL> for object_ , views in clobber_objects : <EOL> try : <EOL> object_ . domain = domain <EOL> object_ . clean ( ) <EOL> object_ . save ( ) <EOL> for view_name in views : <EOL> view = View . objects . get ( name = view_name ) <EOL> object_ . views . add ( view ) <EOL> object_ . save ( ) <EOL> except ValidationError : <EOL> pdb . set_trace ( ) <EOL> pass <EOL> return domain <EOL> def ensure_label_domain ( fqdn , force = False ) : <EOL> """<STR_LIT>""" <EOL> if fqdn == '<STR_LIT>' : <EOL> raise ValidationError ( "<STR_LIT>" ) <EOL> try : <EOL> domain = Domain . objects . get ( name = fqdn ) <EOL> if not domain . soa and not force : <EOL> raise ValidationError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return '<STR_LIT>' , domain <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> fqdn_partition = fqdn . split ( '<STR_LIT:.>' ) <EOL> if len ( fqdn_partition ) == <NUM_LIT:1> : <EOL> raise ValidationError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( fqdn ) ) <EOL> else : <EOL> label , domain_name = fqdn_partition [ <NUM_LIT:0> ] , '<STR_LIT:.>' . join ( fqdn_partition [ <NUM_LIT:1> : ] ) <EOL> domain = ensure_domain ( domain_name , purgeable = True , inherit_soa = True ) <EOL> if not domain . soa and not force : <EOL> raise ValidationError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return label , domain <EOL> def prune_tree ( domain ) : <EOL> return prune_tree_helper ( domain , [ ] ) <EOL> def prune_tree_helper ( domain , deleted_domains ) : <EOL> if not domain : <EOL> return deleted_domains <EOL> if domain . domain_set . all ( ) . count ( ) : <EOL> return deleted_domains <EOL> if domain . has_record_set ( ) : <EOL> return deleted_domains <EOL> elif not domain . purgeable : <EOL> return deleted_domains <EOL> else : <EOL> master_domain = domain . master_domain <EOL> if not master_domain : <EOL> return deleted_domains <EOL> purged_domain = deepcopy ( domain ) <EOL> purged_domain . id = None <EOL> deleted_domains . append ( purged_domain ) <EOL> domain . delete ( ) <EOL> return prune_tree_helper ( master_domain , deleted_domains ) <EOL> def get_zones ( ) : <EOL> """<STR_LIT>""" <EOL> return Domain . objects . filter ( ~ Q ( master_domain__soa = F ( '<STR_LIT>' ) ) , <EOL> soa__isnull = False ) </s>
