Signed-off-by: Marc 'risson' Schmitt <marc.schmitt@risson.space>
This commit is contained in:
Marc 'risson' Schmitt
2025-03-27 14:33:42 +01:00
parent a3ebfd9bbd
commit 43c5c1276d
5 changed files with 215 additions and 15 deletions

64
authentik/tasks/api.py Normal file
View File

@ -0,0 +1,64 @@
from drf_spectacular.utils import OpenApiParameter, extend_schema
from rest_framework.mixins import (
ListModelMixin,
RetrieveModelMixin,
)
from rest_framework.viewsets import GenericViewSet
from authentik.core.api.utils import ModelSerializer
from authentik.tasks.models import Task
from authentik.tasks.schedules.models import Schedule
from authentik.tenants.utils import get_current_tenant
class TaskSerializer(ModelSerializer):
class Meta:
model = Task
fields = [
"message_id",
"queue_name",
"actor_name",
"state",
"mtime",
"schedule_uid",
"uid",
]
class TaskViewSet(
RetrieveModelMixin,
ListModelMixin,
GenericViewSet,
):
serializer_class = TaskSerializer
search_fields = (
"message_id",
"queue_name",
"actor_name",
"state",
"schedule_uid",
)
filterset_fields = (
"queue_name",
"actor_name",
"state",
"schedule_uid",
)
ordering = (
"actor_name",
"-mtime",
)
def get_queryset(self):
qs = Task.objects.filter(tenant=get_current_tenant())
if self.request.query_params.get("exclude_scheduled", "false").lower() == "true":
qs = qs.exclude(schedule_uid__in=Schedule.objects.all().values_list("uid", flat=True))
return qs
@extend_schema(
parameters=[
OpenApiParameter("exclude_scheduled", bool, default=False),
]
)
def list(self, *args, **kwargs):
return super().list(*args, **kwargs)

View File

@ -0,0 +1,21 @@
# Generated by Django 5.0.13 on 2025-03-27 13:33
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
("authentik_tasks", "0002_task_schedule_uid"),
]
operations = [
migrations.AlterModelOptions(
name="task",
options={
"default_permissions": ("view",),
"verbose_name": "Task",
"verbose_name_plural": "Tasks",
},
),
]

View File

