From f26b2f0e879e9c0f1e877c3e95c0fe6691bc3eef Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Wed, 29 Nov 2023 12:16:17 +0100 Subject: [PATCH] Allow user to create a new record on a domain --- orchestra/contrib/musician/forms.py | 21 +++++++++++++++++- .../templates/musician/domain_detail.html | 1 + .../templates/musician/record_form.html | 22 +++++++++++++++++++ orchestra/contrib/musician/urls.py | 2 +- orchestra/contrib/musician/views.py | 19 ++++++++++++++-- 5 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 orchestra/contrib/musician/templates/musician/record_form.html diff --git a/orchestra/contrib/musician/forms.py b/orchestra/contrib/musician/forms.py index 0a2065f6..50fa75ca 100644 --- a/orchestra/contrib/musician/forms.py +++ b/orchestra/contrib/musician/forms.py @@ -3,7 +3,7 @@ from django.contrib.auth.forms import AuthenticationForm from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ -from orchestra.contrib.domains.models import Domain +from orchestra.contrib.domains.models import Domain, Record from orchestra.contrib.mailboxes.models import Address, Mailbox from . import api @@ -131,6 +131,25 @@ class MailboxCreateForm(forms.ModelForm): class MailboxUpdateForm(forms.ModelForm): addresses = forms.MultipleChoiceField(required=False) + class Meta: fields = ('addresses',) model = Mailbox + + +class RecordCreateForm(forms.ModelForm): + + class Meta: + model = Record + fields = ("ttl", "type", "value") + + def __init__(self, *args, **kwargs): + self.domain = kwargs.pop('domain') + super().__init__(*args, **kwargs) + + def save(self, commit=True): + instance = super().save(commit=False) + instance.domain = self.domain + if commit: + super().save(commit=True) + return instance diff --git a/orchestra/contrib/musician/templates/musician/domain_detail.html b/orchestra/contrib/musician/templates/musician/domain_detail.html index d8c96cce..333ceee3 100644 --- a/orchestra/contrib/musician/templates/musician/domain_detail.html +++ b/orchestra/contrib/musician/templates/musician/domain_detail.html @@ -27,4 +27,5 @@ {% endfor %} +{% trans "Add new record" %} {% endblock %} diff --git a/orchestra/contrib/musician/templates/musician/record_form.html b/orchestra/contrib/musician/templates/musician/record_form.html new file mode 100644 index 00000000..43f7252b --- /dev/null +++ b/orchestra/contrib/musician/templates/musician/record_form.html @@ -0,0 +1,22 @@ +{% extends "musician/base.html" %} +{% load bootstrap4 i18n %} + +{% block content %} +{% trans "Go back" %} + +

{% trans "Add record for" %} {{ form.domain.name }}

+ +
+ {% csrf_token %} + {% bootstrap_form form %} + {% buttons %} + {% trans "Cancel" %} + + {% if form.instance.pk %} + + {% endif %} + {% endbuttons %} +
+{% endblock %} diff --git a/orchestra/contrib/musician/urls.py b/orchestra/contrib/musician/urls.py index a00ead27..fda9df2b 100644 --- a/orchestra/contrib/musician/urls.py +++ b/orchestra/contrib/musician/urls.py @@ -8,7 +8,6 @@ from django.urls import path from . import views - app_name = 'musician' urlpatterns = [ @@ -16,6 +15,7 @@ urlpatterns = [ path('auth/logout/', views.LogoutView.as_view(), name='logout'), path('dashboard/', views.DashboardView.as_view(), name='dashboard'), path('domains//', views.DomainDetailView.as_view(), name='domain-detail'), + path('domains//add-record/', views.DomainAddRecordView.as_view(), name='domain-add-record'), path('billing/', views.BillingView.as_view(), name='billing'), path('bills//download/', views.BillDownloadView.as_view(), name='bill-download'), path('profile/', views.ProfileView.as_view(), name='profile'), diff --git a/orchestra/contrib/musician/views.py b/orchestra/contrib/musician/views.py index d1563444..a6b38db0 100644 --- a/orchestra/contrib/musician/views.py +++ b/orchestra/contrib/musician/views.py @@ -24,7 +24,7 @@ from requests.exceptions import HTTPError from orchestra import get_version from orchestra.contrib.bills.models import Bill from orchestra.contrib.databases.models import Database -from orchestra.contrib.domains.models import Domain +from orchestra.contrib.domains.models import Domain, Record from orchestra.contrib.lists.models import List from orchestra.contrib.mailboxes.models import Address, Mailbox from orchestra.contrib.saas.models import SaaS @@ -33,7 +33,7 @@ from orchestra.utils.html import html_to_pdf # from .auth import login as auth_login from .auth import logout as auth_logout from .forms import (LoginForm, MailboxChangePasswordForm, MailboxCreateForm, - MailboxUpdateForm, MailForm) + MailboxUpdateForm, MailForm, RecordCreateForm) from .mixins import (CustomContextMixin, ExtendedPaginationMixin, UserTokenRequiredMixin) from .models import Address as AddressService @@ -468,6 +468,21 @@ class DomainDetailView(CustomContextMixin, UserTokenRequiredMixin, DetailView): return Domain.objects.filter(account=self.request.user) +class DomainAddRecordView(CustomContextMixin, UserTokenRequiredMixin, CreateView): + model = Record + form_class = RecordCreateForm + template_name = "musician/record_form.html" + + def get_form_kwargs(self): + kwargs = super().get_form_kwargs() + domain = get_object_or_404(Domain, account=self.request.user, pk=self.kwargs["pk"]) + kwargs['domain'] = domain + return kwargs + + def get_success_url(self): + return reverse_lazy("musician:domain-detail", kwargs={"pk": self.kwargs["pk"]}) + + class LoginView(FormView): template_name = 'auth/login.html' form_class = LoginForm