django-orchestra-test/orchestra/contrib/domains/helpers.py

33 lines
1.2 KiB
Python
Raw Permalink Normal View History

2014-05-08 16:59:35 +00:00
import copy
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)
2015-05-05 19:42:55 +00:00
def get_declared_records(records=records):
2014-05-08 16:59:35 +00:00
for data in records:
yield Record(type=data['type'], value=data['value'])
2015-05-05 19:42:55 +00:00
domain.get_declared_records = get_declared_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
2015-04-09 14:32:10 +00:00
subdomains = domain.top.subdomains.select_related('top').prefetch_related('records').all()
subdomains = [sub for sub in subdomains if sub.pk != domain.pk]
2014-10-04 13:23:04 +00:00
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