diff --git a/requirements.txt b/requirements.txt index a30b779..59fc648 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,7 @@ scrapy==2.11.0 -psycopg2==2.9.6 +psycopg2-binary==2.9.6 django==4.1.7 django-debug-toolbar==4.0.0 aiogram==3.0.0b8 python-dotenv==1.0.0 +python-whois==0.8.0 diff --git a/src/website/catalog/forms.py b/src/website/catalog/forms.py index 9232b28..94f3aec 100644 --- a/src/website/catalog/forms.py +++ b/src/website/catalog/forms.py @@ -1,4 +1,8 @@ from django import forms +from django.core.validators import RegexValidator + +URL_VALIDATOR_MESSAGE = 'Недопустимый URL.' +URL_VALIDATOR = RegexValidator(regex=r'^(https?://)?(www\.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/[a-zA-Z0-9._%+-]*)*$', message=URL_VALIDATOR_MESSAGE) SORT_BY_CHOICES = [ ("CN", "Имя компании"), @@ -28,3 +32,12 @@ class CompaniesSortForm(forms.Form): label="Поиск", max_length=50, required=False,) + + +class DomainLookupForm(forms.Form): + domain = forms.CharField( + label="Домен:", + max_length=253, + required=False, + validators=[URL_VALIDATOR] + ) diff --git a/src/website/catalog/views.py b/src/website/catalog/views.py index d545f7e..712e8ac 100644 --- a/src/website/catalog/views.py +++ b/src/website/catalog/views.py @@ -1,9 +1,12 @@ from django.shortcuts import render from django.http import HttpResponseNotFound from django.db.models import Q +from django.contrib import messages from .models import Price, Registrator -from .forms import CompaniesSortForm +from .forms import CompaniesSortForm, DomainLookupForm + +import whois SORT_FIELD_NAMES = { @@ -17,18 +20,32 @@ def registrator_list(request): + search = '' + sort_by = 'id' if request.method == "POST": - form = CompaniesSortForm(request.POST) - if form.is_valid(): - sort_by = (form.cleaned_data['reverse_order'] - + SORT_FIELD_NAMES.get( - form.cleaned_data['sort_by'], 'name')) - search = form.cleaned_data['search'] + if 'search_sub' in request.POST: + form = CompaniesSortForm(request.POST) + domain_lookup_form = DomainLookupForm() + if form.is_valid(): + sort_by = form.cleaned_data['reverse_order'] + SORT_FIELD_NAMES.get(form.cleaned_data['sort_by'], 'name') + search = form.cleaned_data['search'] + + elif 'whois_sub' in request.POST: + domain_lookup_form = DomainLookupForm(request.POST) + form = CompaniesSortForm() + if domain_lookup_form.is_valid(): + domain = domain_lookup_form.cleaned_data.get('domain') + try: + whois.whois(domain) + messages.success(request, f"Домен: {domain} занят.") + except whois.parser.PywhoisError: + messages.success(request, f"Домен: {domain} свободен.") + else: + messages.error(request, "Недопустимый URL.") else: form = CompaniesSortForm() - sort_by = 'id' - search = '' + domain_lookup_form = DomainLookupForm() if search: companies = Price.objects.filter(Q(registrator__name__icontains=search) | Q( @@ -40,7 +57,7 @@ def registrator_list(request): companies = Price.objects.filter(id__in=companies).distinct('registrator_id') companies = Price.objects.filter(id__in=companies).order_by(sort_by) - return render(request, 'registrator-list.html', {'companies': companies, 'form': form}) + return render(request, 'registrator-list.html', {'companies': companies, 'form': form, 'domain_lookup_form': domain_lookup_form}) def registrator_details(request, id): diff --git a/src/website/website/templates/companies-sort.html b/src/website/website/templates/companies-sort.html index 8a48f61..8651f90 100644 --- a/src/website/website/templates/companies-sort.html +++ b/src/website/website/templates/companies-sort.html @@ -6,7 +6,7 @@
{% csrf_token %} {{ form }} - +
diff --git a/src/website/website/templates/domain-lookup-form.html b/src/website/website/templates/domain-lookup-form.html new file mode 100644 index 0000000..4bf5775 --- /dev/null +++ b/src/website/website/templates/domain-lookup-form.html @@ -0,0 +1,120 @@ + + + + +
+
+ {% csrf_token %} + {{ domain_lookup_form.domain.label }} + {{ domain_lookup_form.domain }} + + {% if messages %} + + {% endif %} +
+ +
+ diff --git a/src/website/website/templates/navbar.html b/src/website/website/templates/navbar.html index e9244bc..981d539 100644 --- a/src/website/website/templates/navbar.html +++ b/src/website/website/templates/navbar.html @@ -42,6 +42,7 @@ + {% include 'domain-lookup-form.html' with form=form %}
Проект