Example #1
0
def domain_exists(request):
    ''' 判断域名是否存在 '''
    if request.method == 'POST':
        key = request.GET.get('key', 'merchant_shop').strip()
        domain = request.GET.get('domain', '').strip()
        # 检查domain是否符合规则: 域名不能低于4个字符,不能超过20个字符。只能含有“字母”“数字”
        pat = r'^[a-zA-Z0-9]{4,20}(\.yhd\.com)?$'
        m = re.match(pat, domain)
        if not m:
            response = simplejson.dumps({
                'success': False,
                'msg': u'域名不符合规则,请检查'
            })
            return HttpResponse(response)
        try:
            item = DnsApiZone.objects.get(key=key)
        except DnsApiZone.DoesNotExist:
            response = simplejson.dumps({'success': False, 'msg': u'无效的请求'})
            return HttpResponse(response)
        model = item.zone
        if not model:
            response = simplejson.dumps({'success': False, 'msg': u'Zone不存在'})
            return HttpResponse(response)
        zone = Zone(model)
        exists = zone.domain_exists(domain)
        response = simplejson.dumps({'success': True, 'exists': exists})
        return HttpResponse(response)
Example #2
0
def domain_add(request):
    ''' 增加域名列表 '''
    if request.method == 'POST':
        key = request.REQUEST.get('key', 'merchant_shop')
        intime = request.REQUEST.get('intime', '1')
        intime = int(intime) if intime and intime.isdigit() else 1
        lists = [
            item.strip()
            for item in request.REQUEST.get('lists', '').split(',')
        ]
        if not lists:
            response = simplejson.dumps({'success': False, 'msg': u'请输入域名列表'})
            return HttpResponse(response)
        pat = r'^[a-zA-Z0-9]{4,20}(\.yhd\.com)?$'
        for domain in lists:
            m = re.match(pat, domain)
            if not m:
                response = simplejson.dumps({
                    'success': False,
                    'msg': u'域名不符合规则, 请检查'
                })
                return HttpResponse(response)
        try:
            item = DnsApiZone.objects.get(key=key)
        except DnsApiZone.DoesNotExist:
            response = simplejson.dumps({'success': False, 'msg': u'无效的请求'})
            return HttpResponse(response)
        model = item.zone
        if not model:
            response = simplejson.dumps({'success': False, 'msg': u'Zone不存在'})
            return HttpResponse(response)
        zone = Zone(model)
        zone.user = '******'
        zone.owner = 9  # owner = 9 表示属于API
        exists = []
        dones = []
        for domain in lists:
            domain = domain.strip()
            if zone.domain_exists(domain):
                exists.append(domain)
            else:
                try:
                    zone.save_record(domain, zone.ttl, 'CNAME',
                                     'merchant-shop.yhd.com.yhcdn.cn.')
                except ZoneException as e:
                    pass
                else:
                    dones.append(domain)
        '''if intime and dones:
            zone.validate(reload=True, backup=False)         # 生效'''
        response = simplejson.dumps({'success': True, 'exists': exists})
        return HttpResponse(response)