@ -8,6 +8,7 @@ from django.utils.translation import gettext_lazy as _
from authentik.events.logs import LogEvent
from authentik.events.utils import sanitize_item
from authentik.lib.models import SerializerModel
from authentik.lib.utils.errors import exception_to_string
from authentik.tenants.models import Tenant
@ -37,8 +38,7 @@ class TaskStatus(models.TextChoices):
ERROR = "error"
# class Task(SerializerModel):
class Task(models.Model):
class Task(SerializerModel):
message_id = models.UUIDField(primary_key=True, default=uuid4)
queue_name = models.TextField(default="default", help_text=_("Queue name"))
@ -69,6 +69,7 @@ class Task(models.Model):
class Meta:
verbose_name = _("Task")
verbose_name_plural = _("Tasks")
default_permissions = ("view",)
indexes = (models.Index(fields=("state", "mtime")),)
triggers = (
pgtrigger.Trigger(
@ -90,10 +91,11 @@ class Task(models.Model):
def __str__(self):
return str(self.message_id)
# @property
# def serializer(self):
# # TODO: fixme
# pass
@property
def serializer(self):
from authentik.tasks.api import TaskSerializer
return TaskSerializer
def set_uid(self, uid: str):
"""Set UID, so in the case of an unexpected error its saved correctly"""

View File

@ -81,6 +81,46 @@
}
}
},
{
"type": "object",
"required": [
"model",
"identifiers"
],
"properties": {
"model": {
"const": "authentik_tasks.task"
},
"id": {
"type": "string"
},
"state": {
"type": "string",
"enum": [
"absent",
"present",
"created",
"must_created"
],
"default": "present"
},
"conditions": {
"type": "array",
"items": {
"type": "boolean"
}
},
"permissions": {
"$ref": "#/$defs/model_authentik_tasks.task_permissions"
},
"attrs": {
"$ref": "#/$defs/model_authentik_tasks.task"
},
"identifiers": {
"$ref": "#/$defs/model_authentik_tasks.task"
}
}
},
{
"type": "object",
"required": [
@ -4129,6 +4169,77 @@
}
}
},
"model_authentik_tasks.task": {
"type": "object",
"properties": {
"message_id": {
"type": "string",
"format": "uuid",
"title": "Message id"
},
"queue_name": {
"type": "string",
"minLength": 1,
"title": "Queue name",
"description": "Queue name"
},
"actor_name": {
"type": "string",
"minLength": 1,
"title": "Actor name",
"description": "Dramatiq actor name"
},
"state": {
"type": "string",
"enum": [
"queued",
"consumed",
"rejected",
"done"
],
"title": "State",
"description": "Task status"
},
"mtime": {
"type": "string",
"format": "date-time",
"title": "Mtime",
"description": "Task last modified time"
},
"schedule_uid": {
"type": "string",
"title": "Schedule uid"
},
"uid": {
"type": "string",
"title": "Uid"
}
},
"required": []
},
"model_authentik_tasks.task_permissions": {
"type": "array",
"items": {
"type": "object",
"required": [
"permission"
],
"properties": {
"permission": {
"type": "string",
"enum": [
"view_task"
]
},
"user": {
"type": "integer"
},
"role": {
"type": "string"
}
}
}
},
"model_authentik_crypto.certificatekeypair": {
"type": "object",
"properties": {
@ -4762,6 +4873,7 @@
"enum": [
null,
"authentik_tenants.domain",
"authentik_tasks.task",
"authentik_crypto.certificatekeypair",
"authentik_flows.flow",
"authentik_flows.flowstagebinding",
@ -7373,9 +7485,6 @@
"authentik_stages_user_write.change_userwritestage",
"authentik_stages_user_write.delete_userwritestage",
"authentik_stages_user_write.view_userwritestage",
"authentik_tasks.add_task",
"authentik_tasks.change_task",
"authentik_tasks.delete_task",
"authentik_tasks.view_task",
"authentik_tasks_schedules.change_schedule",
"authentik_tasks_schedules.view_schedule",
@ -13929,9 +14038,6 @@
"authentik_stages_user_write.change_userwritestage",
"authentik_stages_user_write.delete_userwritestage",
"authentik_stages_user_write.view_userwritestage",
"authentik_tasks.add_task",
"authentik_tasks.change_task",
"authentik_tasks.delete_task",
"authentik_tasks.view_task",
"authentik_tasks_schedules.change_schedule",
"authentik_tasks_schedules.view_schedule",

View File

@ -24426,6 +24426,7 @@ paths:
- authentik_stages_user_login.userloginstage
- authentik_stages_user_logout.userlogoutstage
- authentik_stages_user_write.userwritestage
- authentik_tasks.task
- authentik_tasks_schedules.schedule
- authentik_tenants.domain
required: true
@ -24671,6 +24672,7 @@ paths:
- authentik_stages_user_login.userloginstage
- authentik_stages_user_logout.userlogoutstage
- authentik_stages_user_write.userwritestage
- authentik_tasks.task
- authentik_tasks_schedules.schedule
- authentik_tenants.domain
required: true
@ -38993,7 +38995,8 @@ paths:
name: id
schema:
type: string
description: A unique value identifying this Schedule.
format: uuid
description: A UUID string identifying this Schedule.
required: true
tags:
- tasks
@ -39025,7 +39028,8 @@ paths:
name: id
schema:
type: string
description: A unique value identifying this Schedule.
format: uuid
description: A UUID string identifying this Schedule.
required: true
tags:
- tasks
@ -39063,7 +39067,8 @@ paths:
name: id
schema:
type: string
description: A unique value identifying this Schedule.
format: uuid
description: A UUID string identifying this Schedule.
required: true
tags:
- tasks
@ -46810,6 +46815,7 @@ components:
ModelEnum:
enum:
- authentik_tenants.domain
- authentik_tasks.task
- authentik_crypto.certificatekeypair
- authentik_flows.flow
- authentik_flows.flowstagebinding
@ -56673,6 +56679,7 @@ components:
properties:
id:
type: string
format: uuid
readOnly: true
name:
type: string