sources/ldap: add status display to show last sync
This commit is contained in:
		| @ -5,12 +5,12 @@ from celery.exceptions import CeleryError | ||||
| from django.core.exceptions import DisallowedHost, ValidationError | ||||
| from django.db import InternalError, OperationalError, ProgrammingError | ||||
| from django_redis.exceptions import ConnectionInterrupted | ||||
| from ldap3.core.exceptions import LDAPException | ||||
| from redis.exceptions import ConnectionError as RedisConnectionError | ||||
| from redis.exceptions import RedisError | ||||
| from rest_framework.exceptions import APIException | ||||
| from structlog import get_logger | ||||
| from websockets.exceptions import WebSocketException | ||||
| from ldap3.core.exceptions import LDAPException | ||||
|  | ||||
| LOGGER = get_logger() | ||||
|  | ||||
|  | ||||
| @ -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 | ||||
|  | ||||
| @ -1,4 +1,8 @@ | ||||
| """LDAP Sync tasks""" | ||||
| from time import time | ||||
|  | ||||
| from django.core.cache import cache | ||||
|  | ||||
| from passbook.root.celery import CELERY_APP | ||||
| from passbook.sources.ldap.connector import Connector | ||||
| from passbook.sources.ldap.models import LDAPSource | ||||
| @ -14,8 +18,10 @@ def sync(): | ||||
| @CELERY_APP.task() | ||||
| def sync_single(source_pk): | ||||
|     """Sync a single source""" | ||||
|     source = LDAPSource.objects.get(pk=source_pk) | ||||
|     source: LDAPSource = LDAPSource.objects.get(pk=source_pk) | ||||
|     connector = Connector(source) | ||||
|     connector.sync_users() | ||||
|     connector.sync_groups() | ||||
|     connector.sync_membership() | ||||
|     cache_key = source.state_cache_prefix("last_sync") | ||||
|     cache.set(cache_key, time(), timeout=60 * 60) | ||||
|  | ||||
| @ -0,0 +1,8 @@ | ||||
| {% load humanize %} | ||||
| {% load i18n %} | ||||
|  | ||||
| {% if last_sync %} | ||||
| <i class="fas fa-check pf-m-success"></i> {% blocktrans with last_sync=last_sync|naturaltime %}Synced {{ last_sync }}.{% endblocktrans %} | ||||
| {% else %} | ||||
| <i class="fas fa-times pf-m-danger"></i> Not synced yet/Sync in Progress | ||||
| {% endif %} | ||||
		Reference in New Issue
	
	Block a user
	 Jens Langhammer
					Jens Langhammer