Search working

This commit is contained in:
Sebastian Lohff 2017-03-27 13:29:32 +02:00
parent 33de8402b7
commit 231f8a7f30
5 changed files with 116 additions and 13 deletions

View File

@ -0,0 +1,19 @@
{% load handletags %}
<table class="table">
{% for field in object|getFields:user %}
<tr>
<td>{{ field.0 }}</td>
<td>{% if field.1.through %}{{ field.1.all|linkObjects|default:"-" }}{% else %}{{ field.1|default:"-" }}{% endif %}</td>
</tr>
{% endfor %}
{% if object|userCanEdit:user %}
<tr>
<td>Actions</td>
{% 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 %}
<td><a href="{% url editView object.getPK %}">Edit object<a/>, <a href="{% url deleteView object.getPK %}">Delete object</a></td>
{% endwith %}
{% endwith %}
</tr>
{% endif %}
</table>

View File

@ -0,0 +1,32 @@
{% extends "base.html" %}
{% block content %}
<div class="row">
<div class="col-sm-12">
<div class="panel panel-default">
<div class="panel-heading">Search {% if term %}for {{ term }}{% else %}database{% endif %}</div>
<div class="panel-body">
<p>
<form class="form" method="post" action="{% url "whoisdb:search" %}">
{% csrf_token %}
<input type="text" name="q" placeholder="Search in DB">
<button class="btn btn-primary">Search</button>
</form>
</p>
{% if term %}
<p>
<h3>Search for '{{ term }}'</h3>
{% for obj in results %}
<h5>{{ obj }}</h5>
{% include "whoisdb/handle_table.html" with object=obj %}
{% empty %}
No objects found.
{% endfor %}
</p>
{% endif %}
</div>
</div>
</div>
</div>
{% endblock %}

View File

@ -2,6 +2,7 @@ import whoisdb.models
import domains.models import domains.models
import ipaddress import ipaddress
import re
def _addFields(fields, obj, fieldNames): def _addFields(fields, obj, fieldNames):
for fieldName in fieldNames: for fieldName in fieldNames:
@ -49,17 +50,67 @@ def guessWhoisObject(self, handle):
def findInDatabase(rawValue): def findInDatabase(rawValue):
# is this an ip address? # is this an ip address?
rawValue = rawValue.strip() rawValue = rawValue.strip().upper()
value = None value = None
isIp = False results = []
# try subnetwork
try: try:
value = ipaddress.ip_network(rawValue, strict=False) value = ipaddress.ip_network(rawValue, strict=False)
isIp = true
except ValueError: except ValueError:
try: pass
value = ipaddress.ip_address(rawValue)
except ValueError: if value:
pass # 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: # try:
# - ipnetwork # - ipnetwork

View File

@ -158,7 +158,7 @@ class Contact(MntdObject):
def getNoDeleteReasons(self): def getNoDeleteReasons(self):
reasons = [] reasons = []
contactables = [Maintainer] contactables = [Maintainer, ASBlock, ASNumber, InetNum]
for contactable in contactables: for contactable in contactables:
candidates = contactable.objects.filter(admin_c=self).annotate(contactCount=models.Count('admin_c')).filter(contactCount__lte=1) candidates = contactable.objects.filter(admin_c=self).annotate(contactCount=models.Count('admin_c')).filter(contactCount__lte=1)
for candidate in candidates: for candidate in candidates:

View File

@ -82,12 +82,13 @@ def showHandle(request, handle):
def searchObject(request): def searchObject(request):
results = None
term = None
if request.method == "POST": if request.method == "POST":
name = request.POST.get("q", None) term = request.POST.get("q", None)
if name: if term:
#print(name) results = findInDatabase(term)
findInDatabase(name) return render(request, "whoisdb/search.html", {"results": results, "term": term})
return render(request, "whoisdb/search.html", {})
class MaintainerCreate(LoginRequiredMixin, CreateView): class MaintainerCreate(LoginRequiredMixin, CreateView):