319 lines
12 KiB
Python
319 lines
12 KiB
Python
|
import uuid
|
||
|
|
||
|
from django.conf import settings
|
||
|
from django.contrib.auth.tokens import default_token_generator
|
||
|
from django.core.files.base import ContentFile
|
||
|
from django.http import HttpResponseRedirect
|
||
|
from django.urls import reverse_lazy, reverse
|
||
|
from django.utils.decorators import method_decorator
|
||
|
from django.utils.http import urlsafe_base64_decode
|
||
|
from django.utils.safestring import mark_safe
|
||
|
from django.views import View
|
||
|
from django.views.generic import CreateView, TemplateView, FormView
|
||
|
from django.utils.translation import gettext_lazy as _
|
||
|
from django.views.decorators.cache import never_cache, cache_control
|
||
|
from django.contrib import messages
|
||
|
from django.shortcuts import render
|
||
|
|
||
|
from dynamicweb2.ldap_manager import LdapManager
|
||
|
from hosting.forms import HostingUserLoginForm, HostingUserSignupForm, ResendActivationEmailForm, \
|
||
|
PasswordResetRequestForm, UserHostingKeyForm
|
||
|
from hosting.mailer import BaseEmail
|
||
|
from hosting.mixins import LoginViewMixin, ResendActivationLinkViewMixin, PasswordResetViewMixin, \
|
||
|
PasswordResetConfirmViewMixin
|
||
|
from hosting.models import CustomUser, UserHostingKey
|
||
|
|
||
|
decorators = [never_cache]
|
||
|
|
||
|
|
||
|
class LoginView(LoginViewMixin):
|
||
|
template_name = "hosting/login.html"
|
||
|
form_class = HostingUserLoginForm
|
||
|
success_url = reverse_lazy('hosting:dashboard')
|
||
|
|
||
|
|
||
|
class SignupView(CreateView):
|
||
|
template_name = 'hosting/signup.html'
|
||
|
form_class = HostingUserSignupForm
|
||
|
model = CustomUser
|
||
|
success_url = reverse_lazy('hosting:dashboard')
|
||
|
|
||
|
def get_success_url(self):
|
||
|
next_url = self.request.session.get(
|
||
|
'next', self.success_url)
|
||
|
return next_url
|
||
|
|
||
|
def form_valid(self, form):
|
||
|
name = form.cleaned_data.get('name')
|
||
|
email = form.cleaned_data.get('email')
|
||
|
password = form.cleaned_data.get('password')
|
||
|
this_base_url = "{0}://{1}".format(self.request.scheme,
|
||
|
self.request.get_host())
|
||
|
CustomUser.register(name, password, email,
|
||
|
app='dcl', base_url=this_base_url)
|
||
|
|
||
|
return HttpResponseRedirect(reverse_lazy('hosting:signup-validate'))
|
||
|
|
||
|
@method_decorator(decorators)
|
||
|
def get(self, request, *args, **kwargs):
|
||
|
if self.request.user.is_authenticated:
|
||
|
return HttpResponseRedirect(self.get_success_url())
|
||
|
return super(SignupView, self).get(request, *args, **kwargs)
|
||
|
|
||
|
|
||
|
class SignupValidateView(TemplateView):
|
||
|
template_name = "hosting/signup_validate.html"
|
||
|
|
||
|
def get_context_data(self, **kwargs):
|
||
|
context = super(SignupValidateView, self).get_context_data(**kwargs)
|
||
|
login_url = '<a href="' + \
|
||
|
reverse('hosting:login') + '">' + str(_('login')) + '</a>'
|
||
|
home_url = '<a href="' + \
|
||
|
reverse('datacenterlight:index') + '">Data Center Light</a>'
|
||
|
message = '{signup_success_message} {lurl}</a> \
|
||
|
<br />{go_back} {hurl}.'.format(
|
||
|
signup_success_message=_(
|
||
|
'Thank you for signing up. We have sent an email to you. '
|
||
|
'Please follow the instructions in it to activate your '
|
||
|
'account. Once activated, you can login using'),
|
||
|
go_back=_('Go back to'),
|
||
|
lurl=login_url,
|
||
|
hurl=home_url
|
||
|
)
|
||
|
context['message'] = mark_safe(message)
|
||
|
context['section_title'] = _('Sign up')
|
||
|
return context
|
||
|
|
||
|
|
||
|
class SignupValidatedView(SignupValidateView):
|
||
|
template_name = "hosting/signup_validate.html"
|
||
|
|
||
|
def get_context_data(self, **kwargs):
|
||
|
context = super(SignupValidateView, self).get_context_data(**kwargs)
|
||
|
validated = CustomUser.validate_url(self.kwargs['validate_slug'])
|
||
|
login_url = '<a href="' + \
|
||
|
reverse('hosting:login') + '">' + str(_('login')) + '</a>'
|
||
|
section_title = _('Account activation')
|
||
|
user = CustomUser.objects.filter(
|
||
|
validation_slug=self.kwargs['validate_slug']).first()
|
||
|
if validated:
|
||
|
message = ('{account_activation_string} <br />'
|
||
|
' {login_string} {lurl}.').format(
|
||
|
account_activation_string=_(
|
||
|
"Your account has been activated."),
|
||
|
login_string=_("You can now"),
|
||
|
lurl=login_url)
|
||
|
email_data = {
|
||
|
'subject': _('Welcome to Data Center Light!'),
|
||
|
'to': user.email,
|
||
|
'context': {
|
||
|
'base_url': "{0}://{1}".format(
|
||
|
self.request.scheme,
|
||
|
self.request.get_host()
|
||
|
)
|
||
|
},
|
||
|
'template_name': 'welcome_user',
|
||
|
'template_path': 'datacenterlight/emails/',
|
||
|
'from_address': settings.DCL_SUPPORT_FROM_ADDRESS,
|
||
|
}
|
||
|
email = BaseEmail(**email_data)
|
||
|
email.send()
|
||
|
else:
|
||
|
home_url = '<a href="' + \
|
||
|
reverse('datacenterlight:cms_index') + \
|
||
|
'">Data Center Light</a>'
|
||
|
message = '{sorry_message} <br />{go_back_to} {hurl}'.format(
|
||
|
sorry_message=_("Sorry. Your request is invalid."),
|
||
|
go_back_to=_('Go back to'),
|
||
|
hurl=home_url
|
||
|
)
|
||
|
context['message'] = mark_safe(message)
|
||
|
context['section_title'] = section_title
|
||
|
return context
|
||
|
|
||
|
@method_decorator(decorators)
|
||
|
def get(self, request, *args, **kwargs):
|
||
|
if self.request.user.is_authenticated:
|
||
|
return HttpResponseRedirect(reverse_lazy('hosting:dashboard'))
|
||
|
return super(SignupValidatedView, self).get(request, *args, **kwargs)
|
||
|
|
||
|
|
||
|
class ResendActivationEmailView(ResendActivationLinkViewMixin):
|
||
|
template_name = 'hosting/resend_activation_link.html'
|
||
|
form_class = ResendActivationEmailForm
|
||
|
success_url = reverse_lazy('hosting:login')
|
||
|
email_template_path = 'datacenterlight/emails/'
|
||
|
email_template_name = 'user_activation'
|
||
|
|
||
|
|
||
|
class PasswordResetView(PasswordResetViewMixin):
|
||
|
site = 'dcl'
|
||
|
template_name = 'hosting/reset_password.html'
|
||
|
form_class = PasswordResetRequestForm
|
||
|
success_url = reverse_lazy('hosting:login')
|
||
|
template_email_path = 'hosting/emails/'
|
||
|
|
||
|
|
||
|
class PasswordResetConfirmView(PasswordResetConfirmViewMixin):
|
||
|
template_name = 'hosting/confirm_reset_password.html'
|
||
|
success_url = reverse_lazy('hosting:login')
|
||
|
|
||
|
def post(self, request, uidb64=None, token=None, *arg, **kwargs):
|
||
|
try:
|
||
|
uid = urlsafe_base64_decode(uidb64)
|
||
|
user = CustomUser.objects.get(pk=uid)
|
||
|
|
||
|
# opennebula_client = OpenNebulaManager(
|
||
|
# email=user.username,
|
||
|
# password=user.password,
|
||
|
# )
|
||
|
|
||
|
except (TypeError, ValueError, OverflowError, CustomUser.DoesNotExist):
|
||
|
user = None
|
||
|
opennebula_client = None
|
||
|
|
||
|
form = self.form_class(request.POST)
|
||
|
|
||
|
if user is not None and default_token_generator.check_token(user,
|
||
|
token):
|
||
|
if form.is_valid():
|
||
|
ldap_manager = LdapManager()
|
||
|
new_password = form.cleaned_data['new_password2']
|
||
|
|
||
|
# Make sure the user have an ldap account already
|
||
|
user.create_ldap_account(new_password)
|
||
|
|
||
|
# We are changing password in ldap before changing in database because
|
||
|
# ldap have more chances of failure than local database
|
||
|
if ldap_manager.change_password(user.username, new_password):
|
||
|
user.set_password(new_password)
|
||
|
user.save()
|
||
|
|
||
|
messages.success(request, _('Password has been reset.'))
|
||
|
|
||
|
# Change opennebula password
|
||
|
opennebula_client.change_user_password(user.password)
|
||
|
|
||
|
return self.form_valid(form)
|
||
|
|
||
|
messages.error(
|
||
|
request, _('Password reset has not been successful.'))
|
||
|
form.add_error(None,
|
||
|
_('Password reset has not been successful.'))
|
||
|
return self.form_invalid(form)
|
||
|
|
||
|
else:
|
||
|
error_msg = _('The reset password link is no longer valid.')
|
||
|
messages.error(request, _(error_msg))
|
||
|
form.add_error(None, error_msg)
|
||
|
return self.form_invalid(form)
|
||
|
|
||
|
|
||
|
class SSHKeyCreateView(FormView):
|
||
|
form_class = UserHostingKeyForm
|
||
|
model = UserHostingKey
|
||
|
template_name = 'hosting/user_key.html'
|
||
|
login_url = reverse_lazy('hosting:login')
|
||
|
context_object_name = "virtual_machine"
|
||
|
success_url = reverse_lazy('hosting:ssh_keys')
|
||
|
|
||
|
def get_form_kwargs(self):
|
||
|
kwargs = super(SSHKeyCreateView, self).get_form_kwargs()
|
||
|
kwargs.update({'request': self.request})
|
||
|
return kwargs
|
||
|
|
||
|
def form_valid(self, form):
|
||
|
form.save()
|
||
|
if settings.DCL_SSH_KEY_NAME_PREFIX in form.instance.name:
|
||
|
content = ContentFile(form.cleaned_data.get('private_key'))
|
||
|
filename = form.cleaned_data.get(
|
||
|
'name') + '_' + str(uuid.uuid4())[:8] + '_private.pem'
|
||
|
form.instance.private_key.save(filename, content)
|
||
|
context = self.get_context_data()
|
||
|
|
||
|
next_url = self.request.session.get(
|
||
|
'next',
|
||
|
reverse_lazy('hosting:create_virtual_machine')
|
||
|
)
|
||
|
|
||
|
if 'next' in self.request.session:
|
||
|
context.update({
|
||
|
'next_url': next_url
|
||
|
})
|
||
|
del (self.request.session['next'])
|
||
|
|
||
|
if form.cleaned_data.get('private_key'):
|
||
|
context.update({
|
||
|
'private_key': form.cleaned_data.get('private_key'),
|
||
|
'key_name': form.cleaned_data.get('name'),
|
||
|
'form': UserHostingKeyForm(request=self.request),
|
||
|
})
|
||
|
|
||
|
if self.request.user.is_authenticated:
|
||
|
owner = self.request.user
|
||
|
# manager = OpenNebulaManager(
|
||
|
# email=owner.username,
|
||
|
# password=owner.password
|
||
|
# )
|
||
|
# keys_to_save = get_all_public_keys(self.request.user)
|
||
|
# manager.save_key_in_opennebula_user('\n'.join(keys_to_save))
|
||
|
else:
|
||
|
self.request.session["new_user_hosting_key_id"] = form.instance.id
|
||
|
return HttpResponseRedirect(self.success_url)
|
||
|
|
||
|
def post(self, request, *args, **kwargs):
|
||
|
form = self.get_form()
|
||
|
required = 'add_ssh' in self.request.POST
|
||
|
form.fields['name'].required = required
|
||
|
form.fields['public_key'].required = required
|
||
|
if form.is_valid():
|
||
|
return self.form_valid(form)
|
||
|
else:
|
||
|
return self.form_invalid(form)
|
||
|
|
||
|
|
||
|
class AskSSHKeyView(SSHKeyCreateView):
|
||
|
form_class = UserHostingKeyForm
|
||
|
template_name = "datacenterlight/add_ssh_key.html"
|
||
|
success_url = reverse_lazy('datacenterlight:order_confirmation')
|
||
|
context_object_name = "dcl_vm_buy_add_ssh_key"
|
||
|
|
||
|
# @cache_control(no_cache=True, must_revalidate=True, no_store=True)
|
||
|
def get(self, request, *args, **kwargs):
|
||
|
context = {
|
||
|
'site_url': reverse_lazy('datacenterlight:index'),
|
||
|
# 'cms_integration': get_cms_integration('default'),
|
||
|
'form': UserHostingKeyForm(request=self.request),
|
||
|
# 'keys': get_all_public_keys(self.request.user)
|
||
|
}
|
||
|
return render(request, self.template_name, context)
|
||
|
|
||
|
def post(self, request, *args, **kwargs):
|
||
|
self.success_url = self.request.session.get("order_confirm_url")
|
||
|
return super(AskSSHKeyView, self).post(self, request, *args, **kwargs)
|
||
|
|
||
|
|
||
|
class IndexView(View):
|
||
|
template_name = "hosting/index.html"
|
||
|
|
||
|
def get_context_data(self, **kwargs):
|
||
|
# TODO refactor below
|
||
|
templates = []
|
||
|
data = None
|
||
|
|
||
|
context = {
|
||
|
'hosting': "nodejs",
|
||
|
'hosting_long': "NodeJS",
|
||
|
'domain': "node-hosting.ch",
|
||
|
'google_analytics': "UA-62285904-7",
|
||
|
'email': "info@node-hosting.ch",
|
||
|
'vm_types': data
|
||
|
# 'vm_types': VirtualMachineType.get_serialized_vm_types(),
|
||
|
}
|
||
|
return context
|
||
|
|
||
|
@method_decorator(decorators)
|
||
|
def get(self, request, *args, **kwargs):
|
||
|
context = self.get_context_data()
|
||
|
return render(request, self.template_name, context)
|