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 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
|
2017-10-25 21:37:12 +00:00
|
|
|
|
from users.models import Adherent, User, Ban, Whitelist
|
2017-10-15 01:47:17 +00:00
|
|
|
|
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(
|
2017-10-28 02:59:40 +00:00
|
|
|
|
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(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(
|
2017-10-28 02:59:40 +00:00
|
|
|
|
Q(type_cotisation='All') | Q(type_cotisation='Connexion'),
|
2017-10-15 01:47:17 +00:00
|
|
|
|
vente__in=Vente.objects.filter(
|
|
|
|
|
facture__in=Facture.objects.all()
|
|
|
|
|
.exclude(valid=False)
|
|
|
|
|
)
|
|
|
|
|
).filter(date_end__gt=search_time)
|
|
|
|
|
)
|
|
|
|
|
)))
|
|
|
|
|
).distinct()
|
|
|
|
|
|
|
|
|
|
|
2017-10-15 16:35:35 +00:00
|
|
|
|
def filter_active_interfaces(interface_set):
|
|
|
|
|
"""Filtre les machines autorisées à sortir sur internet dans une requête"""
|
|
|
|
|
return interface_set.filter(
|
2017-10-15 01:47:17 +00:00
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
|
2017-10-15 16:35:35 +00:00
|
|
|
|
def all_active_interfaces():
|
|
|
|
|
"""Renvoie l'ensemble des machines autorisées à sortir sur internet """
|
|
|
|
|
return filter_active_interfaces(Interface.objects)
|
|
|
|
|
|
|
|
|
|
|
2017-10-15 01:47:17 +00:00
|
|
|
|
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)
|
2017-10-21 15:47:42 +00:00
|
|
|
|
|
|
|
|
|
class SortTable:
|
|
|
|
|
""" Class gathering uselful stuff to sort the colums of a table, according
|
|
|
|
|
to the column and order requested. It's used with a dict of possible
|
|
|
|
|
values and associated model_fields """
|
|
|
|
|
|
2017-10-22 01:37:27 +00:00
|
|
|
|
# All the possible possible values
|
2017-10-21 15:47:42 +00:00
|
|
|
|
# The naming convention is based on the URL or the views function
|
2017-10-22 01:37:27 +00:00
|
|
|
|
# The syntax to describe the sort to apply is a dict where the keys are
|
|
|
|
|
# the url value and the values are a list of model field name to use to
|
|
|
|
|
# order the request. They are applied in the order they are given.
|
|
|
|
|
# A 'default' might be provided to specify what to do if the requested col
|
|
|
|
|
# doesn't match any keys.
|
2017-10-21 15:47:42 +00:00
|
|
|
|
USERS_INDEX = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'user_name': ['name'],
|
|
|
|
|
'user_surname': ['surname'],
|
|
|
|
|
'user_pseudo': ['pseudo'],
|
|
|
|
|
'user_room': ['room'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['state', 'pseudo']
|
2017-10-21 21:45:21 +00:00
|
|
|
|
}
|
|
|
|
|
USERS_INDEX_BAN = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'ban_user': ['user__pseudo'],
|
|
|
|
|
'ban_start': ['date_start'],
|
|
|
|
|
'ban_end': ['date_end'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['-date_end']
|
2017-10-21 21:45:21 +00:00
|
|
|
|
}
|
|
|
|
|
USERS_INDEX_WHITE = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'white_user': ['user__pseudo'],
|
|
|
|
|
'white_start': ['date_start'],
|
|
|
|
|
'white_end': ['date_end'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['-date_end']
|
2017-10-21 15:47:42 +00:00
|
|
|
|
}
|
2017-10-22 00:33:44 +00:00
|
|
|
|
MACHINES_INDEX = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'machine_name': ['name'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['pk']
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
|
|
|
|
COTISATIONS_INDEX = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'cotis_user': ['user__pseudo'],
|
|
|
|
|
'cotis_paiement': ['paiement__moyen'],
|
|
|
|
|
'cotis_date': ['date'],
|
2017-10-27 20:08:53 +00:00
|
|
|
|
'cotis_id': ['id'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['-date']
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
|
|
|
|
COTISATIONS_CONTROL = {
|
2017-10-27 20:08:53 +00:00
|
|
|
|
'control_name': ['user__adherent__name'],
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'control_surname': ['user__surname'],
|
|
|
|
|
'control_paiement': ['paiement'],
|
|
|
|
|
'control_date': ['date'],
|
|
|
|
|
'control_valid': ['valid'],
|
|
|
|
|
'control_control': ['control'],
|
2017-10-27 20:08:53 +00:00
|
|
|
|
'control_id': ['id'],
|
|
|
|
|
'control_user-id': ['user__id'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['-date']
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
|
|
|
|
TOPOLOGIE_INDEX = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'switch_dns': ['switch_interface__domain__name'],
|
|
|
|
|
'switch_ip': ['switch_interface__ipv4__ipv4'],
|
|
|
|
|
'switch_loc': ['location'],
|
|
|
|
|
'switch_ports': ['number'],
|
|
|
|
|
'switch_stack': ['stack__name'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['location', 'stack', 'stack_member_id']
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
|
|
|
|
TOPOLOGIE_INDEX_PORT = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'port_port': ['port'],
|
|
|
|
|
'port_room': ['room__name'],
|
|
|
|
|
'port_interface': ['machine_interface__domain__name'],
|
|
|
|
|
'port_related': ['related__switch__name'],
|
|
|
|
|
'port_radius': ['radius'],
|
|
|
|
|
'port_vlan': ['vlan_force__name'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['port']
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
|
|
|
|
TOPOLOGIE_INDEX_ROOM = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'room_name': ['name'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['name']
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
|
|
|
|
TOPOLOGIE_INDEX_STACK = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'stack_name': ['name'],
|
|
|
|
|
'stack_id': ['stack_id'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['stack_id'],
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
2017-10-26 03:07:11 +00:00
|
|
|
|
TOPOLOGIE_INDEX_MODEL_SWITCH = {
|
|
|
|
|
'model_switch_name': ['reference'],
|
|
|
|
|
'model_switch__contructor' : ['constructor__name'],
|
|
|
|
|
'default': ['reference'],
|
|
|
|
|
}
|
|
|
|
|
TOPOLOGIE_INDEX_CONSTRUCTOR_SWITCH = {
|
|
|
|
|
'room_name': ['name'],
|
|
|
|
|
'default': ['name'],
|
|
|
|
|
}
|
2017-10-22 00:33:44 +00:00
|
|
|
|
LOGS_INDEX = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'sum_date': ['revision__date_created'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['-revision__date_created'],
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
|
|
|
|
LOGS_STATS_LOGS = {
|
2017-10-22 15:48:35 +00:00
|
|
|
|
'logs_author': ['user__name'],
|
|
|
|
|
'logs_date': ['date_created'],
|
2017-10-22 01:37:27 +00:00
|
|
|
|
'default': ['-date_created']
|
2017-10-22 00:33:44 +00:00
|
|
|
|
}
|
2017-10-21 15:47:42 +00:00
|
|
|
|
|
|
|
|
|
@staticmethod
|
2017-10-22 01:37:27 +00:00
|
|
|
|
def sort(request, col, order, values):
|
2017-10-21 15:47:42 +00:00
|
|
|
|
""" Check if the given values are possible and add .order_by() and
|
|
|
|
|
a .reverse() as specified according to those values """
|
2017-10-22 01:37:27 +00:00
|
|
|
|
fields = values.get(col, None)
|
|
|
|
|
if not fields:
|
|
|
|
|
fields = values.get('default', [])
|
|
|
|
|
request = request.order_by(*fields)
|
2017-11-02 00:27:31 +00:00
|
|
|
|
if values.get(col, None) and order == 'desc':
|
2017-10-22 01:37:27 +00:00
|
|
|
|
return request.reverse()
|
2017-10-21 15:47:42 +00:00
|
|
|
|
else:
|
2017-10-22 01:37:27 +00:00
|
|
|
|
return request
|
2017-10-25 21:37:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|