Added functions to handle get and post requests. On get, we need to create a opennebula connection to fetch the templates. On successful post of the form, we need to send email and redirect user to order-success
This commit is contained in:
parent
47d882d1fc
commit
880e0f0dde
1 changed files with 65 additions and 0 deletions
|
@ -240,6 +240,71 @@ class IndexView(CreateView):
|
|||
success_url = "/datacenterlight#requestform"
|
||||
success_message = "Thank you, we will contact you as soon as possible"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
try:
|
||||
manager = OpenNebulaManager()
|
||||
templates = manager.get_templates()
|
||||
context = {
|
||||
'templates': VirtualMachineTemplateSerializer(templates, many=True).data,
|
||||
}
|
||||
except:
|
||||
messages.error( request,
|
||||
'We have a temporary problem to connect to our backend. \
|
||||
Please try again in a few minutes'
|
||||
)
|
||||
context = {
|
||||
'error' : 'connection'
|
||||
}
|
||||
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)
|
||||
template_data = VirtualMachineTemplateSerializer(template).data
|
||||
|
||||
name = request.POST.get('name')
|
||||
email = request.POST.get('email')
|
||||
name_field = forms.CharField()
|
||||
email_field = forms.EmailField()
|
||||
try:
|
||||
name = name_field.clean(name)
|
||||
except ValidationError as err:
|
||||
messages.add_message(self.request, messages.ERROR, '%(value) is not a proper name.'.format(name))
|
||||
return HttpResponseRedirect(reverse('datacenterlight:order'))
|
||||
|
||||
try:
|
||||
email = email_field.clean(email)
|
||||
except ValidationError as err:
|
||||
messages.add_message(self.request, messages.ERROR, '%(value) is not a proper email.'.format(email))
|
||||
return HttpResponseRedirect(reverse('datacenterlight:order'))
|
||||
|
||||
context = {
|
||||
'base_url': "{0}://{1}".format(self.request.scheme, self.request.get_host()),
|
||||
'name': name,
|
||||
'email': email,
|
||||
'cores': cores,
|
||||
'memory': memory,
|
||||
'storage': storage,
|
||||
'price': price,
|
||||
'template': template_data['name'],
|
||||
}
|
||||
email_data = {
|
||||
'subject': 'New Order Received',
|
||||
'to': 'info@ungleich.ch',
|
||||
'context': context,
|
||||
'template_name': 'new_order_notification',
|
||||
'template_path': 'datacenterlight/emails/'
|
||||
}
|
||||
email = BaseEmail(**email_data)
|
||||
email.send()
|
||||
|
||||
return HttpResponseRedirect(reverse('datacenterlight:order_success'))
|
||||
|
||||
def get_success_url(self):
|
||||
success_url = reverse('datacenterlight:index')
|
||||
success_url += "#requestform"
|
||||
|
|
Loading…
Reference in a new issue