Skip to content

Commit

Permalink
Cache email results.
Browse files Browse the repository at this point in the history
  • Loading branch information
antarcticrainforest committed Aug 12, 2024
1 parent b3f90e6 commit 5843da1
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 36 deletions.
3 changes: 3 additions & 0 deletions django_evaluation/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import BaseBackend

from django_evaluation.utils import sync_mail_users


class OIDCPasswordBackend(BaseBackend):
"""
Expand Down Expand Up @@ -67,6 +69,7 @@ def authenticate(
user.last_name = user_info["last_name"]
user.first_name = user_info["first_name"]
user.save()
sync_mail_users(oneshot=True)
return user
return None

Expand Down
18 changes: 6 additions & 12 deletions django_evaluation/settings/local.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ def _set_favicon(html_color: str, project_root: Path) -> None:
if not DEV:
STATIC_ROOT = str(Path(PROJECT_ROOT) / "static")

INSTITUTION_LOGO = _get_logo(
web_config.get("institution_logo", ""), PROJECT_ROOT
)
INSTITUTION_LOGO = _get_logo(web_config.get("institution_logo", ""), PROJECT_ROOT)
FREVA_LOGO = f"{STATIC_URL}img/by_freva_transparent.png"
MAIN_COLOR = _get_conf_key(web_config, "main_color", "Tomato", False)
_set_favicon(MAIN_COLOR, Path(PROJECT_ROOT))
Expand All @@ -150,9 +148,7 @@ def _set_favicon(html_color: str, project_root: Path) -> None:
"Germany",
]
HOMEPAGE_HEADING = web_config.get("homepage_heading") or "Lorem ipsum dolor."
ABOUT_US_TEXT = (
web_config.get("about_us_text") or "Hello world, this is freva."
)
ABOUT_US_TEXT = web_config.get("about_us_text") or "Hello world, this is freva."
CONTACTS = web_config.get("contacts") or ["[email protected]"]
if isinstance(CONTACTS, str):
CONTACTS = [c for c in CONTACTS.split(",") if c.strip()]
Expand Down Expand Up @@ -248,11 +244,11 @@ def _set_favicon(html_color: str, project_root: Path) -> None:
EMAIL_HOST = os.environ.get("EMAIL_HOST", "mailhost.dkrz.de")

email_secrets = _read_secret()
EMAIL_HOST_USER = email_secrets.get("username")
EMAIL_HOST_PASSWORD = email_secrets.get("password")
EMAIL_HOST_USER = email_secrets.get("username") or "k204230"
EMAIL_HOST_PASSWORD = email_secrets.get("password") or "Schw4r!zk0pff"

EMAIL_USE_TLS = True
EMAIL_PORT = os.environ.get("EMAIL_PORT", 25)
EMAIL_PORT = os.environ.get("EMAIL_PORT", 587)

HOME_DIRS_AVAILABLE = False

Expand All @@ -267,9 +263,7 @@ def _set_favicon(html_color: str, project_root: Path) -> None:
# Provide a full list of all valid hosts (including the http(s):// prefix) which are expected
CSRF_TRUSTED_ORIGINS = [
h.strip()
for h in os.environ.get("CSRF_TRUSTED_ORIGINS", "http://localhost").split(
","
)
for h in os.environ.get("CSRF_TRUSTED_ORIGINS", "http://localhost").split(",")
if h.strip()
]

Expand Down
29 changes: 29 additions & 0 deletions django_evaluation/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@

from __future__ import annotations

import json
import threading
import time
from typing import Any, Callable

from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.utils.safestring import mark_safe


def background(func: Callable[..., Any]) -> Callable[..., threading.Thread]:
"""Decorator for running a given function in the background.
Expand All @@ -16,7 +22,30 @@ def call_func(*args: Any, **kwargs: Any) -> threading.Thread:
"""Call the function in a backround setting."""

thread = threading.Thread(target=func, args=args, kwargs=kwargs)
thread.daemon = True
thread.start()
return thread

return call_func


@background
def sync_mail_users(refresh_itervall: int = 3600, oneshot: bool = False) -> None:
"""Synchronise all users with an email address in the background."""
user_model = get_user_model()
while True:
user_info = [
{
"id": u.username,
"text": f"{u.first_name}, {u.last_name} ({u.email})",
}
for u in user_model.objects.exclude(email__exact="").exclude(
email__isnull=True
)
]
cache.set(
"user_email_info", mark_safe(json.dumps(user_info)), refresh_itervall + 10
)
if oneshot is True:
break
time.sleep(refresh_itervall)
4 changes: 4 additions & 0 deletions django_evaluation/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import os
import site
import sys
import time

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__) + "../../")

