Compare commits
3 commits
master
...
8773/add-c
Author | SHA1 | Date | |
---|---|---|---|
|
73c53a7a61 | ||
|
409405f296 | ||
|
e68e0b32f8 |
7 changed files with 117 additions and 5 deletions
|
@ -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):
|
||||
|
|
|
@ -1,9 +1,42 @@
|
|||
from django import forms
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from datacenterlight.utils import get_ip_address
|
||||
|
||||
import logging
|
||||
|
||||
from .models import ContactUs
|
||||
|
||||
import ipaddress
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ContactForm(forms.ModelForm):
|
||||
class Meta:
|
||||
fields = ['name', 'email', 'message']
|
||||
fields = ['name', 'email', 'message', 'ip_address']
|
||||
model = ContactUs
|
||||
labels = {
|
||||
"ip_address": _("You are requesting this website from "
|
||||
"{{ip_address}}. Enter 'IPv{{v4v6}}':".format(
|
||||
ip_address='test', v4v6='4'
|
||||
))
|
||||
}
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.request = kwargs.pop('request', None)
|
||||
super(ContactForm, self).__init__(*args, **kwargs)
|
||||
self.fields['ip_address'] = forms.CharField(required=True)
|
||||
|
||||
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"))
|
||||
|
|
20
datacenterlight/migrations/0032_contactus_ip_address.py
Normal file
20
datacenterlight/migrations/0032_contactus_ip_address.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.4 on 2021-02-11 10:12
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('datacenterlight', '0031_vmpricing_stripe_coupon_id'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='contactus',
|
||||
name='ip_address',
|
||||
field=models.TextField(default='', null=True),
|
||||
),
|
||||
]
|
|
@ -118,6 +118,7 @@ class ContactUs(models.Model):
|
|||
name = models.CharField(max_length=250)
|
||||
email = models.CharField(max_length=250)
|
||||
message = models.TextField()
|
||||
ip_address = models.TextField(default='', null=True)
|
||||
field = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
def __str__(self):
|
||||
|
|
|
@ -26,7 +26,7 @@
|
|||
<div class="form-group">
|
||||
<label class="control-label col-sm-2" for="name">{% trans "Name" %}</label>
|
||||
<div class="col-sm-10">
|
||||
<input type="text" name="name" class="form-control" data-minlength="3" data-error="{% trans 'Please enter your name.' %}" required>
|
||||
<input id="name" type="text" name="name" class="form-control" data-minlength="3" data-error="{% trans 'Please enter your name.' %}" required value="{{contact_form.name.value}}">
|
||||
<div class="help-block with-errors"></div>
|
||||
{{contact_form.name.errors}}
|
||||
</div>
|
||||
|
@ -34,7 +34,7 @@
|
|||
<div class="form-group">
|
||||
<label class="control-label col-sm-2" for="email">{% trans "Email" %}</label>
|
||||
<div class="col-sm-10">
|
||||
<input name="email" type="email" pattern="^[^@\s]+@([^@\s]+\.)+[^@\s]+$" class="form-control" data-error="{% trans 'Please enter a valid email address.' %}" required>
|
||||
<input name="email" type="email" id="email" pattern="^[^@\s]+@([^@\s]+\.)+[^@\s]+$" class="form-control" data-error="{% trans 'Please enter a valid email address.' %}" required value="{{contact_form.email.value}}">
|
||||
<div class="help-block with-errors"></div>
|
||||
{{contact_form.email.errors}}
|
||||
</div>
|
||||
|
@ -42,11 +42,19 @@
|
|||
<div class="form-group">
|
||||
<label class="control-label col-sm-2" for="message">{% trans "Message" %}</label>
|
||||
<div class="col-sm-10">
|
||||
<textarea class="form-control" name="message" id="message" rows="6" required></textarea>
|
||||
<textarea class="form-control" name="message" id="message" rows="6" required>{{contact_form.message.value}}</textarea>
|
||||
<div class="help-block with-errors"></div>
|
||||
{{contact_form.message.errors}}
|
||||
</div>
|
||||
</div>
|
||||
<div class="row"><div class="col-sm-2"> </div><div class="col-sm-10">{{ ip_address_label_title }}{{contact_form.message.errors}}</div></div>
|
||||
<div class="form-group">
|
||||
<label class="control-label col-sm-2" for="ip_address" style="white-space: nowrap;" >{{ ip_address_label }}</label>
|
||||
<div class="col-sm-10">
|
||||
<input name="ip_address" id="ip_address" type="text" class="form-control"data-error="{% trans 'Please enter the text IPv4 or IPv6 based on your IP address.' %}" required value="{{contact_form.ip_address.value}}">
|
||||
<div class="help-block with-errors">{{contact_form.ip_address.errors}}</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-2 col-sm-10 text-right">
|
||||
<div class="form-error hide">{% trans "Sorry, there was an unexpected error. Kindly retry." %}</div>
|
||||
|
|
|
@ -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
|
|
@ -49,11 +49,30 @@ 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'))
|
||||
|
||||
def form_invalid(self, form):
|
||||
if self.request.is_ajax():
|
||||
logger.debug(form)
|
||||
# request_ip_address = get_ip_address(self.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 self.render_to_response(
|
||||
self.get_context_data(contact_form=form))
|
||||
else:
|
||||
|
|
Loading…
Reference in a new issue