This commit is contained in:
@ -41,3 +41,8 @@ else:
|
||||
|
||||
# Automatically log folks in or show the default log in page?
|
||||
AUTOMATICALLY_LOG_IN = environ.get("AUTOMATICALLY_LOG_IN", False)
|
||||
|
||||
# Nextcloud integration
|
||||
NEXTCLOUD_API_BASE_URL = environ.get("NEXTCLOUD_API_BASE_URL")
|
||||
NEXTCLOUD_APP_PASSWORD = environ.get("NEXTCLOUD_APP_PASSWORD")
|
||||
NEXTCLOUD_USER = environ.get("NEXTCLOUD_USER")
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
from datetime import datetime as dt
|
||||
from datetime import timedelta
|
||||
|
||||
import owncloud
|
||||
from fastapi import Depends, Request
|
||||
from humanize import naturaldelta
|
||||
|
||||
@ -38,6 +39,9 @@ async def get_invites(request: Request, user=Depends(get_user)):
|
||||
all_invites = {}
|
||||
|
||||
for username in await request.app.state.redis.keys("*"):
|
||||
if username == "resource_map":
|
||||
continue
|
||||
|
||||
invites = await request.app.state.redis.get(username)
|
||||
|
||||
for invite in invites:
|
||||
@ -49,3 +53,8 @@ async def get_invites(request: Request, user=Depends(get_user)):
|
||||
all_invites[username] = invites
|
||||
|
||||
return all_invites
|
||||
|
||||
|
||||
async def get_resource_map(request: Request):
|
||||
"""Retrieve the resource map listing."""
|
||||
return await request.app.state.redis.get("resource_map")
|
||||
|
||||
@ -13,23 +13,15 @@ from members_lumbung_space.config import (
|
||||
APP_LOG_LEVEL,
|
||||
APP_SECRET_KEY,
|
||||
APP_THEME,
|
||||
REDIS_DB,
|
||||
REDIS_HOST,
|
||||
REDIS_PORT,
|
||||
STATIC_DIR,
|
||||
TEMPLATE_DIR,
|
||||
)
|
||||
from members_lumbung_space.exceptions import RequiresLoginException
|
||||
from members_lumbung_space.keycloak import init_keycloak
|
||||
from members_lumbung_space.nextcloud import init_resource_map
|
||||
from members_lumbung_space.oidc import init_oidc
|
||||
from members_lumbung_space.redis import Redis
|
||||
from members_lumbung_space.routes import (
|
||||
health,
|
||||
invite,
|
||||
oidc,
|
||||
register,
|
||||
root,
|
||||
)
|
||||
from members_lumbung_space.redis import Redis, init_redis
|
||||
from members_lumbung_space.routes import health, invite, oidc, register, root
|
||||
|
||||
log = logging.getLogger("uvicorn")
|
||||
log.setLevel(APP_LOG_LEVEL)
|
||||
@ -50,10 +42,11 @@ async def http_exception_handler(request, exc):
|
||||
|
||||
@app.on_event("startup")
|
||||
async def startup_event():
|
||||
redis = Redis()
|
||||
app.state.redis = await redis.create_pool(
|
||||
f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}?encoding=utf-8"
|
||||
)
|
||||
await init_redis(app)
|
||||
log.info("Initialised redis connection")
|
||||
|
||||
await init_resource_map(app)
|
||||
log.info("Initialised the resource map")
|
||||
|
||||
|
||||
@app.on_event("shutdown")
|
||||
|
||||
20
members_lumbung_space/nextcloud.py
Normal file
20
members_lumbung_space/nextcloud.py
Normal file
@ -0,0 +1,20 @@
|
||||
"""Nextcloud logic."""
|
||||
|
||||
import owncloud
|
||||
|
||||
|
||||
async def init_resource_map(app):
|
||||
"""Initialise resource map listing."""
|
||||
from members_lumbung_space.config import (
|
||||
NEXTCLOUD_API_BASE_URL,
|
||||
NEXTCLOUD_APP_PASSWORD,
|
||||
NEXTCLOUD_USER,
|
||||
)
|
||||
|
||||
nextcloud = owncloud.Client(f"https://{NEXTCLOUD_API_BASE_URL}")
|
||||
nextcloud.login(NEXTCLOUD_USER, NEXTCLOUD_APP_PASSWORD)
|
||||
|
||||
await app.state.redis.set(
|
||||
"resource_map",
|
||||
[f.get_name() for f in nextcloud.list("/", depth="infinity")],
|
||||
)
|
||||
@ -5,6 +5,15 @@ import json
|
||||
from aioredis import create_redis_pool
|
||||
|
||||
|
||||
async def init_redis(app):
|
||||
from members_lumbung_space.config import REDIS_DB, REDIS_HOST, REDIS_PORT
|
||||
|
||||
redis = Redis()
|
||||
app.state.redis = await redis.create_pool(
|
||||
f"redis://{REDIS_HOST}:{REDIS_PORT}/{REDIS_DB}?encoding=utf-8"
|
||||
)
|
||||
|
||||
|
||||
class Redis:
|
||||
"""Redis cache."""
|
||||
|
||||
|
||||
@ -4,6 +4,7 @@ from fastapi import APIRouter, Depends, Request
|
||||
|
||||
from members_lumbung_space.dependencies import (
|
||||
get_invites,
|
||||
get_resource_map,
|
||||
get_user,
|
||||
logged_in,
|
||||
)
|
||||
@ -13,9 +14,18 @@ router = APIRouter()
|
||||
|
||||
@router.get("/", dependencies=[Depends(logged_in)])
|
||||
async def home(
|
||||
request: Request, user=Depends(get_user), invites=Depends(get_invites)
|
||||
request: Request,
|
||||
user=Depends(get_user),
|
||||
invites=Depends(get_invites),
|
||||
resource_map=Depends(get_resource_map),
|
||||
):
|
||||
context = {"request": request, "user": user, "invites": invites}
|
||||
context = {
|
||||
"request": request,
|
||||
"user": user,
|
||||
"invites": invites,
|
||||
"resource_map": resource_map,
|
||||
}
|
||||
|
||||
return request.app.state.templates.TemplateResponse(
|
||||
"admin.html", context=context
|
||||
)
|
||||
|
||||
@ -2,7 +2,6 @@
|
||||
{% block content %}
|
||||
<p>
|
||||
Hello, {{ user.preferred_username }} 👋
|
||||
<small>(<a href="{{ url_for('logout') }}">logout</a>)</small>
|
||||
</p>
|
||||
|
||||
<div>
|
||||
@ -43,8 +42,14 @@
|
||||
{% endif %}
|
||||
|
||||
<p>
|
||||
<a href="{{ url_for('invite_keycloak_create') }}">Generate an invite link</a>
|
||||
<a href="{{ url_for('invite_keycloak_create') }}">Generate</a>
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<h2>Resource map</h2>
|
||||
<p>(i have no idea how to render this)<p>
|
||||
<div>{{ resource_map }}</div>
|
||||
</div>
|
||||
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user