From 33b48341f1300e6f12ec378cbe09908d54d6abcd Mon Sep 17 00:00:00 2001 From: Kristjan Date: Sun, 3 Jan 2021 00:26:33 +0100 Subject: [PATCH] checks before creating new subdomain --- lnbits/extensions/subdomains/crud.py | 10 ++++++++-- lnbits/extensions/subdomains/util.py | 3 --- lnbits/extensions/subdomains/views_api.py | 11 ++++++++++- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lnbits/extensions/subdomains/crud.py b/lnbits/extensions/subdomains/crud.py index cb84ca02f..4b131c602 100644 --- a/lnbits/extensions/subdomains/crud.py +++ b/lnbits/extensions/subdomains/crud.py @@ -4,8 +4,6 @@ from lnbits.helpers import urlsafe_short_hash from . import db from .models import Domains, Subdomains -import httpx - from lnbits.extensions import subdomains @@ -73,6 +71,14 @@ async def get_subdomain(subdomain_id: str) -> Optional[Subdomains]: print(row) return Subdomains(**row) if row else None +async def get_subdomainBySubdomain(subdomain: str) -> Optional[Subdomains]: + row = await db.fetchone( + "SELECT s.*, d.domain as domain_name FROM subdomain s INNER JOIN domain d ON (s.domain = d.id) WHERE s.subdomain = ?", + (subdomain, ), + ) + print(row) + return Subdomains(**row) if row else None + async def get_subdomains(wallet_ids: Union[str, List[str]]) -> List[Subdomains]: if isinstance(wallet_ids, str): diff --git a/lnbits/extensions/subdomains/util.py b/lnbits/extensions/subdomains/util.py index aee883cf9..c7d663073 100644 --- a/lnbits/extensions/subdomains/util.py +++ b/lnbits/extensions/subdomains/util.py @@ -1,7 +1,4 @@ from lnbits.extensions.subdomains.models import Subdomains -import trio # type: ignore -import json -import httpx # Python3 program to validate # domain name diff --git a/lnbits/extensions/subdomains/views_api.py b/lnbits/extensions/subdomains/views_api.py index ed4d60e36..97630da90 100644 --- a/lnbits/extensions/subdomains/views_api.py +++ b/lnbits/extensions/subdomains/views_api.py @@ -1,6 +1,7 @@ import re from quart import g, jsonify, request from http import HTTPStatus +from lnbits.core import crud from lnbits.core.crud import get_user, get_wallet from lnbits.core.services import create_invoice, check_invoice_status @@ -18,6 +19,7 @@ from .crud import ( get_domain, get_domains, delete_domain, + get_subdomainBySubdomain ) @@ -117,9 +119,16 @@ async def api_subdomain_make_subdomain(domain_id): return jsonify({"message": g.data["ip"] + " Not a valid IP address"}), HTTPStatus.BAD_REQUEST if not isValidDomain(g.data["subdomain"] + "." + domain.domain): return ( - jsonify({"message": g.data["subdomain"] + "." + domain.domain + " Bad domain name"}), + jsonify({"message": g.data["subdomain"] + "." + domain.domain + " bad domain name"}), HTTPStatus.BAD_REQUEST, ) + + if ( await get_subdomainBySubdomain(g.data["subdomain"]) is not None): + return ( + jsonify({"message": g.data["subdomain"] + "." + domain.domain + " domain already taken"}), + HTTPStatus.BAD_REQUEST, + ) + if g.data["record_type"] not in domain.allowed_record_types: return jsonify({"message": g.data["record_type"] + "Not a valid record"}), HTTPStatus.BAD_REQUEST