sources/ldap: add status display to show last sync

This commit is contained in:
Jens Langhammer
2020-09-19 15:25:17 +02:00
parent 438250b3a9
commit 116be0b3c0
4 changed files with 33 additions and 2 deletions

View File

@ -1,6 +1,8 @@
"""passbook LDAP Models"""
from datetime import datetime
from typing import Optional, Type
from django.core.cache import cache
from django.db import models
from django.forms import ModelForm
from django.utils.translation import gettext_lazy as _
@ -8,6 +10,7 @@ from ldap3 import Connection, Server
from passbook.core.models import Group, PropertyMapping, Source
from passbook.lib.models import DomainlessURLValidator
from passbook.lib.utils.template import render_to_string
class LDAPSource(Source):
@ -59,6 +62,20 @@ class LDAPSource(Source):
return LDAPSourceForm
def state_cache_prefix(self, suffix: str) -> str:
"""Key by which the ldap source status is saved"""
return f"source_ldap_{self.pk}_state_{suffix}"
@property
def ui_additional_info(self) -> str:
last_sync = cache.get(self.state_cache_prefix("last_sync"), None)
if last_sync:
last_sync = datetime.fromtimestamp(last_sync)
return render_to_string(
"ldap/source_list_status.html", {"source": self, "last_sync": last_sync}
)
_connection: Optional[Connection] = None
@property