django-orchestra/orchestra/apps/domains/helpers.py

39 lines
1.2 KiB
Python
Raw Normal View History

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)
2014-10-04 09:29:18 +00:00
2014-05-08 16:59:35 +00:00
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
def get_subdomains(replace=None, make_top=False):
for subdomain in Domain.objects.filter(name__endswith='.%s' % domain.name):
if replace == subdomain.pk:
# domain is a subdomain, yield our copy
yield domain
else:
if make_top:
subdomain.top = domain
2014-10-03 14:02:11 +00:00
yield subdomain
2014-10-04 09:29:18 +00:00
if not domain.pk:
# top domain lookup for new domains
domain.top = domain.get_top()
2014-05-08 16:59:35 +00:00
if domain.top:
2014-10-04 09:29:18 +00:00
# is a subdomains
domain.top.get_subdomains = partial(get_subdomains, replace=domain.pk)
2014-05-08 16:59:35 +00:00
elif not domain.pk:
2014-10-04 09:29:18 +00:00
# is top domain
domain.get_subdomains = partial(get_subdomains, make_top=True)
2014-05-08 16:59:35 +00:00
return domain