2014-05-08 16:59:35 +00:00
|
|
|
import copy
|
2014-10-04 09:29:18 +00:00
|
|
|
from functools import partial
|
2014-05-08 16:59:35 +00:00
|
|
|
|
|
|
|
from .models import Domain, Record
|
|
|
|
|
|
|
|
|
|
|
|
def domain_for_validation(instance, records):
|
2014-10-04 09:29:18 +00:00
|
|
|
"""
|
|
|
|
Since the new data is not yet on the database, we update it on the fly,
|
|
|
|
so when validation calls render_zone() it will use the new provided data
|
|
|
|
"""
|
2014-05-08 16:59:35 +00:00
|
|
|
domain = copy.copy(instance)
|
|
|
|
def get_records():
|
|
|
|
for data in records:
|
|
|
|
yield Record(type=data['type'], value=data['value'])
|
|
|
|
domain.get_records = get_records
|
2014-10-03 14:02:11 +00:00
|
|
|
|
2014-10-04 09:29:18 +00:00
|
|
|
if not domain.pk:
|
|
|
|
# top domain lookup for new domains
|
2015-03-29 16:10:07 +00:00
|
|
|
domain.top = domain.get_parent(top=True)
|
2014-05-08 16:59:35 +00:00
|
|
|
if domain.top:
|
2014-10-04 13:23:04 +00:00
|
|
|
# is a subdomain
|
|
|
|
subdomains = [sub for sub in domain.top.subdomains.all() if sub.pk != domain.pk]
|
|
|
|
domain.top.get_subdomains = lambda: subdomains + [domain]
|
2014-05-08 16:59:35 +00:00
|
|
|
elif not domain.pk:
|
2014-10-04 13:23:04 +00:00
|
|
|
# is a new top domain
|
|
|
|
subdomains = []
|
|
|
|
for subdomain in Domain.objects.filter(name__endswith='.%s' % domain.name):
|
|
|
|
subdomain.top = domain
|
|
|
|
subdomains.append(subdomain)
|
|
|
|
domain.get_subdomains = lambda: subdomains
|
2014-05-08 16:59:35 +00:00
|
|
|
return domain
|