Commit 57a73687 authored by vanille's avatar vanille Committed by root
Browse files

evict users inactive since too long

parent 649b83cf
Pipeline #9022 passed with stage
in 2 minutes and 46 seconds
......@@ -36,6 +36,8 @@ Fonction :
from __future__ import unicode_literals
from datetime import timedelta
from django.utils import timezone
from django.db.models import Q
from django.contrib.auth.models import Permission
......@@ -61,28 +63,8 @@ def get_group_having_permission(*permission_name):
def all_account(search_time=None, including_asso=True):
""" Fonction renvoyant tous les users adherents. Optimisee pour n'est
qu'une seule requete sql
Inspecte les factures de l'user et ses cotisation, regarde si elles
sont posterieur à now (end_time)"""
if search_time is None:
search_time = timezone.now()
filter_user = Q(
facture__in=Facture.objects.filter(
vente__in=Vente.objects.filter(
Q(type_cotisation="All") | Q(type_cotisation="Adhesion"),
cotisation__in=Cotisation.objects.filter(
vente__in=Vente.objects.filter(
facture__in=Facture.objects.all().exclude(valid=False)
)
).filter(Q(date_start__lt=search_time)),
)
)
)
if including_asso:
asso_user = AssoOption.get_cached_value("utilisateur_asso")
if asso_user:
filter_user |= Q(id=asso_user.id)
return User.objects.filter(filter_user).distinct()
qu'une seule requete sql"""
return User.objects.filter(state=0)
def all_adherent(search_time=None, including_asso=True):
......@@ -177,6 +159,33 @@ def all_has_access(search_time=None, including_asso=True):
return User.objects.filter(filter_user).distinct()
def all_outdated(search_delta=0):
""" Return all users that have not had any connection in the last
search_delta days
----
Renvoie tous les users qui n'ont pas eu de connection depuis
search_delta jours"""
search_time = timezone.now() - timedelta(days=search_delta)
filter_user = (
Q(state=User.STATE_ACTIVE)
& ~Q(
facture__in=Facture.objects.filter(
vente__in=Vente.objects.filter(
cotisation__in=Cotisation.objects.filter(
Q(type_cotisation="All") | Q(type_cotisation="Connexion"),
vente__in=Vente.objects.filter(
facture__in=Facture.objects.all().exclude(valid=False)
),
).filter(
Q(date_end__gt=search_time)
)
)
)
)
)
return User.objects.filter(filter_user).distinct()
def filter_active_interfaces(interface_set):
"""Filtre les machines autorisées à sortir sur internet dans une requête"""
return (
......@@ -228,11 +237,29 @@ def all_active_assigned_interfaces_count():
return all_active_interfaces_count().filter(ipv4__isnull=False)
def remove_room_user(user, kick_clubs=True):
assert u.is_class_adherent or u.is_class_club
if u.is_class_adherent:
u.adherent.room = None
u.adherent.save()
elif kick_clubs:
u.club.room = None
u.club.save()
def remove_user_room(room):
""" Déménage de force l'ancien locataire de la chambre """
try:
user = Adherent.objects.get(room=room)
except Adherent.DoesNotExist:
return
user.room = None
user.save()
remove_room_user(user, kick_clubs=False)
def kick_if_outdated(search_delta=30):
users = all_outdated(search_delta)
for u in users:
if u.room is not None:
print(f"Kicking {u.pseudo} from {u.room}")
remove_room_user(u)
:
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment