From 409405f2967bcf890bae2057bcc72a7a5126f196 Mon Sep 17 00:00:00 2001 From: PCoder Date: Thu, 11 Feb 2021 16:21:54 +0530 Subject: [PATCH] Show captcha text --- datacenterlight/cms_plugins.py | 24 ++++++++++++++++- datacenterlight/forms.py | 26 +++++++++++++++++-- .../datacenterlight/contact_form.html | 13 ++++++++-- datacenterlight/utils.py | 9 +++++++ datacenterlight/views.py | 5 ++++ 5 files changed, 72 insertions(+), 5 deletions(-) diff --git a/datacenterlight/cms_plugins.py b/datacenterlight/cms_plugins.py index 52b4f19f..ffecbf0b 100644 --- a/datacenterlight/cms_plugins.py +++ b/datacenterlight/cms_plugins.py @@ -1,6 +1,7 @@ from cms.plugin_base import CMSPluginBase from cms.plugin_pool import plugin_pool from django.conf import settings +from django.utils.translation import ugettext_lazy as _ from .cms_models import ( DCLBannerItemPluginModel, DCLBannerListPluginModel, DCLContactPluginModel, @@ -10,7 +11,9 @@ from .cms_models import ( DCLSectionPromoPluginModel, DCLCalculatorPluginModel ) from .models import VMTemplate -from datacenterlight.utils import clear_all_session_vars +from datacenterlight.utils import clear_all_session_vars, get_ip_address + +import ipaddress @plugin_pool.register_plugin @@ -170,6 +173,25 @@ class DCLContactPlugin(CMSPluginBase): render_template = "datacenterlight/cms/contact.html" cache = False + def render(self, context, instance, placeholder): + context = super(DCLContactPlugin, self).render( + context, instance, placeholder + ) + request_ip_address = get_ip_address(context['request']) + request_v4v6 = '' + if (type(ipaddress.ip_address(request_ip_address)) == + ipaddress.IPv4Address): + request_v4v6 = 'IPv4' + elif (type(ipaddress.ip_address(request_ip_address)) == + ipaddress.IPv6Address): + request_v4v6 = 'IPv6' + context['ip_address_label_title'] = _( + "You are requesting this website from " + "{ip_address}.".format(ip_address=request_ip_address)) + context['ip_address_label'] = _( + "Enter '{v4v6}':".format(v4v6=request_v4v6)) + return context + @plugin_pool.register_plugin class DCLFooterPlugin(CMSPluginBase): diff --git a/datacenterlight/forms.py b/datacenterlight/forms.py index b697f694..27a0fbb5 100644 --- a/datacenterlight/forms.py +++ b/datacenterlight/forms.py @@ -1,9 +1,31 @@ from django import forms - +from django.utils.translation import ugettext_lazy as _ +from datacenterlight.utils import get_ip_address from .models import ContactUs +import ipaddress + class ContactForm(forms.ModelForm): class Meta: - fields = ['name', 'email', 'message'] + fields = ['name', 'email', 'message', 'ip_address'] model = ContactUs + + 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")) diff --git a/datacenterlight/templates/datacenterlight/contact_form.html b/datacenterlight/templates/datacenterlight/contact_form.html index 87848ff2..26170bd8 100644 --- a/datacenterlight/templates/datacenterlight/contact_form.html +++ b/datacenterlight/templates/datacenterlight/contact_form.html @@ -26,7 +26,7 @@
- +
{{contact_form.name.errors}}
@@ -34,7 +34,7 @@
- +
{{contact_form.email.errors}}
@@ -47,6 +47,15 @@ {{contact_form.message.errors}}
+
 
{{ ip_address_label_title }}
+
+ +
+ +
+ {{contact_form.ip_address.errors}} +
+
{% trans "Sorry, there was an unexpected error. Kindly retry." %}
diff --git a/datacenterlight/utils.py b/datacenterlight/utils.py index 6a0e45ca..435de23c 100644 --- a/datacenterlight/utils.py +++ b/datacenterlight/utils.py @@ -303,3 +303,12 @@ def create_tax_id(stripe_customer_id, billing_address_id, type, billing_address.vat_validation_status = tax_id_obj.verification.status billing_address.save() return tax_id_obj + + +def get_ip_address(request): + x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') + if x_forwarded_for: + ip = x_forwarded_for.split(',')[-1].strip() + else: + ip = request.META.get('REMOTE_ADDR') + return ip \ No newline at end of file diff --git a/datacenterlight/views.py b/datacenterlight/views.py index e149bfc4..f63d93a3 100644 --- a/datacenterlight/views.py +++ b/datacenterlight/views.py @@ -49,6 +49,11 @@ class ContactUsView(FormView): template_name = "datacenterlight/contact_form.html" form_class = ContactForm + def get_form_kwargs(self): + kw = super(ContactUsView, self).get_form_kwargs() + kw['request'] = self.request + return kw + def get(self, request, *args, **kwargs): return HttpResponseRedirect(reverse('datacenterlight:index'))