|
|
|
@ -10,7 +10,7 @@ from rest_framework.response import Response |
|
|
|
|
from rest_framework.views import APIView |
|
|
|
|
|
|
|
|
|
from api.models import UserDomainInfo, Apps |
|
|
|
|
from api.utils.auth import ExpiringTokenAuthentication, DownloadQrPermission |
|
|
|
|
from api.utils.auth import ExpiringTokenAuthentication |
|
|
|
|
from api.utils.baseutils import is_valid_domain, get_cname_from_domain |
|
|
|
|
from api.utils.modelutils import get_user_domain_name, get_min_default_domain_cname_obj |
|
|
|
|
from api.utils.response import BaseResponse |
|
|
|
@ -25,14 +25,27 @@ def get_domain_filter(request): |
|
|
|
|
filter_dict = {'user_id': request.user, 'app_id__app_id': None} |
|
|
|
|
app_id = request.query_params.get("app_id", request.data.get("app_id", None)) |
|
|
|
|
domain_type = request.query_params.get("domain_type", request.data.get("domain_type", None)) |
|
|
|
|
domain_name = request.query_params.get("domain_name", request.data.get("domain_name", '')) |
|
|
|
|
if app_id is not None: |
|
|
|
|
filter_dict['app_id__app_id'] = app_id |
|
|
|
|
if domain_type is not None: |
|
|
|
|
filter_dict['domain_type'] = domain_type |
|
|
|
|
if domain_name: |
|
|
|
|
filter_dict['domain_name'] = domain_name |
|
|
|
|
logger.info(f"domain filter {filter_dict}") |
|
|
|
|
return filter_dict |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def auto_clean_download_cache(user_obj, user_domain_obj, app_obj): |
|
|
|
|
if user_domain_obj: |
|
|
|
|
base_domain_queryset = UserDomainInfo.objects.filter(user_id=user_obj, is_enable=True).all() |
|
|
|
|
if user_domain_obj.domain_type in [0, 1]: |
|
|
|
|
if base_domain_queryset.filter(domain_type__in=[0, 1]).count() == 0: |
|
|
|
|
reset_app_wx_easy_type(user_obj, None) |
|
|
|
|
else: |
|
|
|
|
reset_app_wx_easy_type(user_obj, app_obj) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def remove_domain_wx_easy(app_obj, user_obj): |
|
|
|
|
if app_obj and not get_user_domain_name(user_obj): |
|
|
|
|
app_obj.wxeasytype = True |
|
|
|
@ -62,13 +75,13 @@ def add_new_domain_info(res, request, domain_name, domain_type): |
|
|
|
|
|
|
|
|
|
class DomainCnameView(APIView): |
|
|
|
|
authentication_classes = [ExpiringTokenAuthentication, ] |
|
|
|
|
permission_classes = [DownloadQrPermission, ] |
|
|
|
|
|
|
|
|
|
def get(self, request): |
|
|
|
|
res = BaseResponse() |
|
|
|
|
res.data = {'domain_name': '', 'domain_record': '', 'is_enable': False} |
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**get_domain_filter(request)).first() |
|
|
|
|
if user_domain_obj: |
|
|
|
|
domain_name = request.query_params.get("domain_name", '') |
|
|
|
|
if (user_domain_obj and domain_name) or (user_domain_obj and user_domain_obj.domain_type == 0): |
|
|
|
|
res.data['domain_name'] = user_domain_obj.domain_name |
|
|
|
|
res.data['is_enable'] = user_domain_obj.is_enable |
|
|
|
|
if user_domain_obj.cname_id: |
|
|
|
@ -87,7 +100,7 @@ class DomainCnameView(APIView): |
|
|
|
|
if domain_name: |
|
|
|
|
domain_name = domain_name.strip(" ") |
|
|
|
|
if domain_name and len(domain_name) > 3 and is_valid_domain(domain_name): |
|
|
|
|
filter_dict = get_domain_filter(request) |
|
|
|
|
# filter_dict = get_domain_filter(request) |
|
|
|
|
if not force_bind: |
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(domain_name=domain_name, is_enable=True).first() |
|
|
|
|
if user_domain_obj: |
|
|
|
@ -104,16 +117,18 @@ class DomainCnameView(APIView): |
|
|
|
|
res.msg = "该域名已经被其他用户绑定" |
|
|
|
|
res.msg += ",请更换要绑定的域名,或者进行强制绑定域名" |
|
|
|
|
else: |
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**get_domain_filter(request), |
|
|
|
|
domain_name=domain_name).first() |
|
|
|
|
kwargs = get_domain_filter(request) |
|
|
|
|
kwargs['domain_name'] = domain_name |
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**kwargs).first() |
|
|
|
|
if user_domain_obj: |
|
|
|
|
res.data = {'cname_domain': user_domain_obj.cname_id.domain_record} |
|
|
|
|
else: |
|
|
|
|
UserDomainInfo.objects.filter(**filter_dict, is_enable=False).delete() |
|
|
|
|
UserDomainInfo.objects.filter(**kwargs, is_enable=False).delete() |
|
|
|
|
add_new_domain_info(res, request, domain_name, domain_type) |
|
|
|
|
else: |
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**filter_dict, |
|
|
|
|
domain_name=domain_name).first() |
|
|
|
|
kwargs = get_domain_filter(request) |
|
|
|
|
kwargs['domain_name'] = domain_name |
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**kwargs).first() |
|
|
|
|
if user_domain_obj: |
|
|
|
|
res.data = {'cname_domain': user_domain_obj.cname_id.domain_record} |
|
|
|
|
else: |
|
|
|
@ -125,27 +140,40 @@ class DomainCnameView(APIView): |
|
|
|
|
|
|
|
|
|
def put(self, request): |
|
|
|
|
res = BaseResponse() |
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**get_domain_filter(request)).first() |
|
|
|
|
|
|
|
|
|
kwargs = get_domain_filter(request) |
|
|
|
|
if kwargs.get('domain_type', -1) in [0, 2]: |
|
|
|
|
domain_name = kwargs.get('domain_name') |
|
|
|
|
if domain_name: |
|
|
|
|
kwargs.pop('domain_name') |
|
|
|
|
kwargs['is_enable'] = False |
|
|
|
|
|
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**kwargs).first() |
|
|
|
|
if user_domain_obj: |
|
|
|
|
cname = get_cname_from_domain(user_domain_obj.domain_name) |
|
|
|
|
if cname == user_domain_obj.cname_id.domain_record + '.': |
|
|
|
|
cname = get_cname_from_domain(user_domain_obj.domain_name, user_domain_obj.cname_id.domain_record + '.') |
|
|
|
|
if cname: |
|
|
|
|
user_domain_obj_list = UserDomainInfo.objects.filter(domain_name=user_domain_obj.domain_name, |
|
|
|
|
is_enable=True).all() |
|
|
|
|
if len(user_domain_obj_list) < 2: |
|
|
|
|
if len(user_domain_obj_list) == 1: |
|
|
|
|
user_obj = user_domain_obj_list.first().user_id |
|
|
|
|
app_obj = user_domain_obj_list.first().app_id |
|
|
|
|
o_user_domain_obj = user_domain_obj_list.first() |
|
|
|
|
auto_clean_download_cache(user_obj, o_user_domain_obj, app_obj) |
|
|
|
|
user_domain_obj_list.delete() |
|
|
|
|
reset_app_wx_easy_type(user_obj, app_obj) |
|
|
|
|
|
|
|
|
|
if kwargs.get('domain_type', -1) in [0, 2]: |
|
|
|
|
kwargs.pop('is_enable') |
|
|
|
|
UserDomainInfo.objects.filter(**kwargs).delete() |
|
|
|
|
|
|
|
|
|
user_domain_obj.is_enable = True |
|
|
|
|
user_domain_obj.save(update_fields=["is_enable"]) |
|
|
|
|
user_domain_obj.save() |
|
|
|
|
|
|
|
|
|
app_id = request.data.get("app_id", None) |
|
|
|
|
app_obj = None |
|
|
|
|
if app_id: |
|
|
|
|
app_obj = Apps.objects.filter(app_id=app_id).first() |
|
|
|
|
reset_app_wx_easy_type(request.user, app_obj) |
|
|
|
|
auto_clean_download_cache(request.user, user_domain_obj, app_obj) |
|
|
|
|
else: |
|
|
|
|
res.code = 1002 |
|
|
|
|
res.msg = "该域名查询校验失败,请检查" |
|
|
|
@ -157,7 +185,7 @@ class DomainCnameView(APIView): |
|
|
|
|
res.msg = "域名已经被绑定或者域名有误" |
|
|
|
|
return Response(res.dict) |
|
|
|
|
|
|
|
|
|
def delete(sele, request): |
|
|
|
|
def delete(self, request): |
|
|
|
|
res = BaseResponse() |
|
|
|
|
user_domain_obj = UserDomainInfo.objects.filter(**get_domain_filter(request)).first() |
|
|
|
|
if user_domain_obj: |
|
|
|
@ -165,7 +193,7 @@ class DomainCnameView(APIView): |
|
|
|
|
app_obj = None |
|
|
|
|
if app_id: |
|
|
|
|
app_obj = Apps.objects.filter(app_id=app_id).first() |
|
|
|
|
reset_app_wx_easy_type(request.user, app_obj) |
|
|
|
|
auto_clean_download_cache(request.user, user_domain_obj, app_obj) |
|
|
|
|
user_domain_obj.delete() |
|
|
|
|
return Response(res.dict) |
|
|
|
|
|
|
|
|
@ -203,3 +231,18 @@ class DomainInfoView(APIView): |
|
|
|
|
res.domain_type_choices = get_choices_dict(UserDomainInfo.domain_type_choices) |
|
|
|
|
|
|
|
|
|
return Response(res.dict) |
|
|
|
|
|
|
|
|
|
def put(self, request): |
|
|
|
|
res = BaseResponse() |
|
|
|
|
domain_name = request.data.get('domain_name', '') |
|
|
|
|
weight = request.data.get('weight', 10) |
|
|
|
|
domain_type = request.data.get('domain_type', None) |
|
|
|
|
if domain_type is not None and weight and domain_name: |
|
|
|
|
domain_name_obj = UserDomainInfo.objects.filter(user_id=request.user, domain_name=domain_name, |
|
|
|
|
domain_type=domain_type).all() |
|
|
|
|
if domain_name_obj and len(domain_name_obj) == 1: |
|
|
|
|
domain_name_obj.update(weight=weight) |
|
|
|
|
else: |
|
|
|
|
res.code = 1002 |
|
|
|
|
res.msg = '参数有误' |
|
|
|
|
return Response(res.dict) |
|
|
|
|