From 498f3288228a70b065193628c48b8546f950b86a Mon Sep 17 00:00:00 2001 From: phuongntt Date: Fri, 21 Jun 2024 09:40:40 +0700 Subject: [PATCH] chore: add extract fulldomain serializer --- locker_server/api/v1_0/exclude_domains/serializers.py | 6 +++--- locker_server/shared/utils/network.py | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/locker_server/api/v1_0/exclude_domains/serializers.py b/locker_server/api/v1_0/exclude_domains/serializers.py index 711fa1fc..577a6776 100644 --- a/locker_server/api/v1_0/exclude_domains/serializers.py +++ b/locker_server/api/v1_0/exclude_domains/serializers.py @@ -3,7 +3,7 @@ from rest_framework import serializers -from locker_server.shared.utils.network import is_valid_ip, extract_root_domain, is_valid_domain +from locker_server.shared.utils.network import is_valid_ip, extract_root_domain, is_valid_domain, extract_full_domain class ExcludeDomainSerializer(serializers.Serializer): @@ -23,8 +23,8 @@ def validate(self, data): raise serializers.ValidationError(detail={'domain': [ 'This domain or ip is invalid', 'Domain hoặc ip không hợp lệ' ]}) - # if is_domain: - # data["domain"] = extract_root_domain(domain=domain) + if is_domain: + data["domain"] = extract_full_domain(domain=domain) return data def to_representation(self, instance): diff --git a/locker_server/shared/utils/network.py b/locker_server/shared/utils/network.py index 0d79e526..860cdb5e 100644 --- a/locker_server/shared/utils/network.py +++ b/locker_server/shared/utils/network.py @@ -109,6 +109,14 @@ def extract_root_domain(domain: str) -> str: return "{}.{}".format(extracted.domain, extracted.suffix) +def extract_full_domain(domain: str) -> str: + extracted = tldextract.extract(domain) + full_domain = "{}.{}".format(extracted.domain, extracted.suffix) + if extracted.subdomain is not None: + full_domain = "{}.{}".format(extracted.subdomain, full_domain) + return full_domain + + def detect_device(ua_string: str): """ Detect device information from request