Merge pull request #418 from pcoder/task/3615/decouple_landing_page_from_opennebula

Task/3615/decouple landing page from opennebula
This commit is contained in:
Pcoder 2017-07-27 23:34:49 +02:00 committed by GitHub
commit 53fc982837
6 changed files with 99 additions and 72 deletions

View file

@ -0,0 +1,29 @@
from django.core.management.base import BaseCommand
from opennebula_api.models import OpenNebulaManager
from datacenterlight.models import VMTemplate
import logging
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Fetches the VM templates from OpenNebula and populates the dcl VMTemplate model'
def handle(self, *args, **options):
try:
manager = OpenNebulaManager()
templates = manager.get_templates()
dcl_vm_templates = []
for template in templates:
template_name = template.name.strip('public-')
template_id = template.id
dcl_vm_template = VMTemplate.create(template_name, template_id)
dcl_vm_templates.append(dcl_vm_template)
old_vm_templates = VMTemplate.objects.all()
old_vm_templates.delete()
for dcl_vm_template in dcl_vm_templates:
dcl_vm_template.save()
except Exception as e:
logger.error('Error connecting to OpenNebula. Error Details: {err}'.format(err=str(e)))

View file

@ -15,6 +15,7 @@ class BetaAccessVMType(models.Model):
class BetaAccess(models.Model):
email = models.CharField(max_length=250)
name = models.CharField(max_length=250)
# vm = models.ForeignKey(BetaAccessVM)
def __str__(self):
@ -48,3 +49,13 @@ class BetaAccessVM(models.Model):
amount=vm[VM_AMOUNT], type=vm_type))
return created_vms
class VMTemplate(models.Model):
name = models.CharField(max_length=50)
opennebula_vm_template_id = models.IntegerField()
@classmethod
def create(cls, name, opennebula_vm_template_id):
vm_template = cls(name=name, opennebula_vm_template_id=opennebula_vm_template_id)
return vm_template

View file

@ -18,7 +18,8 @@
<div class="form-group">
<div class="description input">
<i class="fa fa-minus-circle left" data-minus="cpu" aria-hidden="true"></i>
<input class="input-price select-number" type="number" min="1" max="48" id="coreValue" name="cpu" data-error="{% trans 'Please enter a value in range 1 - 48.' %}" required>
<input class="input-price select-number" type="number" min="1" max="48" id="coreValue" name="cpu"
data-error="{% trans 'Please enter a value in range 1 - 48.' %}" required>
<span> Core</span>
<i class="fa fa-plus-circle right" data-plus="cpu" aria-hidden="true"></i>
</div>
@ -35,7 +36,8 @@
<div class="form-group">
<div class="description input">
<i class="fa fa-minus-circle left" data-minus="ram" aria-hidden="true"></i>
<input id="ramValue" class="input-price select-number" type="number" min="2" max="200" name="ram" data-error="{% trans 'Please enter a value in range 2 - 200.' %}" required>
<input id="ramValue" class="input-price select-number" type="number" min="2" max="200" name="ram"
data-error="{% trans 'Please enter a value in range 2 - 200.' %}" required>
<span> GB RAM</span>
<i class="fa fa-plus-circle right" data-plus="ram" aria-hidden="true"></i>
</div>
@ -52,7 +54,8 @@
<div class="form-group">
<div class="description input">
<i class="fa fa-minus-circle left" data-minus="storage" aria-hidden="true"></i>
<input id="storageValue" class="input-price select-number" type="number" min="10" max="2000" step="10" name="storage" data-error="{% trans 'Please enter a value in range 10 - 2000.' %}" required>
<input id="storageValue" class="input-price select-number" type="number" min="10" max="2000" step="10"
name="storage" data-error="{% trans 'Please enter a value in range 10 - 2000.' %}" required>
<span>{% trans "GB Storage (SSD)" %}</span>
<i class="fa fa-plus-circle right" data-plus="storage" aria-hidden="true"></i>
</div>
@ -70,7 +73,7 @@
<label for="config">OS</label>
<select name="config" id="">
{% for template in templates %}
<option value="{{template.id}}">{{template.name}} </option>
<option value="{{template.opennebula_vm_template_id}}">{{template.name}}</option>
{% endfor %}
</select>
</div>
@ -81,14 +84,17 @@
<div class="form-group">
<div class="description input justify-center">
<label for="name" class="control-label">{% trans "Name"%}</label>
<input type="text" name="name" class="form-control" placeholder="{% trans 'Your Name'%}" data-minlength="3" data-error="{% trans 'Please enter your name.' %}" required>
<input type="text" name="name" class="form-control" placeholder="{% trans 'Your Name'%}"
data-minlength="3" data-error="{% trans 'Please enter your name.' %}" required>
</div>
<div class="help-block with-errors">
{% for message in messages %}
{% if 'name' in message.tags %}
<ul class="list-unstyled"><li>
<ul class="list-unstyled">
<li>
{{ message|safe }}
</li></ul>
</li>
</ul>
{% endif %}
{% endfor %}
</div>
@ -96,14 +102,18 @@
<div class="form-group">
<div class="description input justify-center">
<label for="email" class="control-label">{% trans "Email"%}</label>
<input name="email" type="email" pattern="^[^@\s]+@([^@\s]+\.)+[^@\s]+$" class="form-control" placeholder="{% trans 'Your Email' %}" data-error="{% trans 'Please enter a valid email address.' %}" required>
<input name="email" type="email" pattern="^[^@\s]+@([^@\s]+\.)+[^@\s]+$" class="form-control"
placeholder="{% trans 'Your Email' %}"
data-error="{% trans 'Please enter a valid email address.' %}" required>
</div>
<div class="help-block with-errors">
{% for message in messages %}
{% if 'email' in message.tags %}
<ul class="list-unstyled"><li>
<ul class="list-unstyled">
<li>
{{ message|safe }}
</li></ul>
</li>
</ul>
{% endif %}
{% endfor %}
</div>

