2024-12-22 23:46:13 +00:00
|
|
|
"""Registry for services."""
|
|
|
|
|
|
|
|
from django import forms
|
2025-01-15 07:16:12 +00:00
|
|
|
from django.db.models import TextChoices
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2024-12-22 23:46:13 +00:00
|
|
|
from django_registries.registry import Interface
|
|
|
|
from django_registries.registry import Registry
|
|
|
|
|
|
|
|
|
2025-03-03 18:20:28 +00:00
|
|
|
class ServiceRegistry(Registry["ServiceInterface"]):
|
2024-12-22 23:46:13 +00:00
|
|
|
"""Registry for services."""
|
|
|
|
|
|
|
|
implementations_module = "services"
|
|
|
|
|
|
|
|
|
2025-01-15 07:16:12 +00:00
|
|
|
class ServiceRequests(TextChoices):
|
2025-01-26 12:44:10 +00:00
|
|
|
"""Service request choices."""
|
|
|
|
|
2025-01-15 07:16:12 +00:00
|
|
|
CREATION = "CREATION", _("Creation")
|
|
|
|
PASSWORD_RESET = "PASSWORD_RESET", _("Password reset")
|
|
|
|
DELETION = "DELETION", _("Deletion")
|
|
|
|
|
|
|
|
|
|
|
|
DEFAULT_SERVICE_REQUEST_TYPES = [ServiceRequests.CREATION, ServiceRequests.PASSWORD_RESET, ServiceRequests.DELETION]
|
|
|
|
|
|
|
|
|
2024-12-22 23:46:13 +00:00
|
|
|
class ServiceInterface(Interface):
|
|
|
|
"""Interface for services."""
|
|
|
|
|
|
|
|
registry = ServiceRegistry
|
|
|
|
|
|
|
|
name: str
|
|
|
|
description: str
|
|
|
|
url: str
|
|
|
|
|
|
|
|
public: bool = False
|
|
|
|
|
2025-01-15 07:16:12 +00:00
|
|
|
# An auto-created service is added for all new members.
|
|
|
|
# This is a way of saying that the service is "mandatory" to have.
|
|
|
|
auto_create: bool = False
|
|
|
|
|
2025-03-03 18:20:28 +00:00
|
|
|
request_types: list[ServiceRequests] = DEFAULT_SERVICE_REQUEST_TYPES
|
2025-01-15 07:16:12 +00:00
|
|
|
|
2025-03-03 19:23:48 +01:00
|
|
|
subscribe_fields: tuple[tuple[str, forms.Field], ...] = ()
|
2024-12-22 23:46:13 +00:00
|
|
|
|
|
|
|
def get_form_class(self) -> type:
|
|
|
|
"""Get the form class for the service."""
|
|
|
|
return type(
|
|
|
|
"ServiceForm",
|
|
|
|
(forms.Form,),
|
|
|
|
dict(self.subscribe_fields),
|
|
|
|
)
|
2025-01-15 07:16:12 +00:00
|
|
|
|
|
|
|
def __str__(self) -> str:
|
|
|
|
return self.name
|