8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-26 06:32:26 +00:00
re2o/re2o/utils.py

230 lines
7.7 KiB
Python
Raw Normal View History

2017-10-15 01:47:17 +00:00
# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
# Copyright © 2017 Gabriel Détraz
# Copyright © 2017 Lara Kermarec
2017-10-15 01:47:17 +00:00
# Copyright © 2017 Augustin Lemesle
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# -*- coding: utf-8 -*-
# David Sinquin, Gabriel Détraz, Lara Kermarec
2017-10-15 01:47:17 +00:00
"""
Regroupe les fonctions transversales utiles
Fonction :
- récupérer tous les utilisateurs actifs
- récupérer toutes les machines
- récupérer tous les bans
etc
"""
from __future__ import unicode_literals
from django.utils import timezone
from django.db.models import Q
2019-09-05 23:08:53 +00:00
from django.contrib.auth.models import Permission
2017-10-15 01:47:17 +00:00
2018-04-14 19:29:16 +00:00
from cotisations.models import Cotisation, Facture, Vente
from machines.models import Interface, Machine
from users.models import Adherent, User, Ban, Whitelist
from preferences.models import AssoOption
2017-10-15 01:47:17 +00:00
2019-09-05 23:08:53 +00:00
def get_group_having_permission(*permission_name):
"""Returns every group having the permission `permission_name`
"""
groups = set()
for name in permission_name:
app_label, codename = name.split(".")
permission = Permission.objects.get(
content_type__app_label=app_label, codename=codename
)
2019-09-05 23:08:53 +00:00
groups = groups.union(permission.group_set.all())
return groups
def all_adherent(search_time=None, including_asso=True):
2017-10-15 01:47:17 +00:00
""" 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)"""
2018-02-20 22:58:21 +00:00
if search_time is None:
search_time = timezone.now()
filter_user = Q(
facture__in=Facture.objects.filter(
2017-10-15 01:47:17 +00:00
vente__in=Vente.objects.filter(
Q(type_cotisation="All") | Q(type_cotisation="Adhesion"),
2017-10-15 01:47:17 +00:00
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) & Q(date_end__gt=search_time)),
2017-10-15 01:47:17 +00:00
)
)
)
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()
2017-10-15 01:47:17 +00:00
2018-02-20 22:58:21 +00:00
def all_baned(search_time=None):
2017-10-15 01:47:17 +00:00
""" Fonction renvoyant tous les users bannis """
2018-02-20 22:58:21 +00:00
if search_time is None:
search_time = timezone.now()
2017-10-15 01:47:17 +00:00
return User.objects.filter(
ban__in=Ban.objects.filter(
2018-12-21 23:35:39 +00:00
Q(date_start__lt=search_time) & Q(date_end__gt=search_time)
2017-10-15 01:47:17 +00:00
)
).distinct()
2018-02-20 22:58:21 +00:00
def all_whitelisted(search_time=None):
2017-10-15 01:47:17 +00:00
""" Fonction renvoyant tous les users whitelistes """
2018-02-20 22:58:21 +00:00
if search_time is None:
search_time = timezone.now()
2017-10-15 01:47:17 +00:00
return User.objects.filter(
whitelist__in=Whitelist.objects.filter(
2018-12-21 23:35:39 +00:00
Q(date_start__lt=search_time) & Q(date_end__gt=search_time)
2017-10-15 01:47:17 +00:00
)
).distinct()
def all_has_access(search_time=None, including_asso=True):
""" Return all connected users : active users and whitelisted +
asso_user defined in AssoOption pannel
----
Renvoie tous les users beneficiant d'une connexion
: user adherent et whiteliste non banni plus l'utilisateur asso"""
2018-02-20 22:58:21 +00:00
if search_time is None:
search_time = timezone.now()
filter_user = (
Q(state=User.STATE_ACTIVE)
& ~Q(email_state=User.EMAIL_STATE_UNVERIFIED)
& ~Q(
ban__in=Ban.objects.filter(
Q(date_start__lt=search_time) & Q(date_end__gt=search_time)
)
)
& (
Q(
whitelist__in=Whitelist.objects.filter(
Q(date_start__lt=search_time) & Q(date_end__gt=search_time)
)
)
| 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_start__lt=search_time) & Q(date_end__gt=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()
2017-10-15 01:47:17 +00:00
def filter_active_interfaces(interface_set):
"""Filtre les machines autorisées à sortir sur internet dans une requête"""
return (
interface_set.filter(
machine__in=Machine.objects.filter(user__in=all_has_access()).filter(
active=True
)
)
.select_related("domain")
.select_related("machine")
.select_related("machine_type")
.select_related("ipv4")
.select_related("domain__extension")
.select_related("ipv4__ip_type")
.distinct()
)
2017-10-15 01:47:17 +00:00
def filter_complete_interfaces(interface_set):
"""Appel la fonction précédente avec un prefetch_related ipv6 en plus"""
return filter_active_interfaces(interface_set).prefetch_related("ipv6list")
def all_active_interfaces(full=False):
"""Renvoie l'ensemble des machines autorisées à sortir sur internet """
if full:
return filter_complete_interfaces(Interface.objects)
else:
return filter_active_interfaces(Interface.objects)
def all_active_assigned_interfaces(full=False):
2017-10-15 01:47:17 +00:00
""" Renvoie l'ensemble des machines qui ont une ipv4 assignées et
disposant de l'accès internet"""
return all_active_interfaces(full=full).filter(ipv4__isnull=False)
2017-10-15 01:47:17 +00:00
def all_active_interfaces_count():
""" Version light seulement pour compter"""
return Interface.objects.filter(
machine__in=Machine.objects.filter(user__in=all_has_access()).filter(
active=True
)
2017-10-15 01:47:17 +00:00
)
def all_active_assigned_interfaces_count():
""" Version light seulement pour compter"""
return all_active_interfaces_count().filter(ipv4__isnull=False)
2018-04-13 23:42:22 +00:00
2020-04-15 19:04:14 +00:00
def remove_user_room(room, force=True):
""" Déménage de force l'ancien locataire de la chambre """
try:
user = Adherent.objects.get(room=room)
except Adherent.DoesNotExist:
return
2020-04-15 19:04:14 +00:00
if force or not user.has_access():
user.room = None
user.save()
2020-04-21 15:37:17 +00:00
def permission_tree(queryset=None):
r = {}
permissions = queryset or Permission.objects.all()
for p in permissions:
key, app, model = p.natural_key()
name = p.name
if app not in r:
r[app] = {}
if model not in r[app]:
r[app][model] = {}
r[app][model][key] = p
return r