Expand Down Expand Up @@ -49,4 +50,7 @@
# setting points here.
from django.core.wsgi import get_wsgi_application

from django_evaluation.utils import sync_mail_users

sync_mail_users(oneshot=False)
application = get_wsgi_application()
27 changes: 16 additions & 11 deletions history/templatetags/dialogtags.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django import template
from django.contrib.auth import get_user_model
from django.core.cache import cache
from django.utils.safestring import mark_safe

from django_evaluation import settings
Expand Down Expand Up @@ -32,15 +33,19 @@ def caption_dialog(current, default, history_object, user):
@register.inclusion_tag("history/templatetags/mailfield.html")
def mailfield(is_guest):
"""Extract the email information from users that have been logged in."""
data = []
data = mark_safe(json.dumps([]))
if not is_guest:
data = [
{
"id": u.username,
"text": f"{u.first_name}, {u.last_name} ({u.email})",
}
for u in get_user_model()
.objects.exclude(email__exact="")
.exclude(email__isnull=True)
]
return {"user_data": mark_safe(json.dumps(data)), "is_guest": is_guest}
data = cache.get("user_email_info") or mark_safe(
json.dumps(
[
{
"id": u.username,
"text": f"{u.first_name}, {u.last_name} ({u.email})",
}
for u in get_user_model()
.objects.exclude(email__exact="")
.exclude(email__isnull=True)
]
)
)
return {"user_data": data, "is_guest": is_guest}
8 changes: 4 additions & 4 deletions history/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from pathlib import Path

import evaluation_system.api.plugin_manager as pm
from base.exceptions import UserNotFoundError
from base.Users import OpenIdUser
from datatableview import Datatable, columns
from datatableview.views import DatatableView
from django.contrib.auth.decorators import login_required
Expand All @@ -17,18 +15,20 @@
from django.urls import reverse
from django.utils.html import escape
from django.views.decorators.debug import sensitive_post_parameters
from django_evaluation import settings
from evaluation_system.api.workload_manager import get_job_class
from evaluation_system.misc import config as eval_config
from evaluation_system.misc.exceptions import PluginManagerException
from evaluation_system.model.db import UserDB
from evaluation_system.model.history.models import History, ResultTag
from evaluation_system.model.user import User
from plugins.utils import get_scheduler_hosts, ssh_call

from base.exceptions import UserNotFoundError
from base.Users import OpenIdUser
from django_evaluation import settings
from history.models import HistoryTag
from history.templatetags.resulttags import mask_uid
from history.utils import FileDict, sendmail_to_follower
from plugins.utils import get_scheduler_hosts, ssh_call


class HistoryDatatable(Datatable):
Expand Down
19 changes: 11 additions & 8 deletions plugins/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@
from pathlib import Path

import evaluation_system.api.plugin_manager as pm
from base.exceptions import UserNotFoundError
from base.Users import OpenIdUser
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, JsonResponse
from django.shortcuts import redirect, render
from django.views.decorators.debug import (sensitive_post_parameters,
sensitive_variables)
from django_evaluation.settings.local import HOME_DIRS_AVAILABLE
from django.views.decorators.debug import sensitive_post_parameters, sensitive_variables
from evaluation_system.misc import config
from evaluation_system.model.user import User
from history.models import Configuration, History

from base.exceptions import UserNotFoundError
from base.Users import OpenIdUser
from django_evaluation.settings.local import HOME_DIRS_AVAILABLE
from history.models import Configuration, History
from plugins.forms import PluginForm, PluginWeb
from plugins.utils import (get_plugin_or_404, get_scheduler_hosts,
is_path_relative_to, ssh_call)
from plugins.utils import (
get_plugin_or_404,
get_scheduler_hosts,
is_path_relative_to,
ssh_call,
)


@login_required()
Expand Down
3 changes: 2 additions & 1 deletion webpack/webpack.dev.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const mode = "development";
const isDevServer = !!(process.env.npm_lifecycle_event === "dev");

const SERVER_HOST = "127.0.0.1";
const SERVER_PORT = 8080;
const SERVER_PORT = 8086;

const entry = isDevServer
? [`webpack-dev-server/client`, "./assets/js/index"]
Expand All @@ -25,6 +25,7 @@ if (isDevServer) {
allowedHosts: "all",
hot: true,
historyApiFallback: true,
port: SERVER_PORT,
client: {
logging: "verbose",
webSocketURL: `ws://${SERVER_HOST}:${SERVER_PORT}/ws`,
Expand Down

0 comments on commit 5843da1

Please sign in to comment.