Compare commits
10 commits
dcbf98f829
...
2dcc5650a9
Author | SHA1 | Date | |
---|---|---|---|
2dcc5650a9 | |||
7f389434a4 | |||
1b69310c77 | |||
314ecc9c0d | |||
6ed19e90a3 | |||
f17e1a0782 | |||
eae01c74c7 | |||
b36ebcf5ff | |||
72bd7388d6 | |||
ecf4052819 |
23 changed files with 335 additions and 73 deletions
22
src/servala/core/middleware.py
Normal file
22
src/servala/core/middleware.py
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
|
from django.urls import resolve
|
||||||
|
|
||||||
|
from servala.core.models import Organization
|
||||||
|
|
||||||
|
|
||||||
|
class OrganizationMiddleware:
|
||||||
|
|
||||||
|
def __init__(self, get_response):
|
||||||
|
self.get_response = get_response
|
||||||
|
|
||||||
|
def __call__(self, request):
|
||||||
|
url = resolve(request.path_info)
|
||||||
|
|
||||||
|
organization_slug = url.kwargs.get("organization")
|
||||||
|
if organization_slug:
|
||||||
|
pk = organization_slug.rsplit("-", maxsplit=1)[-1]
|
||||||
|
request.organization = get_object_or_404(Organization, pk=pk)
|
||||||
|
else:
|
||||||
|
request.organization = None
|
||||||
|
|
||||||
|
return self.get_response(request)
|
25
src/servala/core/migrations/0003_billing_entity_nullable.py
Normal file
25
src/servala/core/migrations/0003_billing_entity_nullable.py
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
# Generated by Django 5.2b1 on 2025-03-20 08:12
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("core", "0002_billingentity_created_at_billingentity_updated_at_and_more"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="organization",
|
||||||
|
name="billing_entity",
|
||||||
|
field=models.ForeignKey(
|
||||||
|
null=True,
|
||||||
|
on_delete=django.db.models.deletion.PROTECT,
|
||||||
|
related_name="organizations",
|
||||||
|
to="core.billingentity",
|
||||||
|
verbose_name="Billing entity",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
]
|
|
@ -1,5 +1,8 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.urls import reverse
|
||||||
|
from django.utils.functional import cached_property
|
||||||
|
from django.utils.text import slugify
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from .mixins import ServalaModelMixin
|
from .mixins import ServalaModelMixin
|
||||||
|
@ -13,6 +16,7 @@ class Organization(ServalaModelMixin, models.Model):
|
||||||
on_delete=models.PROTECT,
|
on_delete=models.PROTECT,
|
||||||
related_name="organizations",
|
related_name="organizations",
|
||||||
verbose_name=_("Billing entity"),
|
verbose_name=_("Billing entity"),
|
||||||
|
null=True, # TODO: billing entity should be required
|
||||||
)
|
)
|
||||||
origin = models.ForeignKey(
|
origin = models.ForeignKey(
|
||||||
to="OrganizationOrigin",
|
to="OrganizationOrigin",
|
||||||
|
@ -28,6 +32,15 @@ class Organization(ServalaModelMixin, models.Model):
|
||||||
verbose_name=_("Members"),
|
verbose_name=_("Members"),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def slug(self):
|
||||||
|
return f"{slugify(self.name)}-{self.id}"
|
||||||
|
|
||||||
|
def get_absolute_url(self):
|
||||||
|
return reverse(
|
||||||
|
"frontend:organization.dashboard", kwargs={"organization": self.slug}
|
||||||
|
)
|
||||||
|
|
||||||
def set_owner(self, user):
|
def set_owner(self, user):
|
||||||
OrganizationMembership.objects.filter(user=user, organization=self).delete()
|
OrganizationMembership.objects.filter(user=user, organization=self).delete()
|
||||||
OrganizationMembership.objects.create(
|
OrganizationMembership.objects.create(
|
||||||
|
|
|
@ -2,4 +2,4 @@ def add_organizations(request):
|
||||||
if not request.user.is_authenticated:
|
if not request.user.is_authenticated:
|
||||||
return {"user_organizations": []}
|
return {"user_organizations": []}
|
||||||
|
|
||||||
return {"user_organizations": request.user.organizations.all()}
|
return {"user_organizations": request.user.organizations.all().order_by("name")}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
from .organization import OrganizationCreateForm
|
from .organization import OrganizationCreateForm
|
||||||
|
from .profile import UserProfileForm
|
||||||
|
|
||||||
__all__ = ["OrganizationCreateForm"]
|
__all__ = ["OrganizationCreateForm", "UserProfileForm"]
|
||||||
|
|
20
src/servala/frontend/forms/mixins.py
Normal file
20
src/servala/frontend/forms/mixins.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
from servala.frontend.forms.renderers import InlineFormRenderer
|
||||||
|
|
||||||
|
|
||||||
|
class HtmxMixin:
|
||||||
|
"""
|
||||||
|
Form mixin that retains only a single field when specified.
|
||||||
|
Useful when sending single fields with htmx.
|
||||||
|
"""
|
||||||
|
|
||||||
|
default_renderer = InlineFormRenderer
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
self.single_field = kwargs.pop("single_field", None)
|
||||||
|
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
|
if self.single_field and self.single_field in self.fields:
|
||||||
|
field = self.fields[self.single_field]
|
||||||
|
self.fields.clear()
|
||||||
|
self.fields[self.single_field] = field
|
11
src/servala/frontend/forms/profile.py
Normal file
11
src/servala/frontend/forms/profile.py
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
from django import forms
|
||||||
|
|
||||||
|
from servala.core.models import User
|
||||||
|
from servala.frontend.forms.mixins import HtmxMixin
|
||||||
|
|
||||||
|
|
||||||
|
class UserProfileForm(HtmxMixin, forms.ModelForm):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = User
|
||||||
|
fields = ("email", "company")
|
|
@ -1,4 +1,5 @@
|
||||||
from django.forms.renderers import TemplatesSetting
|
from django.forms.renderers import TemplatesSetting
|
||||||
|
from django.forms.widgets import Textarea
|
||||||
|
|
||||||
|
|
||||||
def inject_class(f, class_name):
|
def inject_class(f, class_name):
|
||||||
|
@ -16,13 +17,31 @@ class VerticalFormRenderer(TemplatesSetting):
|
||||||
form_template_name = "frontend/forms/form.html"
|
form_template_name = "frontend/forms/form.html"
|
||||||
field_template_name = "frontend/forms/vertical_field.html"
|
field_template_name = "frontend/forms/vertical_field.html"
|
||||||
|
|
||||||
|
def get_class_names(self, input_type):
|
||||||
|
if input_type == "checkbox":
|
||||||
|
return "form-check-input"
|
||||||
|
return "form-control"
|
||||||
|
|
||||||
|
def get_widget_input_type(self, widget):
|
||||||
|
if isinstance(widget, Textarea):
|
||||||
|
return "textarea"
|
||||||
|
return widget.input_type
|
||||||
|
|
||||||
|
def get_field_input_type(self, field):
|
||||||
|
widget = field.field.widget
|
||||||
|
if inner_widget := getattr(widget, "widget", None):
|
||||||
|
widget = inner_widget
|
||||||
|
return self.get_widget_input_type(widget)
|
||||||
|
|
||||||
def render(self, template_name, context, request=None):
|
def render(self, template_name, context, request=None):
|
||||||
if field := context.get("field"):
|
if field := context.get("field"):
|
||||||
if field.field.widget.input_type == "checkbox":
|
input_type = self.get_field_input_type(field)
|
||||||
class_name = "form-check-input"
|
|
||||||
else:
|
|
||||||
class_name = "form-control"
|
|
||||||
field.build_widget_attrs = inject_class(
|
field.build_widget_attrs = inject_class(
|
||||||
field.build_widget_attrs, class_name
|
field.build_widget_attrs, self.get_class_names(input_type)
|
||||||
)
|
)
|
||||||
return super().render(template_name, context, request)
|
return super().render(template_name, context, request)
|
||||||
|
|
||||||
|
|
||||||
|
class InlineFormRenderer(VerticalFormRenderer):
|
||||||
|
form_template_name = "frontend/forms/form.html"
|
||||||
|
field_template_name = "frontend/forms/inline_field.html"
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
<link rel="stylesheet"
|
<link rel="stylesheet"
|
||||||
href="{% static 'mazer/compiled/css/app-dark.css' %}">
|
href="{% static 'mazer/compiled/css/app-dark.css' %}">
|
||||||
<link rel="stylesheet" href="{% static 'mazer/compiled/css/iconly.css' %}">
|
<link rel="stylesheet" href="{% static 'mazer/compiled/css/iconly.css' %}">
|
||||||
|
<link rel="stylesheet" href="{% static 'css/servala.css' %}">
|
||||||
<script src="{% static "js/htmx.min.js" %}" defer></script>
|
<script src="{% static "js/htmx.min.js" %}" defer></script>
|
||||||
</head>
|
</head>
|
||||||
<title>
|
<title>
|
||||||
|
|
21
src/servala/frontend/templates/frontend/forms/field.html
Normal file
21
src/servala/frontend/templates/frontend/forms/field.html
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
{% load i18n %}
|
||||||
|
<div class="form-group{% if field.field.required %} mandatory{% endif %}{% if errors %} is-invalid{% endif %}{% if extra_class %} {{ extra_class }}{% endif %}">
|
||||||
|
{% if not hide_label %}
|
||||||
|
{% if field.field.widget.input_type != "checkbox" or field.field.widget.allow_multiple_selected %}
|
||||||
|
<label for="{{ field.auto_id }}" class="form-label">{{ field.label }}</label>
|
||||||
|
{% endif %}
|
||||||
|
{% endif %}
|
||||||
|
{% if field.use_fieldset %}
|
||||||
|
<fieldset {% if field.help_text and field.auto_id and "aria-describedby" not in field.field.widget.attrs %} aria-describedby="{{ field.auto_id }}_helptext"{% endif %}>
|
||||||
|
{% endif %}
|
||||||
|
{{ field }}
|
||||||
|
{% if field.field.widget.input_type == "checkbox" and not field.field.widget.allow_multiple_selected %}
|
||||||
|
<label for="{{ field.auto_id }}" class="form-check-label form-label">{{ field.label }}</label>
|
||||||
|
{% endif %}
|
||||||
|
{% if field.use_fieldset %}</fieldset>{% endif %}
|
||||||
|
{% for text in field.errors %}<div class="invalid-feedback">{{ text }}</div>{% endfor %}
|
||||||
|
{% if field.help_text %}
|
||||||
|
<small class="form-text text-muted"
|
||||||
|
{% if field.auto_id %}id="{{ field.auto_id }}_helptext"{% endif %}>{{ field.help_text|safe }}</small>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
|
@ -0,0 +1 @@
|
||||||
|
{% include "frontend/forms/field.html" with extra_class="d-inline" hide_label=True %}
|
|
@ -1,21 +1 @@
|
||||||
{% load i18n %}
|
<div class="col-12">{% include "frontend/forms/field.html" %}</div>
|
||||||
<div class="col-12">
|
|
||||||
<div class="form-group{% if field.field.required %} mandatory{% endif %}{% if errors %} is-invalid{% endif %}">
|
|
||||||
{% if field.field.widget.input_type != "checkbox" or field.field.widget.allow_multiple_selected %}
|
|
||||||
<label for="{{ field.auto_id }}" class="form-label">{{ field.label }}</label>
|
|
||||||
{% endif %}
|
|
||||||
{% if field.use_fieldset %}
|
|
||||||
<fieldset {% if field.help_text and field.auto_id and "aria-describedby" not in field.field.widget.attrs %} aria-describedby="{{ field.auto_id }}_helptext"{% endif %}>
|
|
||||||
{% endif %}
|
|
||||||
{{ field }}
|
|
||||||
{% if field.field.widget.input_type == "checkbox" and not field.field.widget.allow_multiple_selected %}
|
|
||||||
<label for="{{ field.auto_id }}" class="form-check-label form-label">{{ field.label }}</label>
|
|
||||||
{% endif %}
|
|
||||||
{% if field.use_fieldset %}</fieldset>{% endif %}
|
|
||||||
{% for text in field.errors %}<div class="invalid-feedback">{{ text }}</div>{% endfor %}
|
|
||||||
{% if field.help_text %}
|
|
||||||
<small class="form-text text-muted"
|
|
||||||
{% if field.auto_id %}id="{{ field.auto_id }}_helptext"{% endif %}>{{ field.help_text|safe }}</small>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
{% extends "frontend/base.html" %}
|
{% extends "frontend/base.html" %}
|
||||||
{% load i18n static %}
|
{% load i18n static %}
|
||||||
|
{% load partials %}
|
||||||
{% block html_title %}
|
{% block html_title %}
|
||||||
{% block page_title %}
|
{% block page_title %}
|
||||||
{% translate "Profile" %}
|
{% translate "Profile" %}
|
||||||
|
@ -10,6 +11,62 @@
|
||||||
<h4 class="card-title">{% translate "Account" %}</h4>
|
<h4 class="card-title">{% translate "Account" %}</h4>
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
{% partialdef user-email %}
|
||||||
|
<td>
|
||||||
|
{{ request.user.email }}
|
||||||
|
<button class="btn btn-primary"
|
||||||
|
hx-get="{% url 'frontend:profile' %}?fragment=user-email-edit"
|
||||||
|
hx-target="closest td"
|
||||||
|
hx-swap="outerHTML">{% translate "Edit" %}</button>
|
||||||
|
</td>
|
||||||
|
{% endpartialdef user-email %}
|
||||||
|
{% partialdef user-company %}
|
||||||
|
<td>
|
||||||
|
{{ request.user.company|default:"–" }}
|
||||||
|
<button class="btn btn-primary"
|
||||||
|
hx-get="{% url 'frontend:profile' %}?fragment=user-company-edit"
|
||||||
|
hx-target="closest td"
|
||||||
|
hx-swap="outerHTML">{% translate "Edit" %}</button>
|
||||||
|
</td>
|
||||||
|
{% endpartialdef user-company %}
|
||||||
|
{% partialdef user-email-edit %}
|
||||||
|
<td>
|
||||||
|
<form hx-target="closest td"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
hx-post="{{ request.url }}">
|
||||||
|
<div class="d-flex align-items-baseline">
|
||||||
|
{{ form.email.as_field_group }}
|
||||||
|
<input type="hidden" name="hx-single-field" value="email">
|
||||||
|
<input type="hidden" name="fragment" value="user-email">
|
||||||
|
<button type="submit" class="btn btn-primary mx-1">{% translate "Save" %}</button>
|
||||||
|
<button type="button"
|
||||||
|
class="btn btn-secondary"
|
||||||
|
hx-get="{{ request.path }}?fragment=user-email"
|
||||||
|
hx-target="closest td"
|
||||||
|
hx-swap="outerHTML">{% translate "Cancel" %}</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
{% endpartialdef %}
|
||||||
|
{% partialdef user-company-edit %}
|
||||||
|
<td>
|
||||||
|
<form hx-target="closest td"
|
||||||
|
hx-swap="outerHTML"
|
||||||
|
hx-post="{{ request.url }}">
|
||||||
|
<div class="d-flex align-items-baseline">
|
||||||
|
{{ form.company.as_field_group }}
|
||||||
|
<input type="hidden" name="hx-single-field" value="company">
|
||||||
|
<input type="hidden" name="fragment" value="user-company">
|
||||||
|
<button type="submit" class="btn mx-1 btn-primary">{% translate "Save" %}</button>
|
||||||
|
<button type="button"
|
||||||
|
class="btn btn-secondary"
|
||||||
|
hx-get="{{ request.path }}?fragment=user-company"
|
||||||
|
hx-target="closest td"
|
||||||
|
hx-swap="outerHTML">{% translate "Cancel" %}</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</td>
|
||||||
|
{% endpartialdef %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<section>
|
<section>
|
||||||
<div class="row match-height">
|
<div class="row match-height">
|
||||||
|
@ -24,20 +81,20 @@
|
||||||
<table class="table table-lg">
|
<table class="table table-lg">
|
||||||
<tbody>
|
<tbody>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{% translate "E-mail" %}</th>
|
<th class="w-25">{% translate "Name" %}</th>
|
||||||
<td>{{ request.user.email }}</td>
|
<td>{{ request.user.first_name }} {{ request.user.last_name }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{% translate "First name" %}</th>
|
<th class="w-25">
|
||||||
<td>{{ request.user.first_name }}</td>
|
<span class="d-flex mt-2">{% translate "E-mail" %}</span>
|
||||||
|
</th>
|
||||||
|
{% partial user-email %}
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>{% translate "Last name" %}</th>
|
<th class="w-25">
|
||||||
<td>{{ request.user.last_name }}</td>
|
<span class="d-flex mt-2">{% translate "Company" %}</span>
|
||||||
</tr>
|
</th>
|
||||||
<tr>
|
{% partial user-company %}
|
||||||
<th>{% translate "Company" %}</th>
|
|
||||||
<td>{{ request.user.company }}</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
|
@ -5,11 +5,13 @@
|
||||||
{% include "includes/form_errors.html" %}
|
{% include "includes/form_errors.html" %}
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
{{ form }}
|
{{ form }}
|
||||||
<button class="btn btn-primary" type="submit">
|
<div class="col-sm-12 d-flex justify-content-end">
|
||||||
{% if form_submit_label %}
|
<button class="btn btn-primary me-1 mb-1" type="submit">
|
||||||
{{ form_submit_label }}
|
{% if form_submit_label %}
|
||||||
{% else %}
|
{{ form_submit_label }}
|
||||||
{% translate "Save" %}
|
{% else %}
|
||||||
{% endif %}
|
{% translate "Save" %}
|
||||||
</button>
|
{% endif %}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
<div class="sidebar-header position-relative">
|
<div class="sidebar-header position-relative">
|
||||||
<div class="d-flex justify-content-between align-items-center">
|
<div class="d-flex justify-content-between align-items-center">
|
||||||
<div class="logo">
|
<div class="logo">
|
||||||
<a href="index.html">
|
<a href="{% if request.organization %}{{ request.organization.get_absolute_url }}{% else %}/{% endif %}">
|
||||||
<img src="" alt="{% translate 'Logo' %}" srcset="">
|
<img src="" alt="{% translate 'Logo' %}" srcset="">
|
||||||
</a>
|
</a>
|
||||||
</div>
|
</div>
|
||||||
|
@ -66,26 +66,34 @@
|
||||||
{% else %}
|
{% else %}
|
||||||
{# request.user.is_authenticated #}
|
{# request.user.is_authenticated #}
|
||||||
<li class="sidebar-item">
|
<li class="sidebar-item">
|
||||||
{% if user_organizations.count > 1 %}
|
{% if user_organizations.count %}
|
||||||
<button class="btn btn-primary dropdown-toggle me-1"
|
<button class="btn btn-outline-primary dropdown-toggle w-100"
|
||||||
type="button"
|
type="button"
|
||||||
id="organizationDropdown"
|
id="organizationDropdown"
|
||||||
data-bs-toggle="dropdown"
|
data-bs-toggle="dropdown"
|
||||||
aria-haspopup="true"
|
aria-haspopup="true"
|
||||||
aria-expanded="false">
|
aria-expanded="false">
|
||||||
{% if current_organization %}
|
{% if request.organization %}
|
||||||
{{ current_organization.name }}
|
{{ request.organization.name }}
|
||||||
{% else %}
|
{% else %}
|
||||||
{% translate "Organizations" %}
|
{% translate "Organizations" %}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</button>
|
</button>
|
||||||
<div class="dropdown-menu" aria-labelledby="organizationDropdown">
|
<div class="dropdown-menu shadow"
|
||||||
|
aria-labelledby="organizationDropdown"
|
||||||
|
id="organization-dropdown">
|
||||||
{% for organization in user_organizations %}
|
{% for organization in user_organizations %}
|
||||||
<a class="dropdown-item" href="#TODO">{{ organization.name }}</a>
|
<a class="dropdown-item{% if organization == request.organization %} active{% endif %}"
|
||||||
|
href="{{ organization.get_absolute_url }}">
|
||||||
|
<i class="bi bi-building-fill me-1"></i>
|
||||||
|
{{ organization.name }}
|
||||||
|
</a>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
<a href="{% url 'frontend:organization.create' %}" class="dropdown-item">
|
||||||
|
<i class="bi bi-building-add me-1"></i>
|
||||||
|
<span>{% translate "Create organization" %}</span>
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
{% elif current_organization %}
|
|
||||||
{% translate "Organization" %}: {{ current_organization.name }}
|
|
||||||
{% else %}
|
{% else %}
|
||||||
<a href="{% url 'frontend:organization.create' %}" class="sidebar-link">
|
<a href="{% url 'frontend:organization.create' %}" class="sidebar-link">
|
||||||
<i class="bi bi-plus-square"></i>
|
<i class="bi bi-plus-square"></i>
|
||||||
|
@ -93,12 +101,15 @@
|
||||||
</a>
|
</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</li>
|
</li>
|
||||||
<li class="sidebar-item">
|
{% if request.organization %}
|
||||||
<a href="index.html" class='sidebar-link'>
|
<li class="sidebar-item">
|
||||||
<i class="bi bi-grid-fill"></i>
|
<a href="{{ request.organization.get_absolute_url }}"
|
||||||
<span>{% translate 'Dashboard' %}</span>
|
class='sidebar-link'>
|
||||||
</a>
|
<i class="bi bi-grid-fill"></i>
|
||||||
</li>
|
<span>{% translate 'Dashboard' %}</span>
|
||||||
|
</a>
|
||||||
|
</li>
|
||||||
|
{% endif %}
|
||||||
<li class="sidebar-title">{% translate 'Account' %}</li>
|
<li class="sidebar-title">{% translate 'Account' %}</li>
|
||||||
<li class="sidebar-item">
|
<li class="sidebar-item">
|
||||||
<a href="{% url 'frontend:profile' %}" class='sidebar-link'>
|
<a href="{% url 'frontend:profile' %}" class='sidebar-link'>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from django.urls import path
|
from django.urls import include, path
|
||||||
|
from django.views.generic import RedirectView
|
||||||
|
|
||||||
from servala.frontend import views
|
from servala.frontend import views
|
||||||
|
|
||||||
|
@ -10,5 +11,17 @@ urlpatterns = [
|
||||||
views.OrganizationCreateView.as_view(),
|
views.OrganizationCreateView.as_view(),
|
||||||
name="organization.create",
|
name="organization.create",
|
||||||
),
|
),
|
||||||
path("", views.IndexView.as_view(), name="index"),
|
path(
|
||||||
|
"<slug:organization>/",
|
||||||
|
include(
|
||||||
|
[
|
||||||
|
path(
|
||||||
|
"",
|
||||||
|
views.OrganizationDashboardView.as_view(),
|
||||||
|
name="organization.dashboard",
|
||||||
|
),
|
||||||
|
]
|
||||||
|
),
|
||||||
|
),
|
||||||
|
path("", RedirectView.as_view(pattern_name="frontend:profile"), name="index"),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
from .auth import LogoutView
|
from .auth import LogoutView
|
||||||
from .generic import IndexView, ProfileView
|
from .generic import IndexView, ProfileView
|
||||||
from .organization import OrganizationCreateView
|
from .organization import OrganizationCreateView, OrganizationDashboardView
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"IndexView",
|
"IndexView",
|
||||||
"LogoutView",
|
"LogoutView",
|
||||||
"OrganizationCreateView",
|
"OrganizationCreateView",
|
||||||
|
"OrganizationDashboardView",
|
||||||
"ProfileView",
|
"ProfileView",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,21 +1,41 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.views.generic import TemplateView
|
from django.urls import reverse_lazy
|
||||||
|
from django.utils.functional import cached_property
|
||||||
|
from django.views.generic import TemplateView, UpdateView
|
||||||
|
|
||||||
|
from servala.core.models import User
|
||||||
|
from servala.frontend.forms.profile import UserProfileForm
|
||||||
|
from servala.frontend.views.mixins import HtmxMixin
|
||||||
|
|
||||||
|
|
||||||
class IndexView(TemplateView):
|
class IndexView(TemplateView):
|
||||||
template_name = "frontend/index.html"
|
template_name = "frontend/index.html"
|
||||||
|
|
||||||
|
|
||||||
class ProfileView(TemplateView):
|
class ProfileView(HtmxMixin, UpdateView):
|
||||||
template_name = "frontend/profile.html"
|
template_name = "frontend/profile.html"
|
||||||
|
form_class = UserProfileForm
|
||||||
|
success_url = reverse_lazy("frontend:profile")
|
||||||
|
fragments = ("user-email", "user-email-edit", "user-company", "user-company-edit")
|
||||||
|
model = User
|
||||||
|
|
||||||
|
def get_object(self):
|
||||||
|
return self.request.user
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def object(self):
|
||||||
|
return self.get_object()
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
keycloak_server_url = settings.SOCIALACCOUNT_PROVIDERS["openid_connect"][
|
keycloak_settings = settings.SOCIALACCOUNT_PROVIDERS["openid_connect"]
|
||||||
"APPS"
|
keycloak_server_url = keycloak_settings["APPS"][0]["settings"]["server_url"]
|
||||||
][0]["settings"]["server_url"]
|
|
||||||
account_url = keycloak_server_url.replace(
|
account_url = keycloak_server_url.replace(
|
||||||
"/.well-known/openid-configuration", "/account"
|
"/.well-known/openid-configuration", "/account"
|
||||||
)
|
)
|
||||||
context["account_href"] = account_url
|
context["account_href"] = account_url
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
def form_valid(self, form):
|
||||||
|
form.save()
|
||||||
|
return super().form_valid(form)
|
||||||
|
|
36
src/servala/frontend/views/mixins.py
Normal file
36
src/servala/frontend/views/mixins.py
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
from django.utils.functional import cached_property
|
||||||
|
from django.views.generic import TemplateView
|
||||||
|
|
||||||
|
|
||||||
|
class HtmxMixin(TemplateView):
|
||||||
|
fragments = []
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def is_htmx(self):
|
||||||
|
return self.request.headers.get("HX-Request")
|
||||||
|
|
||||||
|
def _get_fragment(self):
|
||||||
|
if self.request.method == "POST":
|
||||||
|
fragment = self.request.POST.get("fragment")
|
||||||
|
else:
|
||||||
|
fragment = self.request.GET.get("fragment")
|
||||||
|
if fragment and fragment in self.fragments:
|
||||||
|
return fragment
|
||||||
|
|
||||||
|
def get_template_names(self):
|
||||||
|
template_names = super().get_template_names()
|
||||||
|
if self.is_htmx and (fragment := self._get_fragment()):
|
||||||
|
return [f"{template_names[0]}#{fragment}"]
|
||||||
|
return template_names
|
||||||
|
|
||||||
|
def get_form_kwargs(self):
|
||||||
|
result = super().get_form_kwargs()
|
||||||
|
if self.is_htmx and (field_name := self.request.POST.get("hx-single-field")):
|
||||||
|
result["single_field"] = field_name
|
||||||
|
return result
|
||||||
|
|
||||||
|
def form_valid(self, form):
|
||||||
|
result = super().form_valid(form)
|
||||||
|
if self.is_htmx and self._get_fragment():
|
||||||
|
return self.get(self.request, *self.args, **self.kwargs)
|
||||||
|
return result
|
|
@ -1,4 +1,5 @@
|
||||||
from django.views.generic import FormView
|
from django.shortcuts import redirect
|
||||||
|
from django.views.generic import FormView, TemplateView
|
||||||
|
|
||||||
from servala.frontend.forms import OrganizationCreateForm
|
from servala.frontend.forms import OrganizationCreateForm
|
||||||
|
|
||||||
|
@ -8,8 +9,11 @@ class OrganizationCreateView(FormView):
|
||||||
template_name = "frontend/organizations/create.html"
|
template_name = "frontend/organizations/create.html"
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
form.instance.create_organization(form.instance, owner=self.request.user)
|
instance = form.instance.create_organization(
|
||||||
return super().form_valid(form)
|
form.instance, owner=self.request.user
|
||||||
|
)
|
||||||
|
return redirect(instance.get_absolute_url())
|
||||||
|
|
||||||
def get_success_url(self):
|
|
||||||
return "/"
|
class OrganizationDashboardView(TemplateView):
|
||||||
|
template_name = "frontend/organizations/dashboard.html"
|
||||||
|
|
|
@ -116,6 +116,7 @@ MIDDLEWARE = [
|
||||||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||||||
"allauth.account.middleware.AccountMiddleware",
|
"allauth.account.middleware.AccountMiddleware",
|
||||||
"django.contrib.auth.middleware.LoginRequiredMiddleware",
|
"django.contrib.auth.middleware.LoginRequiredMiddleware",
|
||||||
|
"servala.core.middleware.OrganizationMiddleware",
|
||||||
]
|
]
|
||||||
LOGIN_URL = "account_login"
|
LOGIN_URL = "account_login"
|
||||||
|
|
||||||
|
|
3
src/servala/static/css/servala.css
Normal file
3
src/servala/static/css/servala.css
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
.form-group.d-inline {
|
||||||
|
margin-bottom: 0;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue