Skip to content

Commit

Permalink
Add branding endpoints - should probably make representation more gen…
Browse files Browse the repository at this point in the history
…eric?
  • Loading branch information
vanatteveldt committed Feb 6, 2025
1 parent e1baa1d commit d93b3fc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
37 changes: 34 additions & 3 deletions amcat4/api/info.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from fastapi import Request
from fastapi import Depends, Request
from fastapi import APIRouter
from fastapi.templating import Jinja2Templates
from importlib.metadata import version

from pydantic import BaseModel

from amcat4 import elastic
from amcat4.api.auth import get_middlecat_config
from amcat4.config import get_settings
from amcat4.api.auth import authenticated_admin, get_middlecat_config
from amcat4.config import get_settings, validate_settings
from amcat4.index import get_branding, set_branding

templates = Jinja2Templates(directory="templates")

Expand All @@ -28,3 +31,31 @@ def index(request: Request):
except OSError:
pass
return templates.TemplateResponse("index.html", locals())


@app_info.get("/config")
@app_info.get("/middlecat")
def get_auth_config():
return {
"middlecat_url": get_settings().middlecat_url,
"resource": get_settings().host,
"authorization": get_settings().auth,
"warnings": [validate_settings()],
"api_version": version("amcat4"),
}


@app_info.get("/config/branding")
def read_branding():
return get_branding()


class ChangeBranding(BaseModel):
server_name: str
server_icon: str
welcome_text: str


@app_info.put("/config/branding")
def change_branding(data: ChangeBranding, user: str = Depends(authenticated_admin)):
set_branding(server_icon=data.server_icon, server_name=data.server_name, welcome_text=data.welcome_text)
12 changes: 0 additions & 12 deletions amcat4/api/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,3 @@ def modify_user(email: EmailStr, data: ChangeUserForm, _user: str = Depends(auth
role = Role[data.role.upper()]
set_global_role(email, role)
return {"email": email, "role": role.name}


@app_users.get("/config")
@app_users.get("/middlecat")
def get_auth_config():
return {
"middlecat_url": get_settings().middlecat_url,
"resource": get_settings().host,
"authorization": get_settings().auth,
"warnings": [validate_settings()],
"api_version": version("amcat4"),
}
2 changes: 1 addition & 1 deletion amcat4/elastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def get_system_version(elastic=None) -> Optional[int]:
r = elastic.get(index=settings.system_index, id=GLOBAL_ROLES, source_includes="version")
except NotFoundError:
return None
return r["_source"]["version"]
return r["_source"].get("version")


def _setup_elastic():
Expand Down
30 changes: 30 additions & 0 deletions amcat4/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import collections
from enum import IntEnum
import logging
from multiprocessing import Value
from typing import Any, Iterable, Mapping, Optional, Literal

import hashlib
Expand Down Expand Up @@ -570,3 +571,32 @@ def update_documents_by_query(index: str | list[str], query: dict, field: str, v
source="ctx._source[params.field] = params.value", lang="painless", params=dict(field=field, value=value)
)
return es().update_by_query(index=index, query=query, script=script, refresh=True)


def get_branding():
# We (ab)use the _global settings document for this, even if using summary_field for an icon url is a bit weird
# (Maybe we should just add a nested object for more flexibility?)
doc = es().get(
index=get_settings().system_index, id=GLOBAL_ROLES, source_includes=["name", "description", "summary_field"]
)
return dict(
server_name=doc["_source"].get("name"),
welcome_text=doc["_source"].get("description"),
server_icon=doc["_source"].get("summary_field"),
)


def set_branding(server_name: Optional[str] = None, welcome_text: Optional[str] = None, server_icon: Optional[str] = None):
"""Change the branding info for this server. Set params to None to keep unchanged, or to '' to delete the entry"""
doc = {}
if server_name is not None:
doc["name"] = server_name or None
if welcome_text is not None:
doc["description"] = welcome_text or None
if server_icon is not None:
doc["summary_field"] = server_icon or None
if not doc:
# Nothing to do!
return

return es().update(index=get_settings().system_index, id=GLOBAL_ROLES, doc=doc)

0 comments on commit d93b3fc

Please sign in to comment.