Add MariaDB database requests and deployment manifests
This commit is contained in:
1 parent
e1dc9a26cb
commit
9746bd8a74
12 files changed
+418
-8
No files matched your search
@@ -1,16 +1,17 @@
|
||||
from django.contrib.auth.mixins import PermissionRequiredMixin
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import IntegrityError
|
||||
from django.db import IntegrityError, transaction
|
||||
from django.db.models import Q
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
from django.shortcuts import get_object_or_404, redirect
|
||||
from django.urls import reverse_lazy
|
||||
from django.utils.functional import cached_property
|
||||
from django.views import View
|
||||
from django.views.generic import CreateView, DetailView, ListView, UpdateView
|
||||
from django.views.generic.detail import SingleObjectMixin
|
||||
|
||||
from .access import OperatorOnlyMixin, ResellerScopeMixin, reseller_for_user
|
||||
from .models import HostingAccount, HostingPlan, WebProvisioningPlan
|
||||
from .models import HostingAccount, HostingDatabase, HostingPlan, WebProvisioningPlan
|
||||
from .reseller_accounts import create_reseller_account
|
||||
from .web_node import build_web_smoke_bundle, serialize_deployment_manifest
|
||||
|
||||
@@ -19,6 +20,7 @@ CHANGE_ACCOUNTS = "hosting.change_hostingaccount"
|
||||
VIEW_PLANS = "hosting.view_webprovisioningplan"
|
||||
VIEW_HOSTING_PLANS = "hosting.view_hostingplan"
|
||||
VIEW_RESELLERS = "hosting.view_reseller"
|
||||
VIEW_DATABASES = "hosting.view_hostingdatabase"
|
||||
|
||||
|
||||
class AccountList(ResellerScopeMixin, PermissionRequiredMixin, ListView):
|
||||
@@ -140,15 +142,60 @@ class AccountDetail(ResellerScopeMixin, PermissionRequiredMixin, DetailView):
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
if reseller_for_user(
|
||||
self.request.user
|
||||
) is not None or self.request.user.has_perm(VIEW_PLANS):
|
||||
user = self.request.user
|
||||
reseller = reseller_for_user(user)
|
||||
if reseller is not None or user.has_perm(VIEW_PLANS):
|
||||
context["plans"] = self.object.webprovisioningplan_set.order_by(
|
||||
"-requested_at", "-pk"
|
||||
)[:10]
|
||||
if reseller is not None or user.has_perm(VIEW_DATABASES):
|
||||
context["databases"] = self.object.databases.all()
|
||||
return context
|
||||
|
||||
|
||||
class DatabaseCreate(OperatorOnlyMixin, PermissionRequiredMixin, CreateView):
|
||||
model = HostingDatabase
|
||||
fields = ("suffix",)
|
||||
template_name = "hosting/database_form.html"
|
||||
permission_required = (VIEW_ACCOUNTS, VIEW_DATABASES, "hosting.add_hostingdatabase")
|
||||
|
||||
@cached_property
|
||||
def account(self):
|
||||
return get_object_or_404(HostingAccount, pk=self.kwargs["pk"])
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
return super().get_context_data(account=self.account, **kwargs)
|
||||
|
||||
def form_valid(self, form):
|
||||
self.object = form.save(commit=False)
|
||||
self.object.account = self.account
|
||||
try:
|
||||
with transaction.atomic():
|
||||
self.object.save()
|
||||
except IntegrityError:
|
||||
if not self.account.databases.filter(suffix=self.object.suffix).exists():
|
||||
raise
|
||||
form.add_error(
|
||||
"suffix", "This account already has a database with that suffix."
|
||||
)
|
||||
return self.form_invalid(form)
|
||||
return redirect(self.account)
|
||||
|
||||
|
||||
class DownloadDatabaseManifest(OperatorOnlyMixin, PermissionRequiredMixin, View):
|
||||
permission_required = (VIEW_ACCOUNTS, VIEW_DATABASES)
|
||||
|
||||
def get(self, request, pk):
|
||||
database = get_object_or_404(HostingDatabase, pk=pk)
|
||||
return JsonResponse(
|
||||
database.spec.manifest(),
|
||||
headers={
|
||||
"Cache-Control": "private, no-store",
|
||||
"Content-Disposition": f'attachment; filename="coulobre-database-{database.pk}.json"',
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
class HostingPlanList(OperatorOnlyMixin, PermissionRequiredMixin, ListView):
|
||||
model = HostingPlan
|
||||
permission_required = VIEW_HOSTING_PLANS
|
||||
|
||||
Reference in new issue
Block a user