2017-06-22 07:51:07 +00:00
|
|
|
from django.views.generic import FormView, CreateView, TemplateView, DetailView
|
2017-04-23 21:31:05 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
2016-12-20 23:05:20 +00:00
|
|
|
from .forms import BetaAccessForm
|
2017-07-26 18:39:51 +00:00
|
|
|
from .models import BetaAccess, BetaAccessVMType, BetaAccessVM, VMTemplate
|
2016-12-20 23:05:20 +00:00
|
|
|
from django.contrib import messages
|
2017-08-17 16:24:10 +00:00
|
|
|
from django.core.urlresolvers import reverse
|
2017-06-09 22:33:26 +00:00
|
|
|
from django.core.mail import EmailMessage
|
2017-04-23 21:31:05 +00:00
|
|
|
from utils.mailer import BaseEmail
|
2017-08-17 16:24:10 +00:00
|
|
|
from django.shortcuts import render
|
2017-05-23 17:58:29 +00:00
|
|
|
from django.shortcuts import redirect
|
2017-06-05 21:39:34 +00:00
|
|
|
from django import forms
|
|
|
|
from django.core.exceptions import ValidationError
|
2017-08-17 16:24:10 +00:00
|
|
|
from django.views.decorators.cache import cache_control
|
2017-06-21 07:44:58 +00:00
|
|
|
from django.conf import settings
|
2017-07-01 12:32:10 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2017-06-21 07:44:58 +00:00
|
|
|
from utils.forms import BillingAddressForm, UserBillingAddressForm
|
2017-06-29 07:38:24 +00:00
|
|
|
from utils.models import BillingAddress
|
2017-06-21 07:44:58 +00:00
|
|
|
from hosting.models import HostingOrder, HostingBill
|
|
|
|
from utils.stripe_utils import StripeUtils
|
|
|
|
from datetime import datetime
|
2017-06-30 06:57:18 +00:00
|
|
|
from membership.models import CustomUser, StripeCustomer
|
2017-05-20 16:06:10 +00:00
|
|
|
from opennebula_api.models import OpenNebulaManager
|
2017-07-27 06:35:01 +00:00
|
|
|
from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer, VMTemplateSerializer
|
2017-04-23 21:31:05 +00:00
|
|
|
|
2017-07-03 21:53:37 +00:00
|
|
|
|
2017-04-23 21:31:05 +00:00
|
|
|
class LandingProgramView(TemplateView):
|
|
|
|
template_name = "datacenterlight/landing.html"
|
|
|
|
|
2017-07-03 21:53:37 +00:00
|
|
|
|
2017-06-05 22:13:50 +00:00
|
|
|
class SuccessView(TemplateView):
|
|
|
|
template_name = "datacenterlight/success.html"
|
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
if 'specs' not in request.session or 'user' not in request.session:
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
2017-07-07 14:49:22 +00:00
|
|
|
|
2017-06-30 06:38:07 +00:00
|
|
|
elif 'token' not in request.session:
|
2017-07-01 13:53:25 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:payment'))
|
2017-06-30 06:38:07 +00:00
|
|
|
elif 'order_confirmation' not in request.session:
|
2017-07-01 13:53:25 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:order_confirmation'))
|
2017-06-30 06:38:07 +00:00
|
|
|
else:
|
2017-07-01 13:53:25 +00:00
|
|
|
for session_var in ['specs', 'user', 'template', 'billing_address', 'billing_address_data',
|
|
|
|
'token', 'customer']:
|
2017-06-30 06:38:07 +00:00
|
|
|
if session_var in request.session:
|
|
|
|
del request.session[session_var]
|
2017-06-21 07:44:58 +00:00
|
|
|
return render(request, self.template_name)
|
|
|
|
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-05-23 04:35:29 +00:00
|
|
|
class PricingView(TemplateView):
|
|
|
|
template_name = "datacenterlight/pricing.html"
|
|
|
|
|
2017-05-23 17:58:29 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
try:
|
|
|
|
manager = OpenNebulaManager()
|
|
|
|
templates = manager.get_templates()
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'templates': VirtualMachineTemplateSerializer(templates, many=True).data,
|
|
|
|
}
|
|
|
|
except:
|
2017-06-29 14:34:40 +00:00
|
|
|
messages.error(request,
|
|
|
|
'We have a temporary problem to connect to our backend. \
|
|
|
|
Please try again in a few minutes'
|
|
|
|
)
|
2017-05-23 17:58:29 +00:00
|
|
|
context = {
|
2017-06-29 14:34:40 +00:00
|
|
|
'error': 'connection'
|
2017-06-29 16:23:25 +00:00
|
|
|
}
|
2017-05-23 17:58:29 +00:00
|
|
|
|
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
|
|
|
|
cores = request.POST.get('cpu')
|
|
|
|
memory = request.POST.get('ram')
|
|
|
|
storage = request.POST.get('storage')
|
|
|
|
price = request.POST.get('total')
|
|
|
|
|
|
|
|
template_id = int(request.POST.get('config'))
|
|
|
|
manager = OpenNebulaManager()
|
|
|
|
template = manager.get_template(template_id)
|
|
|
|
|
2017-08-03 16:30:41 +00:00
|
|
|
request.session['template'] = VirtualMachineTemplateSerializer(
|
|
|
|
template).data
|
2017-05-23 17:58:29 +00:00
|
|
|
|
2017-05-24 15:58:16 +00:00
|
|
|
if not request.user.is_authenticated():
|
|
|
|
request.session['next'] = reverse('hosting:payment')
|
|
|
|
|
2017-06-09 22:33:26 +00:00
|
|
|
request.session['specs'] = {
|
2017-06-29 14:34:40 +00:00
|
|
|
'cpu': cores,
|
2017-05-23 17:58:29 +00:00
|
|
|
'memory': memory,
|
|
|
|
'disk_size': storage,
|
|
|
|
'price': price,
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect(reverse('hosting:payment'))
|
|
|
|
|
|
|
|
|
2017-05-22 16:03:33 +00:00
|
|
|
class BetaAccessView(FormView):
|
|
|
|
template_name = "datacenterlight/beta_access.html"
|
2017-06-03 14:03:02 +00:00
|
|
|
form_class = BetaAccessForm
|
2017-05-22 16:03:33 +00:00
|
|
|
success_message = "Thank you, we will contact you as soon as possible"
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
context = {
|
|
|
|
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host())
|
|
|
|
}
|
|
|
|
|
|
|
|
email_data = {
|
|
|
|
'subject': 'DatacenterLight Beta Access Request',
|
2017-06-07 06:05:16 +00:00
|
|
|
'from_address': '(datacenterlight) datacenterlight Support <support@datacenterlight.ch>',
|
2017-05-22 16:03:33 +00:00
|
|
|
'to': form.cleaned_data.get('email'),
|
2017-06-09 20:36:14 +00:00
|
|
|
'from': '(datacenterlight) DatacenterLight Support support@datacenterlight.ch',
|
2017-05-22 16:03:33 +00:00
|
|
|
'context': context,
|
|
|
|
'template_name': 'request_access_confirmation',
|
|
|
|
'template_path': 'datacenterlight/emails/'
|
|
|
|
}
|
|
|
|
email = BaseEmail(**email_data)
|
|
|
|
email.send()
|
|
|
|
|
|
|
|
context.update({
|
|
|
|
'email': form.cleaned_data.get('email')
|
|
|
|
})
|
|
|
|
|
|
|
|
email_data = {
|
|
|
|
'subject': 'DatacenterLight Beta Access Request',
|
2017-06-07 06:05:16 +00:00
|
|
|
'from_address': '(datacenterlight) datacenterlight Support <support@datacenterlight.ch>',
|
2017-05-22 16:03:33 +00:00
|
|
|
'to': 'info@ungleich.ch',
|
|
|
|
'context': context,
|
|
|
|
'template_name': 'request_access_notification',
|
|
|
|
'template_path': 'datacenterlight/emails/'
|
|
|
|
}
|
|
|
|
email = BaseEmail(**email_data)
|
|
|
|
email.send()
|
|
|
|
|
2017-08-03 16:30:41 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.SUCCESS, self.success_message)
|
2017-05-22 16:03:33 +00:00
|
|
|
return render(self.request, 'datacenterlight/beta_success.html', {})
|
2017-04-23 21:31:05 +00:00
|
|
|
|
2017-06-29 14:34:40 +00:00
|
|
|
|
2017-04-23 21:31:05 +00:00
|
|
|
class BetaProgramView(CreateView):
|
|
|
|
template_name = "datacenterlight/beta.html"
|
|
|
|
model = BetaAccessVM
|
|
|
|
fields = '__all__'
|
|
|
|
# form_class = BetaAccessForm
|
|
|
|
# success_url = "/datacenterlight#requestform"
|
|
|
|
success_message = "Thank you, we will contact you as soon as possible"
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
success_url = reverse('datacenterlight:beta')
|
|
|
|
success_url += "#success"
|
|
|
|
return success_url
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
vms = BetaAccessVMType.objects.all()
|
|
|
|
context = super(BetaProgramView, self).get_context_data(**kwargs)
|
2017-05-20 16:06:10 +00:00
|
|
|
|
|
|
|
# templates = OpenNebulaManager().get_templates()
|
|
|
|
# data = VirtualMachineTemplateSerializer(templates, many=True).data
|
|
|
|
|
2017-04-23 21:31:05 +00:00
|
|
|
context.update({
|
|
|
|
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()),
|
|
|
|
'vms': vms
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
data = request.POST
|
|
|
|
vms = BetaAccessVM.create(data)
|
|
|
|
|
|
|
|
context = {
|
|
|
|
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()),
|
|
|
|
'email': data.get('email'),
|
|
|
|
'name': data.get('name'),
|
|
|
|
'vms': vms
|
|
|
|
}
|
|
|
|
|
|
|
|
email_data = {
|
|
|
|
'subject': 'DatacenterLight Beta Access Request',
|
2017-06-07 06:05:16 +00:00
|
|
|
'from_address': '(datacenterlight) datacenterlight Support <support@datacenterlight.ch>',
|
2017-04-23 21:31:05 +00:00
|
|
|
'to': 'info@ungleich.ch',
|
|
|
|
'context': context,
|
|
|
|
'template_name': 'request_beta_access_notification',
|
|
|
|
'template_path': 'datacenterlight/emails/'
|
|
|
|
}
|
|
|
|
email = BaseEmail(**email_data)
|
|
|
|
email.send()
|
|
|
|
|
2017-08-03 16:30:41 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.SUCCESS, self.success_message)
|
2017-04-23 21:31:05 +00:00
|
|
|
return HttpResponseRedirect(self.get_success_url())
|
2016-12-20 23:05:20 +00:00
|
|
|
|
2017-07-06 19:50:16 +00:00
|
|
|
|
2016-12-20 23:05:20 +00:00
|
|
|
class IndexView(CreateView):
|
|
|
|
template_name = "datacenterlight/index.html"
|
|
|
|
model = BetaAccess
|
|
|
|
form_class = BetaAccessForm
|
|
|
|
success_url = "/datacenterlight#requestform"
|
|
|
|
success_message = "Thank you, we will contact you as soon as possible"
|
2017-06-29 14:34:40 +00:00
|
|
|
|
2017-07-14 08:34:40 +00:00
|
|
|
def validate_cores(self, value):
|
|
|
|
if (value > 48) or (value < 1):
|
2017-07-31 18:31:19 +00:00
|
|
|
raise ValidationError(_('Invalid number of cores'))
|
2017-07-14 08:34:40 +00:00
|
|
|
|
|
|
|
def validate_memory(self, value):
|
|
|
|
if (value > 200) or (value < 2):
|
2017-07-31 18:31:19 +00:00
|
|
|
raise ValidationError(_('Invalid RAM size'))
|
2017-07-14 08:34:40 +00:00
|
|
|
|
|
|
|
def validate_storage(self, value):
|
|
|
|
if (value > 2000) or (value < 10):
|
2017-07-31 18:31:19 +00:00
|
|
|
raise ValidationError(_('Invalid storage size'))
|
2017-07-14 08:34:40 +00:00
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
2017-06-11 11:02:38 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
2017-07-01 12:32:10 +00:00
|
|
|
for session_var in ['specs', 'user', 'billing_address_data']:
|
|
|
|
if session_var in request.session:
|
|
|
|
del request.session[session_var]
|
2017-07-26 18:39:51 +00:00
|
|
|
|
|
|
|
vm_templates = VMTemplate.objects.all()
|
|
|
|
context = {
|
|
|
|
'templates': vm_templates
|
|
|
|
}
|
2017-06-11 11:02:38 +00:00
|
|
|
return render(request, self.template_name, context)
|
|
|
|
|
|
|
|
def post(self, request):
|
|
|
|
cores = request.POST.get('cpu')
|
2017-07-14 08:34:40 +00:00
|
|
|
cores_field = forms.IntegerField(validators=[self.validate_cores])
|
2017-06-11 11:02:38 +00:00
|
|
|
memory = request.POST.get('ram')
|
2017-07-14 08:34:40 +00:00
|
|
|
memory_field = forms.IntegerField(validators=[self.validate_memory])
|
2017-06-11 11:02:38 +00:00
|
|
|
storage = request.POST.get('storage')
|
2017-07-14 08:34:40 +00:00
|
|
|
storage_field = forms.IntegerField(validators=[self.validate_storage])
|
2017-06-11 11:02:38 +00:00
|
|
|
price = request.POST.get('total')
|
|
|
|
template_id = int(request.POST.get('config'))
|
2017-08-03 16:30:41 +00:00
|
|
|
template = VMTemplate.objects.filter(
|
|
|
|
opennebula_vm_template_id=template_id).first()
|
2017-07-27 06:35:01 +00:00
|
|
|
template_data = VMTemplateSerializer(template).data
|
2017-06-29 14:34:40 +00:00
|
|
|
|
2017-06-11 11:02:38 +00:00
|
|
|
name = request.POST.get('name')
|
|
|
|
email = request.POST.get('email')
|
|
|
|
name_field = forms.CharField()
|
|
|
|
email_field = forms.EmailField()
|
2017-07-14 08:34:40 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
cores = cores_field.clean(cores)
|
|
|
|
except ValidationError as err:
|
2017-07-25 20:25:31 +00:00
|
|
|
msg = '{} : {}.'.format(cores, str(err))
|
2017-08-03 16:30:41 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, msg, extra_tags='cores')
|
2017-07-14 08:34:40 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form")
|
|
|
|
|
|
|
|
try:
|
|
|
|
memory = memory_field.clean(memory)
|
|
|
|
except ValidationError as err:
|
2017-07-25 20:25:31 +00:00
|
|
|
msg = '{} : {}.'.format(memory, str(err))
|
2017-08-03 16:30:41 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, msg, extra_tags='memory')
|
2017-07-14 08:34:40 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form")
|
|
|
|
|
|
|
|
try:
|
|
|
|
storage = storage_field.clean(storage)
|
|
|
|
except ValidationError as err:
|
2017-07-25 20:25:31 +00:00
|
|
|
msg = '{} : {}.'.format(storage, str(err))
|
2017-08-03 16:30:41 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, msg, extra_tags='storage')
|
2017-07-14 08:34:40 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form")
|
|
|
|
|
2017-06-11 11:02:38 +00:00
|
|
|
try:
|
|
|
|
name = name_field.clean(name)
|
|
|
|
except ValidationError as err:
|
2017-07-03 21:53:37 +00:00
|
|
|
msg = '{} {}.'.format(name, _('is not a proper name'))
|
2017-08-03 16:30:41 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, msg, extra_tags='name')
|
2017-07-01 12:32:10 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form")
|
2017-06-11 11:02:38 +00:00
|
|
|
|
2017-06-29 14:34:40 +00:00
|
|
|
try:
|
2017-06-11 11:02:38 +00:00
|
|
|
email = email_field.clean(email)
|
|
|
|
except ValidationError as err:
|
2017-07-03 21:53:37 +00:00
|
|
|
msg = '{} {}.'.format(email, _('is not a proper email'))
|
2017-08-03 16:30:41 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.ERROR, msg, extra_tags='email')
|
2017-07-01 12:32:10 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index') + "#order_form")
|
2017-06-11 11:02:38 +00:00
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
specs = {
|
|
|
|
'cpu': cores,
|
2017-06-11 11:02:38 +00:00
|
|
|
'memory': memory,
|
2017-06-21 07:44:58 +00:00
|
|
|
'disk_size': storage,
|
|
|
|
'price': price
|
2017-06-11 11:02:38 +00:00
|
|
|
}
|
2017-06-29 16:23:25 +00:00
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
this_user = {
|
|
|
|
'name': name,
|
|
|
|
'email': email
|
2017-06-11 11:02:38 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
request.session['specs'] = specs
|
|
|
|
request.session['template'] = template_data
|
|
|
|
request.session['user'] = this_user
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:payment'))
|
2016-12-20 23:05:20 +00:00
|
|
|
|
2016-12-21 06:24:04 +00:00
|
|
|
def get_success_url(self):
|
|
|
|
success_url = reverse('datacenterlight:index')
|
|
|
|
success_url += "#requestform"
|
|
|
|
return success_url
|
|
|
|
|
2017-04-23 21:31:05 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(IndexView, self).get_context_data(**kwargs)
|
|
|
|
context.update({
|
|
|
|
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host())
|
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
2016-12-20 23:05:20 +00:00
|
|
|
def form_valid(self, form):
|
2017-04-23 21:31:05 +00:00
|
|
|
|
|
|
|
context = {
|
|
|
|
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host())
|
|
|
|
}
|
|
|
|
|
|
|
|
email_data = {
|
|
|
|
'subject': 'DatacenterLight Beta Access Request',
|
2017-06-07 06:05:16 +00:00
|
|
|
'from_address': '(datacenterlight) datacenterlight Support <support@datacenterlight.ch>',
|
2017-04-23 21:31:05 +00:00
|
|
|
'to': form.cleaned_data.get('email'),
|
2017-06-09 20:36:14 +00:00
|
|
|
'from': '(datacenterlight) DatacenterLight Support support@datacenterlight.ch',
|
2017-04-23 21:31:05 +00:00
|
|
|
'context': context,
|
|
|
|
'template_name': 'request_access_confirmation',
|
|
|
|
'template_path': 'datacenterlight/emails/'
|
|
|
|
}
|
|
|
|
email = BaseEmail(**email_data)
|
|
|
|
email.send()
|
|
|
|
|
|
|
|
context.update({
|
|
|
|
'email': form.cleaned_data.get('email')
|
|
|
|
})
|
|
|
|
|
|
|
|
email_data = {
|
|
|
|
'subject': 'DatacenterLight Beta Access Request',
|
2017-06-07 06:05:16 +00:00
|
|
|
'from_address': '(datacenterlight) datacenterlight Support <support@datacenterlight.ch>',
|
2017-04-23 21:31:05 +00:00
|
|
|
'to': 'info@ungleich.ch',
|
|
|
|
'context': context,
|
|
|
|
'template_name': 'request_access_notification',
|
|
|
|
'template_path': 'datacenterlight/emails/'
|
|
|
|
}
|
|
|
|
email = BaseEmail(**email_data)
|
|
|
|
email.send()
|
|
|
|
|
2017-08-03 16:30:41 +00:00
|
|
|
messages.add_message(
|
|
|
|
self.request, messages.SUCCESS, self.success_message)
|
2016-12-20 23:05:20 +00:00
|
|
|
return super(IndexView, self).form_valid(form)
|
2017-06-21 07:44:58 +00:00
|
|
|
|
2017-07-06 19:50:16 +00:00
|
|
|
|
2017-06-29 07:44:13 +00:00
|
|
|
class WhyDataCenterLightView(IndexView):
|
|
|
|
template_name = "datacenterlight/whydatacenterlight.html"
|
|
|
|
model = BetaAccess
|
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
|
|
|
|
class PaymentOrderView(FormView):
|
|
|
|
template_name = 'hosting/payment.html'
|
|
|
|
form_class = BillingAddressForm
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-30 21:41:16 +00:00
|
|
|
def get_form_kwargs(self):
|
|
|
|
form_kwargs = super(PaymentOrderView, self).get_form_kwargs()
|
2017-06-30 22:24:08 +00:00
|
|
|
billing_address_data = self.request.session.get('billing_address_data')
|
|
|
|
if billing_address_data:
|
2017-06-30 21:41:16 +00:00
|
|
|
form_kwargs.update({
|
|
|
|
'initial': {
|
2017-07-20 12:02:59 +00:00
|
|
|
'cardholder_name': billing_address_data['cardholder_name'],
|
2017-06-30 22:24:08 +00:00
|
|
|
'street_address': billing_address_data['street_address'],
|
|
|
|
'city': billing_address_data['city'],
|
|
|
|
'postal_code': billing_address_data['postal_code'],
|
|
|
|
'country': billing_address_data['country'],
|
2017-06-30 21:41:16 +00:00
|
|
|
}
|
|
|
|
})
|
2017-06-30 22:24:08 +00:00
|
|
|
return form_kwargs
|
|
|
|
|
2017-06-21 07:44:58 +00:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(PaymentOrderView, self).get_context_data(**kwargs)
|
|
|
|
context.update({
|
2017-06-30 22:59:11 +00:00
|
|
|
'stripe_key': settings.STRIPE_API_PUBLIC_KEY,
|
|
|
|
'site_url': reverse('datacenterlight:index')
|
2017-06-21 07:44:58 +00:00
|
|
|
})
|
|
|
|
return context
|
|
|
|
|
|
|
|
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
if 'specs' not in request.session or 'user' not in request.session:
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
|
|
|
return self.render_to_response(self.get_context_data())
|
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
form = self.get_form()
|
|
|
|
if form.is_valid():
|
|
|
|
# Get billing address data
|
|
|
|
billing_address_data = form.cleaned_data
|
|
|
|
token = form.cleaned_data.get('token')
|
2017-06-29 07:38:24 +00:00
|
|
|
user = request.session.get('user')
|
2017-06-22 08:10:39 +00:00
|
|
|
try:
|
2017-07-01 13:53:25 +00:00
|
|
|
CustomUser.objects.get(email=user.get('email'))
|
2017-06-22 08:10:39 +00:00
|
|
|
except CustomUser.DoesNotExist:
|
2017-06-22 07:51:07 +00:00
|
|
|
password = CustomUser.get_random_password()
|
|
|
|
# Register the user, and do not send emails
|
2017-06-29 16:23:25 +00:00
|
|
|
CustomUser.register(user.get('name'),
|
|
|
|
password,
|
|
|
|
user.get('email'),
|
|
|
|
app='dcl',
|
2017-06-22 07:51:07 +00:00
|
|
|
base_url=None, send_email=False)
|
2017-06-21 18:07:36 +00:00
|
|
|
|
2017-06-22 07:51:07 +00:00
|
|
|
# Get or create stripe customer
|
2017-06-22 19:01:08 +00:00
|
|
|
customer = StripeCustomer.get_or_create(email=user.get('email'),
|
2017-06-30 06:57:18 +00:00
|
|
|
token=token)
|
2017-06-22 07:51:07 +00:00
|
|
|
if not customer:
|
2017-06-21 07:44:58 +00:00
|
|
|
form.add_error("__all__", "Invalid credit card")
|
|
|
|
return self.render_to_response(self.get_context_data(form=form))
|
2017-06-21 18:07:36 +00:00
|
|
|
|
2017-06-22 07:51:07 +00:00
|
|
|
# Create Billing Address
|
2017-06-21 07:44:58 +00:00
|
|
|
billing_address = form.save()
|
2017-06-22 07:51:07 +00:00
|
|
|
|
2017-06-29 07:38:24 +00:00
|
|
|
request.session['billing_address_data'] = billing_address_data
|
|
|
|
request.session['billing_address'] = billing_address.id
|
|
|
|
request.session['token'] = token
|
|
|
|
request.session['customer'] = customer.id
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:order_confirmation'))
|
2017-06-21 07:44:58 +00:00
|
|
|
else:
|
|
|
|
return self.form_invalid(form)
|
2017-06-22 07:51:07 +00:00
|
|
|
|
2017-06-29 16:23:25 +00:00
|
|
|
|
2017-06-22 07:51:07 +00:00
|
|
|
class OrderConfirmationView(DetailView):
|
|
|
|
template_name = "datacenterlight/order_detail.html"
|
2017-06-30 06:38:07 +00:00
|
|
|
payment_template_name = 'hosting/payment.html'
|
2017-06-22 07:51:07 +00:00
|
|
|
context_object_name = "order"
|
|
|
|
model = HostingOrder
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-30 06:38:07 +00:00
|
|
|
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
2017-06-29 07:38:24 +00:00
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
if 'specs' not in request.session or 'user' not in request.session:
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:index'))
|
2017-06-30 06:38:07 +00:00
|
|
|
if 'token' not in request.session:
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:payment'))
|
2017-06-29 07:38:24 +00:00
|
|
|
stripe_customer_id = request.session.get('customer')
|
|
|
|
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
|
2017-06-30 05:36:58 +00:00
|
|
|
stripe_utils = StripeUtils()
|
2017-08-03 16:30:41 +00:00
|
|
|
card_details = stripe_utils.get_card_details(
|
|
|
|
customer.stripe_id, request.session.get('token'))
|
2017-08-03 09:47:34 +00:00
|
|
|
if not card_details.get('response_object') and not card_details.get('paid'):
|
2017-08-04 08:01:32 +00:00
|
|
|
msg = card_details.get('error')
|
2017-08-03 09:47:34 +00:00
|
|
|
messages.add_message(self.request, messages.ERROR, msg, extra_tags='failed_payment')
|
2017-08-07 14:23:58 +00:00
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:payment') + '#payment_error')
|
2017-08-04 08:05:37 +00:00
|
|
|
|
2017-06-30 05:36:58 +00:00
|
|
|
context = {
|
2017-06-30 22:59:11 +00:00
|
|
|
'site_url': reverse('datacenterlight:index'),
|
2017-07-01 13:53:25 +00:00
|
|
|
'cc_last4': card_details.get('response_object').get('last4'),
|
|
|
|
'cc_brand': card_details.get('response_object').get('brand')
|
2017-06-30 05:36:58 +00:00
|
|
|
}
|
|
|
|
return render(request, self.template_name, context)
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-29 07:38:24 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
template = request.session.get('template')
|
|
|
|
specs = request.session.get('specs')
|
|
|
|
user = request.session.get('user')
|
2017-06-30 06:38:07 +00:00
|
|
|
stripe_customer_id = request.session.get('customer')
|
|
|
|
customer = StripeCustomer.objects.filter(id=stripe_customer_id).first()
|
2017-06-29 07:38:24 +00:00
|
|
|
billing_address_data = request.session.get('billing_address_data')
|
|
|
|
billing_address_id = request.session.get('billing_address')
|
2017-08-03 16:30:41 +00:00
|
|
|
billing_address = BillingAddress.objects.filter(
|
|
|
|
id=billing_address_id).first()
|
2017-06-29 07:38:24 +00:00
|
|
|
vm_template_id = template.get('id', 1)
|
|
|
|
final_price = specs.get('price')
|
|
|
|
|
|
|
|
# Make stripe charge to a customer
|
|
|
|
stripe_utils = StripeUtils()
|
2017-08-17 16:24:10 +00:00
|
|
|
cpu = specs.get('cpu')
|
|
|
|
memory = specs.get('memory')
|
|
|
|
disk_size = specs.get('disk_size')
|
|
|
|
amount_to_be_charged = (cpu * 5) + (memory * 2) + (disk_size * 0.6)
|
|
|
|
plan_name = "{cpu} Cores, {memory} GB RAM, {disk_size} GB SSD".format(cpu=cpu,
|
|
|
|
memory=memory,
|
|
|
|
disk_size=disk_size)
|
|
|
|
stripe_plan_id = StripeUtils.get_stripe_plan_id(cpu=cpu,
|
|
|
|
ram=memory,
|
|
|
|
ssd=disk_size,
|
|
|
|
version=1,
|
|
|
|
app='dcl')
|
|
|
|
stripe_plan = stripe_utils.get_or_create_stripe_plan(amount=amount_to_be_charged,
|
|
|
|
name=plan_name,
|
|
|
|
stripe_plan_id=stripe_plan_id)
|
|
|
|
subscription_result = stripe_utils.subscribe_customer_to_plan(customer.stripe_id,
|
|
|
|
[{"plan": stripe_plan.get(
|
|
|
|
'response_object').stripe_plan_id}])
|
|
|
|
response_object = subscription_result.get('response_object')
|
2017-08-17 16:31:27 +00:00
|
|
|
if response_object is None or response_object.status is not 'active':
|
2017-07-01 13:53:25 +00:00
|
|
|
context = {}
|
2017-06-29 07:38:24 +00:00
|
|
|
context.update({
|
2017-08-17 16:24:10 +00:00
|
|
|
'paymentError': response_object.get('error')
|
2017-06-29 07:38:24 +00:00
|
|
|
})
|
2017-06-30 06:38:07 +00:00
|
|
|
return render(request, self.payment_template_name, context)
|
2017-06-29 07:38:24 +00:00
|
|
|
|
|
|
|
# Create OpenNebulaManager
|
2017-06-22 07:51:07 +00:00
|
|
|
manager = OpenNebulaManager(email=settings.OPENNEBULA_USERNAME,
|
|
|
|
password=settings.OPENNEBULA_PASSWORD)
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-30 06:38:07 +00:00
|
|
|
# Create a vm using oneadmin, also specify the name
|
2017-06-29 07:38:24 +00:00
|
|
|
vm_id = manager.create_vm(
|
|
|
|
template_id=vm_template_id,
|
|
|
|
specs=specs,
|
2017-08-14 20:59:01 +00:00
|
|
|
ssh_key=settings.ONEADMIN_USER_SSH_PUBLIC_KEY,
|
2017-06-29 07:38:24 +00:00
|
|
|
vm_name="{email}-{template_name}-{date}".format(
|
2017-07-26 18:39:51 +00:00
|
|
|
email=user.get('email'),
|
|
|
|
template_name=template.get('name'),
|
|
|
|
date=int(datetime.now().strftime("%s")))
|
2017-06-29 07:38:24 +00:00
|
|
|
)
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-29 07:38:24 +00:00
|
|
|
# Create a Hosting Order
|
|
|
|
order = HostingOrder.create(
|
|
|
|
price=final_price,
|
|
|
|
vm_id=vm_id,
|
|
|
|
customer=customer,
|
|
|
|
billing_address=billing_address
|
|
|
|
)
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-29 07:38:24 +00:00
|
|
|
# Create a Hosting Bill
|
2017-07-01 13:53:25 +00:00
|
|
|
HostingBill.create(
|
2017-06-29 07:38:24 +00:00
|
|
|
customer=customer, billing_address=billing_address)
|
|
|
|
|
|
|
|
# Create Billing Address for User if he does not have one
|
|
|
|
if not customer.user.billing_addresses.count():
|
|
|
|
billing_address_data.update({
|
|
|
|
'user': customer.user.id
|
|
|
|
})
|
|
|
|
billing_address_user_form = UserBillingAddressForm(
|
|
|
|
billing_address_data)
|
|
|
|
billing_address_user_form.is_valid()
|
|
|
|
billing_address_user_form.save()
|
|
|
|
|
2017-08-17 16:24:10 +00:00
|
|
|
# Associate an order with a stripe subscription
|
|
|
|
order.set_subscription_id(response_object)
|
2017-06-29 07:38:24 +00:00
|
|
|
|
2017-08-17 16:24:10 +00:00
|
|
|
# If the Stripe payment succeeded, set order status approved
|
2017-06-29 07:38:24 +00:00
|
|
|
order.set_approved()
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-29 07:38:24 +00:00
|
|
|
vm = VirtualMachineSerializer(manager.get_vm(vm_id)).data
|
2017-07-01 13:53:25 +00:00
|
|
|
|
2017-06-29 07:38:24 +00:00
|
|
|
context = {
|
|
|
|
'name': user.get('name'),
|
|
|
|
'email': user.get('email'),
|
|
|
|
'cores': specs.get('cpu'),
|
|
|
|
'memory': specs.get('memory'),
|
|
|
|
'storage': specs.get('disk_size'),
|
|
|
|
'price': specs.get('price'),
|
|
|
|
'template': template.get('name'),
|
|
|
|
'vm.name': vm['name'],
|
|
|
|
'vm.id': vm['vm_id'],
|
|
|
|
'order.id': order.id
|
|
|
|
}
|
|
|
|
email_data = {
|
2017-06-30 07:17:04 +00:00
|
|
|
'subject': settings.DCL_TEXT + " Order from %s" % context['email'],
|
|
|
|
'from_email': settings.DCL_SUPPORT_FROM_ADDRESS,
|
2017-06-29 07:38:24 +00:00
|
|
|
'to': ['info@ungleich.ch'],
|
|
|
|
'body': "\n".join(["%s=%s" % (k, v) for (k, v) in context.items()]),
|
|
|
|
'reply_to': [context['email']],
|
|
|
|
}
|
|
|
|
email = EmailMessage(**email_data)
|
|
|
|
email.send()
|
2017-06-30 06:38:07 +00:00
|
|
|
request.session['order_confirmation'] = True
|
|
|
|
return HttpResponseRedirect(reverse('datacenterlight:order_success'))
|