2016-12-20 18:05:20 -05:00
|
|
|
from django import forms
|
2021-02-11 16:21:54 +05:30
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from datacenterlight.utils import get_ip_address
|
2018-02-20 15:11:09 +01:00
|
|
|
from .models import ContactUs
|
2017-02-14 23:34:06 -05:00
|
|
|
|
2021-02-11 16:21:54 +05:30
|
|
|
import ipaddress
|
|
|
|
|
2017-02-14 23:34:06 -05:00
|
|
|
|
2017-08-27 13:19:05 +05:30
|
|
|
class ContactForm(forms.ModelForm):
|
|
|
|
class Meta:
|
2021-02-11 16:21:54 +05:30
|
|
|
fields = ['name', 'email', 'message', 'ip_address']
|
2017-08-27 13:19:05 +05:30
|
|
|
model = ContactUs
|
2021-02-11 16:21:54 +05:30
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.request = kwargs.pop('request', None)
|
|
|
|
super(ContactForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean_ip_address(self):
|
|
|
|
input_ip_address_text = self.cleaned_data.get('ip_address')
|
|
|
|
request_ip_address = get_ip_address(self.request)
|
|
|
|
ip_address_type_text = 'ipv4'
|
|
|
|
if (type(ipaddress.ip_address(request_ip_address))
|
|
|
|
== ipaddress.IPv4Address):
|
|
|
|
ip_address_type_text = 'ipv4'
|
|
|
|
elif (type(ipaddress.ip_address(request_ip_address))
|
|
|
|
== ipaddress.IPv6Address):
|
|
|
|
ip_address_type_text = 'ipv6'
|
|
|
|
if input_ip_address_text.strip().lower() == ip_address_type_text:
|
|
|
|
return request_ip_address
|
|
|
|
else:
|
|
|
|
raise forms.ValidationError(_("Input correct IP address type"))
|