def _get_unique_domain_name(basename, domain_id):
"""Returns unique domain name for given basename.
If basename does not exists in DB with specified domain_id, then it will
be returned as is.
Otherwise will be returned basename with random suffix
"""
pod_domain = PodDomain.query.filter_by(name=basename,
domain_id=domain_id).first()
if pod_domain is None:
return basename
res = None
# try to get unique random domain name. If it fails for tries limit,
# then something is going wrong, return None and it will be better to fail
# in calling code
tries_limit = 100
random_suffix_length = 6
for _ in xrange(tries_limit):
suffix = randstr(
symbols=string.lowercase + string.digits,
length=random_suffix_length)
new_name = '{0}{1}'.format(basename, suffix)
pod_domain = PodDomain.query.filter_by(
name=new_name, domain_id=domain_id).first()
if pod_domain is None:
res = new_name
break
return res
评论列表
文章目录