mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-09 03:16:25 +00:00
Factorisation de code.
This commit is contained in:
parent
2f6a2eb083
commit
b84f654fb3
1 changed files with 96 additions and 17 deletions
|
@ -43,31 +43,107 @@ from django.contrib.auth import authenticate, login
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
|
||||||
from rest_framework.renderers import JSONRenderer
|
from rest_framework.renderers import JSONRenderer
|
||||||
from machines.serializers import FullInterfaceSerializer, InterfaceSerializer, TypeSerializer, DomainSerializer, TextSerializer, MxSerializer, ExtensionSerializer, ServiceServersSerializer, NsSerializer, OuverturePortsSerializer
|
from machines.serializers import ( FullInterfaceSerializer,
|
||||||
|
InterfaceSerializer,
|
||||||
|
TypeSerializer,
|
||||||
|
DomainSerializer,
|
||||||
|
TextSerializer,
|
||||||
|
MxSerializer,
|
||||||
|
ExtensionSerializer,
|
||||||
|
ServiceServersSerializer,
|
||||||
|
NsSerializer,
|
||||||
|
OuverturePortsSerializer
|
||||||
|
)
|
||||||
from reversion import revisions as reversion
|
from reversion import revisions as reversion
|
||||||
from reversion.models import Version
|
from reversion.models import Version
|
||||||
|
|
||||||
import re
|
import re
|
||||||
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm
|
from .forms import (
|
||||||
from .forms import EditIpTypeForm, IpTypeForm, DelIpTypeForm, DomainForm, AliasForm, DelAliasForm, NsForm, DelNsForm, TextForm, DelTextForm, MxForm, DelMxForm, VlanForm, DelVlanForm, ServiceForm, DelServiceForm, NasForm, DelNasForm
|
NewMachineForm,
|
||||||
|
EditMachineForm,
|
||||||
|
EditInterfaceForm,
|
||||||
|
AddInterfaceForm,
|
||||||
|
MachineTypeForm,
|
||||||
|
DelMachineTypeForm,
|
||||||
|
ExtensionForm,
|
||||||
|
DelExtensionForm,
|
||||||
|
BaseEditInterfaceForm,
|
||||||
|
BaseEditMachineForm
|
||||||
|
)
|
||||||
|
from .forms import (
|
||||||
|
EditIpTypeForm,
|
||||||
|
IpTypeForm,
|
||||||
|
DelIpTypeForm,
|
||||||
|
DomainForm,
|
||||||
|
AliasForm,
|
||||||
|
DelAliasForm,
|
||||||
|
NsForm,
|
||||||
|
DelNsForm,
|
||||||
|
TextForm,
|
||||||
|
DelTextForm,
|
||||||
|
MxForm,
|
||||||
|
DelMxForm,
|
||||||
|
VlanForm,
|
||||||
|
DelVlanForm,
|
||||||
|
ServiceForm,
|
||||||
|
DelServiceForm,
|
||||||
|
NasForm,
|
||||||
|
DelNasForm
|
||||||
|
)
|
||||||
from .forms import EditOuverturePortListForm, EditOuverturePortConfigForm
|
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 .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 User
|
||||||
from users.models import all_has_access
|
from users.models import all_has_access
|
||||||
from preferences.models import GeneralOption, OptionalMachine
|
from preferences.models import GeneralOption, OptionalMachine
|
||||||
from .templatetags.bootstrap_form_typeahead import hidden_id, input_id
|
from .templatetags.bootstrap_form_typeahead import hidden_id, input_id
|
||||||
|
|
||||||
|
def filter_active_interfaces(q):
|
||||||
|
"""Filtre les machines autorisées à sortir sur internet dans une requête"""
|
||||||
|
return q.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_interfaces():
|
def all_active_interfaces():
|
||||||
"""Renvoie l'ensemble des machines autorisées à sortir sur internet """
|
"""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()
|
return filter_active_interfaces(Interface.objects)
|
||||||
|
|
||||||
def all_active_assigned_interfaces():
|
def all_active_assigned_interfaces():
|
||||||
""" Renvoie l'ensemble des machines qui ont une ipv4 assignées et disposant de l'accès internet"""
|
"""
|
||||||
|
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)
|
return all_active_interfaces().filter(ipv4__isnull=False)
|
||||||
|
|
||||||
def all_active_interfaces_count():
|
def all_active_interfaces_count():
|
||||||
""" Version light seulement pour compter"""
|
""" Version light seulement pour compter"""
|
||||||
return Interface.objects.filter(machine__in=Machine.objects.filter(user__in=all_has_access()).filter(active=True))
|
return Interface.objects.filter(
|
||||||
|
machine__in=Machine.objects.filter(user__in=all_has_access())\
|
||||||
|
.filter(active=True)
|
||||||
|
)
|
||||||
|
|
||||||
def all_active_assigned_interfaces_count():
|
def all_active_assigned_interfaces_count():
|
||||||
""" Version light seulement pour compter"""
|
""" Version light seulement pour compter"""
|
||||||
|
@ -92,7 +168,8 @@ def generate_ipv4_choices( form ) :
|
||||||
choices = '{"":[{key:"",value:"Choisissez d\'abord un type de machine"},'
|
choices = '{"":[{key:"",value:"Choisissez d\'abord un type de machine"},'
|
||||||
mtype_id = -1
|
mtype_id = -1
|
||||||
|
|
||||||
for ip in f_ipv4.queryset.annotate(mtype_id=F('ip_type__machinetype__id')).order_by('mtype_id', 'id') :
|
for ip in f_ipv4.queryset.annotate(mtype_id=F('ip_type__machinetype__id'))\
|
||||||
|
.order_by('mtype_id', 'id') :
|
||||||
if mtype_id != ip.mtype_id :
|
if mtype_id != ip.mtype_id :
|
||||||
mtype_id = ip.mtype_id
|
mtype_id = ip.mtype_id
|
||||||
used_mtype_id.append(mtype_id)
|
used_mtype_id.append(mtype_id)
|
||||||
|
@ -159,8 +236,8 @@ def generate_ipv4_bft_param( form, is_type_tt ):
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
def new_machine(request, userid):
|
def new_machine(request, userid):
|
||||||
""" Fonction de creation d'une machine. Cree l'objet machine, le sous objet interface et l'objet domain
|
""" Fonction de creation d'une machine. Cree l'objet machine,
|
||||||
à partir de model forms.
|
le sous objet interface et l'objet domain à partir de model forms.
|
||||||
Trop complexe, devrait être simplifié"""
|
Trop complexe, devrait être simplifié"""
|
||||||
try:
|
try:
|
||||||
user = User.objects.get(pk=userid)
|
user = User.objects.get(pk=userid)
|
||||||
|
@ -171,7 +248,9 @@ def new_machine(request, userid):
|
||||||
max_lambdauser_interfaces = options.max_lambdauser_interfaces
|
max_lambdauser_interfaces = options.max_lambdauser_interfaces
|
||||||
if not request.user.has_perms(('cableur',)):
|
if not request.user.has_perms(('cableur',)):
|
||||||
if user != request.user:
|
if user != request.user:
|
||||||
messages.error(request, "Vous ne pouvez pas ajouter une machine à un autre user que vous sans droit")
|
messages.error(
|
||||||
|
request,
|
||||||
|
"Vous ne pouvez pas ajouter une machine à un autre user que vous sans droit")
|
||||||
return redirect("/users/profil/" + str(request.user.id))
|
return redirect("/users/profil/" + str(request.user.id))
|
||||||
if user.user_interfaces().count() >= max_lambdauser_interfaces:
|
if user.user_interfaces().count() >= max_lambdauser_interfaces:
|
||||||
messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces)
|
messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces)
|
||||||
|
@ -1205,14 +1284,14 @@ def service_servers(request):
|
||||||
@permission_required('serveur')
|
@permission_required('serveur')
|
||||||
def ouverture_ports(request):
|
def ouverture_ports(request):
|
||||||
r = {'ipv4':{}, 'ipv6':{}}
|
r = {'ipv4':{}, 'ipv6':{}}
|
||||||
for o in OuverturePortList.objects.all().prefetch_related('ouvertureport_set').prefetch_related('interface_set'):
|
for o in OuverturePortList.objects.all().prefetch_related('ouvertureport_set').prefetch_related('interface_set', 'interface_set__ipv4'):
|
||||||
pl = {
|
pl = {
|
||||||
"tcp_in":set(map(str,o.tcp_ports_in())),
|
"tcp_in":set(map(str,o.ouvertureport_set.filter(protocole=OuverturePort.TCP, io=OuverturePort.IN))),
|
||||||
"tcp_out":set(map(str,o.tcp_ports_out())),
|
"tcp_out":set(map(str,o.ouvertureport_set.filter(protocole=OuverturePort.TCP, io=OuverturePort.OUT))),
|
||||||
"udp_in":set(map(str,o.udp_ports_in())),
|
"udp_in":set(map(str,o.ouvertureport_set.filter(protocole=OuverturePort.UDP, io=OuverturePort.IN))),
|
||||||
"udp_out":set(map(str,o.udp_ports_out())),
|
"udp_out":set(map(str,o.ouvertureport_set.filter(protocole=OuverturePort.UDP, io=OuverturePort.OUT))),
|
||||||
}
|
}
|
||||||
for i in o.interface_set.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():
|
for i in filter_active_interfaces(o.interface_set):
|
||||||
if i.may_have_port_open():
|
if i.may_have_port_open():
|
||||||
d = r['ipv4'].get(i.ipv4.ipv4, {})
|
d = r['ipv4'].get(i.ipv4.ipv4, {})
|
||||||
d["tcp_in"] = d.get("tcp_in",set()).union(pl["tcp_in"])
|
d["tcp_in"] = d.get("tcp_in",set()).union(pl["tcp_in"])
|
||||||
|
|
Loading…
Reference in a new issue