dashboard/dash/views.py

143 lines
4.4 KiB
Python
Raw Permalink Normal View History

# Copyright © 2022 Aravinth Manivannan <realaravinth@batsense.net>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.shortcuts import render, redirect, get_object_or_404
from django.utils.http import urlencode
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth import get_user_model
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_protect
from django.urls import reverse
from accounts.decorators import confirm_access
from infrastructure.utils import Infra
2022-06-27 15:13:02 +00:00
from .models import Instance, InstanceConfiguration
2022-06-30 09:07:49 +00:00
from .utils import (
create_instance as create_instance_util,
VmErrors,
VmException,
footer_ctx,
)
2022-06-27 15:13:02 +00:00
def default_ctx(title: str, username: str):
"""
Default context for all dashboard pages
"""
return {
"title": title,
"username": username,
"open_instances": "open",
2022-06-30 09:07:49 +00:00
"footer": footer_ctx(),
}
@login_required
def home(request):
"""
Dashboard homepage view
"""
PAGE_TITLE = "Home"
2022-06-17 17:58:51 +00:00
user = request.user
ctx = default_ctx(title=PAGE_TITLE, username=user.username)
instances = Instance.objects.filter(owned_by=user)
if instances:
ctx["instances"] = instances
return render(request, "dash/home/index.html", context=ctx)
2022-06-17 17:58:51 +00:00
@login_required
@csrf_protect
def create_instance(request):
"""
Create instance view
"""
PAGE_TITLE = "Create Instance"
user = request.user
def get_ctx():
ctx = default_ctx(title=PAGE_TITLE, username=user.username)
configurations = InstanceConfiguration.objects.all()
ctx["configurations"] = InstanceConfiguration.objects.all()
return ctx
if request.method == "GET":
ctx = get_ctx()
return render(request, "dash/instances/new/index.html", context=ctx)
name = request.POST["name"]
configuration = request.POST["configuration"]
2022-06-27 15:13:02 +00:00
try:
instance = create_instance_util(
vm_name=name, configuration_name=configuration, user=request.user
)
return redirect(reverse("billing.invoice.generate", args=(instance.name,)))
except VmException as e:
2022-06-17 17:58:51 +00:00
ctx = get_ctx()
reason = e.code.value
2022-06-27 15:13:02 +00:00
2022-06-17 17:58:51 +00:00
ctx["error"] = {
"title": "Can't create instance",
2022-06-27 15:13:02 +00:00
"reason": reason,
2022-06-17 17:58:51 +00:00
}
return render(request, "dash/instances/new/index.html", status=400, context=ctx)
@login_required
def list_instances(request):
PAGE_TITLE = "My Instances"
user = request.user
instances = Instance.objects.filter(owned_by=user)
ctx = default_ctx(title=PAGE_TITLE, username=user.username)
ctx["instances"] = instances
return render(request, "dash/instances/list/index.html", context=ctx)
@login_required
def view_instance(request, name: str):
PAGE_TITLE = f"Hostea Instance: {name}"
user = request.user
instance = get_object_or_404(Instance, owned_by=user, name=name)
ctx = default_ctx(title=PAGE_TITLE, username=user.username)
instance.configuration = instance.configuration_id
gitea_uri = Infra.get_gitea_uri(instance=instance)
woodpecker = Infra.get_woodpecker_uri(instance=instance)
ctx["instance"] = instance
ctx["woodpecker"] = woodpecker
ctx["gitea_uri"] = gitea_uri
return render(request, "dash/instances/view/index.html", context=ctx)
@login_required
@csrf_protect
@confirm_access
def delete_instance(request, name):
PAGE_TITLE = f"Delete Instance {name}"
user = request.user
instance = get_object_or_404(Instance, name=name, owned_by=user)
if request.method == "GET":
ctx = default_ctx(title=PAGE_TITLE, username=user.username)
ctx["instance"] = instance
return render(request, "dash/instances/delete/index.html", context=ctx)
return redirect(reverse("infra.rm", args=(instance.name,)))