View file

@ -1,7 +1,7 @@
from django.views.generic import FormView, CreateView, TemplateView, DetailView
from django.http import HttpResponseRedirect
from .forms import BetaAccessForm
from .models import BetaAccess, BetaAccessVMType, BetaAccessVM
from .models import BetaAccess, BetaAccessVMType, BetaAccessVM, VMTemplate
from django.contrib import messages
from django.core.urlresolvers import reverse
from django.core.mail import EmailMessage
@ -21,7 +21,7 @@ from datetime import datetime
from membership.models import CustomUser, StripeCustomer
from opennebula_api.models import OpenNebulaManager
from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer
from opennebula_api.serializers import VirtualMachineTemplateSerializer, VirtualMachineSerializer, VMTemplateSerializer
class LandingProgramView(TemplateView):
@ -211,19 +211,10 @@ class IndexView(CreateView):
for session_var in ['specs', 'user', 'billing_address_data']:
if session_var in request.session:
del request.session[session_var]
try:
manager = OpenNebulaManager()
templates = manager.get_templates()
vm_templates = VMTemplate.objects.all()
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'
'templates': vm_templates
}
return render(request, self.template_name, context)
@ -236,9 +227,8 @@ class IndexView(CreateView):
storage_field = forms.IntegerField(validators=[self.validate_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
template = VMTemplate.objects.filter(opennebula_vm_template_id=template_id).first()
template_data = VMTemplateSerializer(template).data
name = request.POST.get('name')
email = request.POST.get('email')
@ -350,25 +340,6 @@ class WhyDataCenterLightView(IndexView):
template_name = "datacenterlight/whydatacenterlight.html"
model = BetaAccess
@cache_control(no_cache=True, must_revalidate=True, no_store=True)
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)
class PaymentOrderView(FormView):
template_name = 'hosting/payment.html'

View file

@ -325,7 +325,7 @@ class OpenNebulaManager():
public_templates = [
template
for template in self._get_template_pool()
if 'public-' in template.name
if template.name.startswith('public-')
]
return public_templates
except ConnectionRefusedError:

View file

@ -129,6 +129,12 @@ class VirtualMachineSerializer(serializers.Serializer):
return obj.name.strip('public-')
class VMTemplateSerializer(serializers.Serializer):
"""Serializer to map the VMTemplate instance into JSON format."""
id = serializers.IntegerField(read_only=True, source='opennebula_vm_template_id')
name = serializers.CharField(read_only=True)
def hexstr2int(string):
return int(string.replace(':', ''), 16)