First draft of organization creation

This commit is contained in:
Tobias Kunze 2025-03-18 06:58:40 +01:00
parent 325e767b0e
commit 2baa3fd5ec
8 changed files with 58 additions and 1 deletions

View file

@ -27,6 +27,12 @@ class Organization(ServalaModelMixin, models.Model):
verbose_name=_("Members"),
)
def set_owner(self, user):
OrganizationMembership.objects.filter(user=user, organization=self).delete()
OrganizationMembership.objects.create(
user=user, organization=self, role=OrganizationRole.OWNER
)
class Meta:
verbose_name = _("Organization")
verbose_name_plural = _("Organizations")

View file

@ -0,0 +1,3 @@
from .organization import OrganizationCreateForm
__all__ = ["OrganizationCreateForm"]

View file

@ -0,0 +1,9 @@
from django.forms import ModelForm
from servala.core.models import Organization
class OrganizationCreateForm(ModelForm):
class Meta:
model = Organization
fields = ("name",)

View file

@ -0,0 +1,16 @@
{% extends "frontend/base.html" %}
{% load i18n %}
{% block html_title %}
{% block page_title %}
{% translate "Create a new organization" %}
{% endblock page_title %}
{% endblock html_title %}
{% block content %}
<section class="section">
<div class="card">
<div class="card-content">
<div class="card-body">{% include "includes/form.html" %}</div>
</div>
</div>
</section>
{% endblock content %}

View file

@ -87,7 +87,7 @@
{% elif current_organization %}
{% translate "Organization" %}: {{ current_organization.name }}
{% else %}
<a href="#TODO" class="sidebar-link">
<a href="{% url 'frontend:organization.create' %}" class="sidebar-link">
<i class="bi bi-plus-square"></i>
<span>{% translate "Create organization" %}</span>
</a>

View file

@ -5,5 +5,10 @@ from servala.frontend import views
urlpatterns = [
path("accounts/profile/", views.ProfileView.as_view(), name="profile"),
path("accounts/logout/", views.LogoutView.as_view(), name="logout"),
path(
"organizations/create",
views.OrganizationCreateView.as_view(),
name="organization.create",
),
path("", views.IndexView.as_view(), name="index"),
]

View file

@ -1,8 +1,10 @@
from .auth import LogoutView
from .generic import IndexView, ProfileView
from .organization import OrganizationCreateView
__all__ = [
"IndexView",
"LogoutView",
"OrganizationCreateView",
"ProfileView",
]

View file

@ -0,0 +1,16 @@
from django.views.generic import FormView
from servala.frontend.forms import OrganizationCreateForm
class OrganizationCreateView(FormView):
form_class = OrganizationCreateForm
template_name = "frontend/organizations/create.html"
def form_valid(self, form):
form.save()
form.instance.set_owner(self.request.user)
return super().form_valid(form)
def get_success_url(self):
return "/"