From 231f8a7f30fbffd324b0608374e68f323504f69b Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Mon, 27 Mar 2017 13:29:32 +0200 Subject: [PATCH] Search working --- templates/whoisdb/handle_table.html | 19 +++++++++ templates/whoisdb/search.html | 32 ++++++++++++++ whoisdb/helpers.py | 65 +++++++++++++++++++++++++---- whoisdb/models.py | 2 +- whoisdb/views.py | 11 ++--- 5 files changed, 116 insertions(+), 13 deletions(-) create mode 100644 templates/whoisdb/handle_table.html create mode 100644 templates/whoisdb/search.html diff --git a/templates/whoisdb/handle_table.html b/templates/whoisdb/handle_table.html new file mode 100644 index 0000000..305b51e --- /dev/null +++ b/templates/whoisdb/handle_table.html @@ -0,0 +1,19 @@ +{% load handletags %} + +{% for field in object|getFields:user %} + + + + +{% endfor %} +{% if object|userCanEdit:user %} + + + {% with request.resolver_match.namespaces.0|add:":"|add:object.getClassName|lower|add:"-edit" as editView %} + {% with request.resolver_match.namespaces.0|add:":"|add:object.getClassName|lower|add:"-delete" as deleteView %} + + {% endwith %} + {% endwith %} + +{% endif %} +
{{ field.0 }}{% if field.1.through %}{{ field.1.all|linkObjects|default:"-" }}{% else %}{{ field.1|default:"-" }}{% endif %}
ActionsEdit object, Delete object
diff --git a/templates/whoisdb/search.html b/templates/whoisdb/search.html new file mode 100644 index 0000000..6c4f2d5 --- /dev/null +++ b/templates/whoisdb/search.html @@ -0,0 +1,32 @@ +{% extends "base.html" %} + +{% block content %} +
+
+
+
Search {% if term %}for {{ term }}{% else %}database{% endif %}
+
+

+

+ {% csrf_token %} + + +
+

+ {% if term %} +

+

Search for '{{ term }}'

+ {% for obj in results %} +
{{ obj }}
+ {% include "whoisdb/handle_table.html" with object=obj %} + {% empty %} + No objects found. + {% endfor %} +

+ {% endif %} +
+
+
+
+{% endblock %} + diff --git a/whoisdb/helpers.py b/whoisdb/helpers.py index 8b02d9f..b0712c2 100644 --- a/whoisdb/helpers.py +++ b/whoisdb/helpers.py @@ -2,6 +2,7 @@ import whoisdb.models import domains.models import ipaddress +import re def _addFields(fields, obj, fieldNames): for fieldName in fieldNames: @@ -49,17 +50,67 @@ def guessWhoisObject(self, handle): def findInDatabase(rawValue): # is this an ip address? - rawValue = rawValue.strip() + rawValue = rawValue.strip().upper() value = None - isIp = False + results = [] + + # try subnetwork try: value = ipaddress.ip_network(rawValue, strict=False) - isIp = true except ValueError: - try: - value = ipaddress.ip_address(rawValue) - except ValueError: - pass + pass + + if value: + # ssubnet + obj = whoisdb.models.InetNum.objects.filter(address=str(value.network_address), netmask=value.prefixlen) + results.extend(obj) + + # single ip + value = None + try: + value = ipaddress.ip_address(rawValue) + except ValueError: + pass + + if value: + # TODO FIXME: find smallest matching network + obj = whoisdb.models.InetNum.objects.filter(address=str(value)) + results.extend(obj) + + # asnumber? + m = re.match("^(?:AS)?(\d+)$", rawValue) + if m: + # asnumber! + obj = whoisdb.models.ASNumber(number=int(m.group(1))) + results.extend(obj) + + # asblocks? smallest asblock containing the range + # WHEN anotation foo... could also be done in asnumber match + # also look for number - number queries? + + # domain? + if rawValue.endswith("DN") or rawValue.endswith("DN."): + value = rawValue + if not value.endswith("."): + value += "." + + obj = domains.models.Domain.filter(name=value) + results.extend(obj) + + # contact by name? + + # handlenames for Maintainer, Contact, InetNum, ASNumber, ASBlock + handleObjs = [ + whoisdb.models.Contact, + whoisdb.models.Maintainer, + whoisdb.models.InetNum, + whoisdb.models.ASBlock, + whoisdb.models.ASNumber, + ] + for handleObj in handleObjs: + results.extend(handleObj.objects.filter(handle=rawValue)) + + return results # try: # - ipnetwork diff --git a/whoisdb/models.py b/whoisdb/models.py index 4afd270..f97438f 100644 --- a/whoisdb/models.py +++ b/whoisdb/models.py @@ -158,7 +158,7 @@ class Contact(MntdObject): def getNoDeleteReasons(self): reasons = [] - contactables = [Maintainer] + contactables = [Maintainer, ASBlock, ASNumber, InetNum] for contactable in contactables: candidates = contactable.objects.filter(admin_c=self).annotate(contactCount=models.Count('admin_c')).filter(contactCount__lte=1) for candidate in candidates: diff --git a/whoisdb/views.py b/whoisdb/views.py index 7d0011e..66f6ae8 100644 --- a/whoisdb/views.py +++ b/whoisdb/views.py @@ -82,12 +82,13 @@ def showHandle(request, handle): def searchObject(request): + results = None + term = None if request.method == "POST": - name = request.POST.get("q", None) - if name: - #print(name) - findInDatabase(name) - return render(request, "whoisdb/search.html", {}) + term = request.POST.get("q", None) + if term: + results = findInDatabase(term) + return render(request, "whoisdb/search.html", {"results": results, "term": term}) class MaintainerCreate(LoginRequiredMixin, CreateView):