From 1263784154b9b665257a1376e1d759099a18eba9 Mon Sep 17 00:00:00 2001 From: chirac Date: Sun, 15 Oct 2017 03:47:17 +0200 Subject: [PATCH] Menage --- logs/views.py | 9 ++- machines/views.py | 24 +------- re2o/utils.py | 136 ++++++++++++++++++++++++++++++++++++++++++++++ users/models.py | 56 ------------------- users/views.py | 4 +- 5 files changed, 144 insertions(+), 85 deletions(-) create mode 100644 re2o/utils.py diff --git a/logs/views.py b/logs/views.py index 13879c86..e21d4de6 100644 --- a/logs/views.py +++ b/logs/views.py @@ -47,18 +47,17 @@ from reversion.models import Revision from reversion.models import Version, ContentType from users.models import User, ServiceUser, Right, School, ListRight, ListShell -from users.models import Ban, Whitelist, all_has_access -from users.models import all_whitelisted, all_baned, all_adherent +from users.models import Ban, Whitelist from cotisations.models import Facture, Vente, Article, Banque, Paiement from cotisations.models import Cotisation from machines.models import Machine, MachineType, IpType, Extension, Interface from machines.models import Domain, IpList -from machines.views import all_active_assigned_interfaces_count -from machines.views import all_active_interfaces_count from topologie.models import Switch, Port, Room from preferences.models import GeneralOption from re2o.views import form - +from re2o.utils import all_whitelisted, all_baned, all_has_access, all_adherent +from re2o.utils import all_active_assigned_interfaces_count +from re2o.utils import all_active_interfaces_count STATS_DICT = { 0: ["Tout", 36], diff --git a/machines/views.py b/machines/views.py index 0e00dc67..0bfe36f0 100644 --- a/machines/views.py +++ b/machines/views.py @@ -53,30 +53,10 @@ from .forms import EditIpTypeForm, IpTypeForm, DelIpTypeForm, DomainForm, AliasF from .forms import EditOuverturePortListForm, EditOuverturePortConfigForm from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Domain, Service, Service_link, Vlan, Nas, Text, OuverturePortList, OuverturePort from users.models import User -from users.models import all_has_access from preferences.models import GeneralOption, OptionalMachine from re2o.templatetags.bootstrap_form_typeahead import hidden_id, input_id - -def all_active_interfaces(): - """Renvoie l'ensemble des machines autorisées à sortir sur internet """ - return Interface.objects.filter(machine__in=Machine.objects.filter(user__in=all_has_access()).filter(active=True)).select_related('domain').select_related('machine').select_related('type').select_related('ipv4').select_related('domain__extension').select_related('ipv4__ip_type').distinct() - -def all_active_assigned_interfaces(): - """ Renvoie l'ensemble des machines qui ont une ipv4 assignées et disposant de l'accès internet""" - return all_active_interfaces().filter(ipv4__isnull=False) - -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)) - -def all_active_assigned_interfaces_count(): - """ Version light seulement pour compter""" - return all_active_interfaces_count().filter(ipv4__isnull=False) - -def form(ctx, template, request): - c = ctx - c.update(csrf(request)) - return render(request, template, c) +from re2o.utils import all_active_assigned_interfaces, all_has_access +from re2o.views import form def f_type_id( is_type_tt ): """ The id that will be used in HTML to store the value of the field diff --git a/re2o/utils.py b/re2o/utils.py new file mode 100644 index 00000000..e2ca6db9 --- /dev/null +++ b/re2o/utils.py @@ -0,0 +1,136 @@ +# -*- 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 Goulven Kermarec +# 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, Goulven Kermarec +""" +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 + +from cotisations.models import Cotisation, Facture, Paiement, Vente +from machines.models import Domain, Interface, Machine +from users.models import User, Ban, Whitelist +from preferences.models import Service + +DT_NOW = timezone.now() + + +def all_adherent(search_time=DT_NOW): + """ 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)""" + return User.objects.filter( + facture__in=Facture.objects.filter( + vente__in=Vente.objects.filter( + cotisation__in=Cotisation.objects.filter( + vente__in=Vente.objects.filter( + facture__in=Facture.objects.all().exclude(valid=False) + ) + ).filter(date_end__gt=search_time) + ) + ) + ).distinct() + + +def all_baned(search_time=DT_NOW): + """ Fonction renvoyant tous les users bannis """ + return User.objects.filter( + ban__in=Ban.objects.filter( + date_end__gt=search_time + ) + ).distinct() + + +def all_whitelisted(search_time=DT_NOW): + """ Fonction renvoyant tous les users whitelistes """ + return User.objects.filter( + whitelist__in=Whitelist.objects.filter( + date_end__gt=search_time + ) + ).distinct() + + +def all_has_access(search_time=DT_NOW): + """ Renvoie tous les users beneficiant d'une connexion + : user adherent ou whiteliste et non banni """ + return User.objects.filter( + Q(state=User.STATE_ACTIVE) & + ~Q(ban__in=Ban.objects.filter(date_end__gt=search_time)) & + (Q(whitelist__in=Whitelist.objects.filter(date_end__gt=search_time)) | + Q(facture__in=Facture.objects.filter( + vente__in=Vente.objects.filter( + cotisation__in=Cotisation.objects.filter( + vente__in=Vente.objects.filter( + facture__in=Facture.objects.all() + .exclude(valid=False) + ) + ).filter(date_end__gt=search_time) + ) + ))) + ).distinct() + + +def all_active_interfaces(): + """Renvoie l'ensemble des machines autorisées à sortir sur internet """ + return Interface.objects.filter( + machine__in=Machine.objects.filter( + user__in=all_has_access() + ).filter(active=True) + ).select_related('domain').select_related('machine')\ + .select_related('type').select_related('ipv4')\ + .select_related('domain__extension').select_related('ipv4__ip_type')\ + .distinct() + + +def all_active_assigned_interfaces(): + """ Renvoie l'ensemble des machines qui ont une ipv4 assignées et + disposant de l'accès internet""" + return all_active_interfaces().filter(ipv4__isnull=False) + + +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) + ) + + +def all_active_assigned_interfaces_count(): + """ Version light seulement pour compter""" + return all_active_interfaces_count().filter(ipv4__isnull=False) diff --git a/users/models.py b/users/models.py index a0ef29f8..2f8f888f 100644 --- a/users/models.py +++ b/users/models.py @@ -144,62 +144,6 @@ def get_admin_right(): return admin_right -def all_adherent(search_time=DT_NOW): - """ 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)""" - return User.objects.filter( - facture__in=Facture.objects.filter( - vente__in=Vente.objects.filter( - cotisation__in=Cotisation.objects.filter( - vente__in=Vente.objects.filter( - facture__in=Facture.objects.all().exclude(valid=False) - ) - ).filter(date_end__gt=search_time) - ) - ) - ).distinct() - - -def all_baned(search_time=DT_NOW): - """ Fonction renvoyant tous les users bannis """ - return User.objects.filter( - ban__in=Ban.objects.filter( - date_end__gt=search_time - ) - ).distinct() - - -def all_whitelisted(search_time=DT_NOW): - """ Fonction renvoyant tous les users whitelistes """ - return User.objects.filter( - whitelist__in=Whitelist.objects.filter( - date_end__gt=search_time - ) - ).distinct() - - -def all_has_access(search_time=DT_NOW): - """ Renvoie tous les users beneficiant d'une connexion - : user adherent ou whiteliste et non banni """ - return User.objects.filter( - Q(state=User.STATE_ACTIVE) & - ~Q(ban__in=Ban.objects.filter(date_end__gt=search_time)) & - (Q(whitelist__in=Whitelist.objects.filter(date_end__gt=search_time)) | - Q(facture__in=Facture.objects.filter( - vente__in=Vente.objects.filter( - cotisation__in=Cotisation.objects.filter( - vente__in=Vente.objects.filter( - facture__in=Facture.objects.all() - .exclude(valid=False) - ) - ).filter(date_end__gt=search_time) - ) - ))) - ).distinct() - - class UserManager(BaseUserManager): """User manager basique de django""" def _create_user( diff --git a/users/views.py b/users/views.py index e553f5b8..5b0b3910 100644 --- a/users/views.py +++ b/users/views.py @@ -53,7 +53,7 @@ from reversion.models import Version from reversion import revisions as reversion from users.serializers import MailSerializer from users.models import User, Right, Ban, Whitelist, School, ListRight -from users.models import Request, ServiceUser, all_has_access +from users.models import Request, ServiceUser from users.forms import DelRightForm, BanForm, WhitelistForm, DelSchoolForm from users.forms import DelListRightForm, NewListRightForm from users.forms import InfoForm, BaseInfoForm, StateForm @@ -65,7 +65,7 @@ from machines.models import Machine from preferences.models import OptionalUser, GeneralOption from re2o.views import form - +from re2o.utils import all_has_access def password_change_action(u_form, user, request, req=False): """ Fonction qui effectue le changeemnt de mdp bdd"""