imrpoved ipv4/ipv6 check. now always update ip, in case host changes IP. added a create_host() method for the HostForm.
This commit is contained in:
parent
8775295c9a
commit
a96c7b4608
@ -2,14 +2,16 @@
|
||||
from django.http import HttpResponse
|
||||
from django.conf import settings
|
||||
from main.forms import *
|
||||
import dns.inet
|
||||
|
||||
def MyIpView(request):
|
||||
return HttpResponse(request.META['REMOTE_ADDR'], content_type="text/plain")
|
||||
|
||||
def UpdateIpView(request):
|
||||
ip = request.META['REMOTE_ADDR']
|
||||
if ':' in ip:
|
||||
request.session['ipv6'] = request.META['REMOTE_ADDR']
|
||||
if '.' in ip:
|
||||
request.session['ipv4'] = request.META['REMOTE_ADDR']
|
||||
ipaddr = request.META['REMOTE_ADDR']
|
||||
af = dns.inet.af_for_address(ipaddr)
|
||||
if af == dns.inet.AF_INET:
|
||||
request.session['ipv4'] = ipaddr
|
||||
else:
|
||||
request.session['ipv6'] = ipaddr
|
||||
return HttpResponse('OK', content_type="text/plain")
|
@ -9,3 +9,10 @@ class HostForm(forms.ModelForm):
|
||||
model = Host
|
||||
fields = ['fqdn', 'comment', ]
|
||||
|
||||
def create_host(self):
|
||||
self.clean()
|
||||
host = Host(fqdn=self.cleaned_data['fqdn'],
|
||||
comment=self.cleaned_data['comment'])
|
||||
host.save()
|
||||
return host
|
||||
|
||||
|
@ -8,12 +8,8 @@
|
||||
IPv6: <input type='text' class="form-control ipv6adr" placeholder="can't find IP" value="{{session.ipv6}}">
|
||||
|
||||
<div style="display: none">
|
||||
{% if not session.ipv4 %}
|
||||
<img src="//{{ WWW_IPV4_HOST }}/updateip" />
|
||||
{% endif %}
|
||||
{% if not session.ipv6 %}
|
||||
<img src="//{{ WWW_IPV6_HOST }}/updateip" />
|
||||
{% endif %}
|
||||
<img src="//{{ WWW_IPV4_HOST }}/updateip" />
|
||||
<img src="//{{ WWW_IPV6_HOST }}/updateip" />
|
||||
</div>
|
||||
|
||||
{{ HostForm }}
|
||||
|
@ -28,5 +28,4 @@ class OverviewView(TemplateView):
|
||||
if form.is_valid():
|
||||
host = form.create_host()
|
||||
host.save()
|
||||
context['HostForm'] = form
|
||||
return context
|
||||
|
Loading…
x
Reference in New Issue
Block a user