Skip to content
Snippets Groups Projects
Verified Commit 4a936f85 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Generate scope names for live documents via ORM

parent 251c6a65
No related branches found
No related tags found
1 merge request!21Resolve "Support OAuth2 authentication for LiveDocument PDF view"
Pipeline #39364 passed
from django.apps import apps
from django.utils.translation import gettext as _
from django.db import models
from django.db.models import functions
from django.utils.translation import gettext_lazy as _
from aleksis.core.util.apps import AppConfig
......@@ -22,9 +24,17 @@ class ResintConfig(AppConfig):
def get_all_scopes(cls) -> dict[str, str]:
"""Return all OAuth scopes and their descriptions for this app."""
LiveDocument = apps.get_model("resint", "LiveDocument")
scopes = {}
for live_document in LiveDocument.objects.all():
scopes[live_document.scope] = _("Access PDF file for live document {}").format(
live_document.name
label_prefix = _("Access PDF file for live document")
scopes = dict(
LiveDocument.objects.annotate(
scope=functions.Concat(
models.Value(f"{LiveDocument.SCOPE_PREFIX}_"),
models.F("slug"),
output_field=models.CharField(),
),
label=functions.Concat(models.Value(f"{label_prefix}: "), models.F("name")),
)
.values_list("scope", "label")
.distinct()
)
return scopes
......@@ -149,6 +149,8 @@ class Poster(ExtensibleModel):
class LiveDocument(ExtensiblePolymorphicModel):
"""Model for periodically/automatically updated files."""
SCOPE_PREFIX = "live_document_pdf"
slug = models.SlugField(
verbose_name=_("Slug"),
help_text=_("This will be used for the name of the current PDF file."),
......@@ -196,7 +198,7 @@ class LiveDocument(ExtensiblePolymorphicModel):
@property
def scope(self) -> str:
"""Return OAuth2 scope name to access PDF file via API."""
return f"live_document_pdf_{self.slug}"
return f"{self.SCOPE_PREFIX}_{self.slug}"
def save(self, *args, **kwargs):
with reversion.create_revision():
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment