Commit 48d5ab10 authored by erdnaxe's avatar erdnaxe 🎇 Committed by Hugo LEVY-FALK

Autoformat code

parent 1e9a4b3d
......@@ -41,4 +41,3 @@ def can_view(user):
can = user.has_module_perms('machines')
return can, None if can else _("You don't have the right to view this"
" application.")
......@@ -29,7 +29,6 @@ from __future__ import unicode_literals
from django.contrib import admin
from reversion.admin import VersionAdmin
from .models import IpType, Machine, MachineType, Domain, IpList, Interface
from .models import (
Extension,
SOA,
......@@ -47,6 +46,7 @@ from .models import (
Ipv6List,
OuverturePortList,
)
from .models import IpType, Machine, MachineType, Domain, IpList, Interface
class MachineAdmin(VersionAdmin):
......@@ -98,6 +98,7 @@ class TxtAdmin(VersionAdmin):
""" Admin view of a TXT object """
pass
class DNameAdmin(VersionAdmin):
""" Admin view of a DName object """
pass
......@@ -147,12 +148,12 @@ class ServiceAdmin(VersionAdmin):
""" Admin view of a ServiceAdmin object """
list_display = ('service_type', 'min_time_regen', 'regular_time_regen')
class RoleAdmin(VersionAdmin):
""" Admin view of a RoleAdmin object """
pass
admin.site.register(Machine, MachineAdmin)
admin.site.register(MachineType, MachineTypeAdmin)
admin.site.register(IpType, IpTypeAdmin)
......
......@@ -35,13 +35,12 @@ Formulaires d'ajout, edition et suppressions de :
from __future__ import unicode_literals
from django.forms import ModelForm, Form
from django import forms
from django.forms import ModelForm, Form
from django.utils.translation import ugettext_lazy as _
from re2o.field_permissions import FieldPermissionFormMixin
from re2o.mixins import FormRevMixin
from .models import (
Domain,
Machine,
......@@ -68,6 +67,7 @@ from .models import (
class EditMachineForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
"""Formulaire d'édition d'une machine"""
class Meta:
model = Machine
fields = '__all__'
......@@ -80,12 +80,14 @@ class EditMachineForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
class NewMachineForm(EditMachineForm):
"""Creation d'une machine, ne renseigne que le nom"""
class Meta(EditMachineForm.Meta):
fields = ['name']
class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
"""Edition d'une interface. Edition complète"""
class Meta:
model = Interface
fields = ['machine', 'type', 'ipv4', 'mac_address', 'details']
......@@ -116,7 +118,7 @@ class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
interface=self.instance
)
if "machine" in self.fields:
self.fields['machine'].queryset = Machine.objects.all()\
self.fields['machine'].queryset = Machine.objects.all() \
.select_related('user')
can_use_all_machinetype, _reason = MachineType.can_use_all(user)
if not can_use_all_machinetype:
......@@ -128,12 +130,14 @@ class EditInterfaceForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
class AddInterfaceForm(EditInterfaceForm):
"""Ajout d'une interface à une machine. En fonction des droits,
affiche ou non l'ensemble des ip disponibles"""
class Meta(EditInterfaceForm.Meta):
fields = ['type', 'ipv4', 'mac_address', 'details']
class AliasForm(FormRevMixin, ModelForm):
"""Ajout d'un alias (et edition), CNAME, contenant nom et extension"""
class Meta:
model = Domain
fields = ['name', 'extension']
......@@ -151,6 +155,7 @@ class AliasForm(FormRevMixin, ModelForm):
class DomainForm(FormRevMixin, ModelForm):
"""Ajout et edition d'un enregistrement de nom, relié à interface"""
class Meta:
model = Domain
fields = ['name']
......@@ -183,6 +188,7 @@ class DelAliasForm(FormRevMixin, Form):
class MachineTypeForm(FormRevMixin, ModelForm):
"""Ajout et edition d'un machinetype, relié à un iptype"""
class Meta:
model = MachineType
fields = ['type', 'ip_type']
......@@ -214,6 +220,7 @@ class DelMachineTypeForm(FormRevMixin, Form):
class IpTypeForm(FormRevMixin, ModelForm):
"""Formulaire d'ajout d'un iptype. Pas d'edition de l'ip de start et de
stop après creation"""
class Meta:
model = IpType
fields = '__all__'
......@@ -227,6 +234,7 @@ class IpTypeForm(FormRevMixin, ModelForm):
class EditIpTypeForm(IpTypeForm):
"""Edition d'un iptype. Pas d'edition du rangev4 possible, car il faudrait
synchroniser les objets iplist"""
class Meta(IpTypeForm.Meta):
fields = ['extension', 'type', 'need_infra', 'domaine_ip_network', 'domaine_ip_netmask',
'prefix_v6', 'prefix_v6_length',
......@@ -253,6 +261,7 @@ class DelIpTypeForm(FormRevMixin, Form):
class ExtensionForm(FormRevMixin, ModelForm):
"""Formulaire d'ajout et edition d'une extension"""
class Meta:
model = Extension
fields = '__all__'
......@@ -285,6 +294,7 @@ class DelExtensionForm(FormRevMixin, Form):
class Ipv6ListForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
"""Gestion des ipv6 d'une machine"""
class Meta:
model = Ipv6List
fields = ['ipv6', 'slaac_ip']
......@@ -296,6 +306,7 @@ class Ipv6ListForm(FormRevMixin, FieldPermissionFormMixin, ModelForm):
class SOAForm(FormRevMixin, ModelForm):
"""Ajout et edition d'un SOA"""
class Meta:
model = SOA
fields = '__all__'
......@@ -324,6 +335,7 @@ class DelSOAForm(FormRevMixin, Form):
class MxForm(FormRevMixin, ModelForm):
"""Ajout et edition d'un MX"""
class Meta:
model = Mx
fields = ['zone', 'priority', 'name']
......@@ -357,6 +369,7 @@ class NsForm(FormRevMixin, ModelForm):
"""Ajout d'un NS pour une zone
On exclue les CNAME dans les objets domain (interdit par la rfc)
donc on prend uniquemet """
class Meta:
model = Ns
fields = ['zone', 'ns']
......@@ -388,6 +401,7 @@ class DelNsForm(FormRevMixin, Form):
class TxtForm(FormRevMixin, ModelForm):
"""Ajout d'un txt pour une zone"""
class Meta:
model = Txt
fields = '__all__'
......@@ -416,6 +430,7 @@ class DelTxtForm(FormRevMixin, Form):
class DNameForm(FormRevMixin, ModelForm):
"""Add a DNAME entry for a zone"""
class Meta:
model = DName
fields = '__all__'
......@@ -444,6 +459,7 @@ class DelDNameForm(FormRevMixin, Form):
class SrvForm(FormRevMixin, ModelForm):
"""Ajout d'un srv pour une zone"""
class Meta:
model = Srv
fields = '__all__'
......@@ -473,6 +489,7 @@ class DelSrvForm(FormRevMixin, Form):
class NasForm(FormRevMixin, ModelForm):
"""Ajout d'un type de nas (machine d'authentification,
swicths, bornes...)"""
class Meta:
model = Nas
fields = '__all__'
......@@ -501,6 +518,7 @@ class DelNasForm(FormRevMixin, Form):
class RoleForm(FormRevMixin, ModelForm):
"""Add and edit role."""
class Meta:
model = Role
fields = '__all__'
......@@ -509,9 +527,9 @@ class RoleForm(FormRevMixin, ModelForm):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(RoleForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['servers'].queryset = (Interface.objects.all()
.select_related(
'domain__extension'
))
.select_related(
'domain__extension'
))
class DelRoleForm(FormRevMixin, Form):
......@@ -533,6 +551,7 @@ class DelRoleForm(FormRevMixin, Form):
class ServiceForm(FormRevMixin, ModelForm):
"""Ajout et edition d'une classe de service : dns, dhcp, etc"""
class Meta:
model = Service
fields = '__all__'
......@@ -541,9 +560,9 @@ class ServiceForm(FormRevMixin, ModelForm):
prefix = kwargs.pop('prefix', self.Meta.model.__name__)
super(ServiceForm, self).__init__(*args, prefix=prefix, **kwargs)
self.fields['servers'].queryset = (Interface.objects.all()
.select_related(
'domain__extension'
))
.select_related(
'domain__extension'
))
def save(self, commit=True):
# TODO : None of the parents of ServiceForm use the commit
......@@ -574,6 +593,7 @@ class DelServiceForm(FormRevMixin, Form):
class VlanForm(FormRevMixin, ModelForm):
"""Ajout d'un vlan : id, nom"""
class Meta:
model = Vlan
fields = ['vlan_id', 'name', 'comment']
......@@ -614,6 +634,7 @@ class DelVlanForm(FormRevMixin, Form):
class EditOuverturePortConfigForm(FormRevMixin, ModelForm):
"""Edition de la liste des profils d'ouverture de ports
pour l'interface"""
class Meta:
model = Interface
fields = ['port_lists']
......@@ -630,6 +651,7 @@ class EditOuverturePortConfigForm(FormRevMixin, ModelForm):
class EditOuverturePortListForm(FormRevMixin, ModelForm):
"""Edition de la liste des ports et profils d'ouverture
des ports"""
class Meta:
model = OuverturePortList
fields = '__all__'
......@@ -642,9 +664,10 @@ class EditOuverturePortListForm(FormRevMixin, ModelForm):
**kwargs
)
class SshFpForm(FormRevMixin, ModelForm):
"""Edits a SSHFP record."""
class Meta:
model = SshFp
exclude = ('machine',)
......@@ -656,4 +679,3 @@ class SshFpForm(FormRevMixin, ModelForm):
prefix=prefix,
**kwargs
)
This diff is collapsed.
......@@ -26,8 +26,8 @@
Serializers for the Machines app
"""
from rest_framework import serializers
from machines.models import (
Interface,
IpType,
......@@ -148,13 +148,13 @@ class TypeSerializer(serializers.ModelSerializer):
get ForeignKey values. Infos about the general port policy is added """
extension = ExtensionNameField(read_only=True)
ouverture_ports_tcp_in = serializers\
ouverture_ports_tcp_in = serializers \
.SerializerMethodField('get_port_policy_input_tcp')
ouverture_ports_tcp_out = serializers\
ouverture_ports_tcp_out = serializers \
.SerializerMethodField('get_port_policy_output_tcp')
ouverture_ports_udp_in = serializers\
ouverture_ports_udp_in = serializers \
.SerializerMethodField('get_port_policy_input_udp')
ouverture_ports_udp_out = serializers\
ouverture_ports_udp_out = serializers \
.SerializerMethodField('get_port_policy_output_udp')
class Meta:
......@@ -400,7 +400,7 @@ class OuverturePortsSerializer(serializers.Serializer):
return {
i.ipv4.ipv4: {
"tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
"tcp_out": [j.tcp_ports_out()for j in i.port_lists.all()],
"tcp_out": [j.tcp_ports_out() for j in i.port_lists.all()],
"udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
"udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
}
......@@ -413,7 +413,7 @@ class OuverturePortsSerializer(serializers.Serializer):
return {
i.ipv6: {
"tcp_in": [j.tcp_ports_in() for j in i.port_lists.all()],
"tcp_out": [j.tcp_ports_out()for j in i.port_lists.all()],
"tcp_out": [j.tcp_ports_out() for j in i.port_lists.all()],
"udp_in": [j.udp_ports_in() for j in i.port_lists.all()],
"udp_out": [j.udp_ports_out() for j in i.port_lists.all()],
}
......
......@@ -27,6 +27,7 @@ The defined URLs for the Machines app
from __future__ import unicode_literals
from django.conf.urls import url
from . import views
urlpatterns = [
......
......@@ -32,26 +32,18 @@ The views for the Machines app
from __future__ import unicode_literals
from django.urls import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required
from django.db.models import ProtectedError, F
from django.forms import modelformset_factory
from django.views.decorators.csrf import csrf_exempt
from django.http import HttpResponse
from django.shortcuts import render, redirect
from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from users.models import User
from preferences.models import GeneralOption
from re2o.utils import (
all_active_assigned_interfaces,
filter_active_interfaces,
SortTable,
re2o_paginator,
)
from re2o.acl import (
can_create,
can_edit,
......@@ -60,21 +52,14 @@ from re2o.acl import (
can_view_all,
can_delete_set,
)
from re2o.views import form
from .serializers import (
FullInterfaceSerializer,
InterfaceSerializer,
TypeSerializer,
DomainSerializer,
TxtSerializer,
SrvSerializer,
MxSerializer,
ExtensionSerializer,
ServiceServersSerializer,
NsSerializer,
from re2o.utils import (
all_active_assigned_interfaces,
filter_active_interfaces,
SortTable,
re2o_paginator,
)
from re2o.views import form
from users.models import User
from .forms import (
NewMachineForm,
EditMachineForm,
......@@ -139,6 +124,18 @@ from .models import (
OuverturePort,
Ipv6List,
)
from .serializers import (
FullInterfaceSerializer,
InterfaceSerializer,
TypeSerializer,
DomainSerializer,
TxtSerializer,
SrvSerializer,
MxSerializer,
ExtensionSerializer,
ServiceServersSerializer,
NsSerializer,
)
def f_type_id(is_type_tt):
......@@ -153,12 +150,12 @@ def generate_ipv4_choices(form_obj):
"""
f_ipv4 = form_obj.fields['ipv4']
used_mtype_id = []
choices = '{"":[{key:"",value:"'+_("Select a machine type first.") + '"}'
choices = '{"":[{key:"",value:"' + _("Select a machine type first.") + '"}'
mtype_id = -1
for ip in (f_ipv4.queryset
.annotate(mtype_id=F('ip_type__machinetype__id'))
.order_by('mtype_id', 'id')):
.annotate(mtype_id=F('ip_type__machinetype__id'))
.order_by('mtype_id', 'id')):
if mtype_id != ip.mtype_id:
mtype_id = ip.mtype_id
used_mtype_id.append(mtype_id)
......@@ -172,7 +169,7 @@ def generate_ipv4_choices(form_obj):
)
for t in form_obj.fields['type'].queryset.exclude(id__in=used_mtype_id):
choices += '], "'+str(t.id)+'": ['
choices += '], "' + str(t.id) + '": ['
choices += '{key: "", value: "' + str(f_ipv4.empty_label) + '"},'
choices += ']}'
return choices
......@@ -889,6 +886,7 @@ def del_ns(request, instances):
request
)
@login_required
@can_create(DName)
def add_dname(request):
......@@ -935,9 +933,9 @@ def del_dname(request, instances):
messages.success(request, _("The DNAME record was deleted."))
except ProtectedError:
messages.error(
request,
_("Error: the DNAME record %s can't be deleted.")
% dname_del
request,
_("Error: the DNAME record %s can't be deleted.")
% dname_del
)
return redirect(reverse('machines:index-extension'))
return form(
......@@ -1252,16 +1250,16 @@ def del_service(request, instances):
request
)
@login_required
@can_edit(Service)
def regen_service(request,service, **_kwargs):
def regen_service(request, service, **_kwargs):
"""Ask for a regen of the service"""
regen(service)
return index_service(request)
@login_required
@can_create(Vlan)
def add_vlan(request):
......@@ -1388,10 +1386,10 @@ def index(request):
.prefetch_related('interface_set__domain__extension')
.prefetch_related('interface_set__ipv4__ip_type')
.prefetch_related(
'interface_set__type__ip_type__extension'
).prefetch_related(
'interface_set__domain__related_domain__extension'
).prefetch_related('interface_set__ipv6list'))
'interface_set__type__ip_type__extension'
).prefetch_related(
'interface_set__domain__related_domain__extension'
).prefetch_related('interface_set__ipv6list'))
machines_list = SortTable.sort(
machines_list,
request.GET.get('col'),
......@@ -1548,8 +1546,8 @@ def index_role(request):
""" View used to display the list of existing roles """
role_list = (Role.objects
.prefetch_related(
'servers__domain__extension'
).all())
'servers__domain__extension'
).all())
return render(
request,
'machines/index_role.html',
......@@ -1563,8 +1561,8 @@ def index_service(request):
""" View used to display the list of existing services """
service_list = (Service.objects
.prefetch_related(
'service_link_set__server__domain__extension'
).all())
'service_link_set__server__domain__extension'
).all())
servers_list = (Service_link.objects
.select_related('server__domain__extension')
.select_related('service')
......@@ -1740,10 +1738,10 @@ def alias(_request):
alias = (Domain.objects
.filter(interface_parent=None)
.filter(
cname__in=Domain.objects.filter(
interface_parent__in=Interface.objects.exclude(ipv4=None)
)
).select_related('extension')
cname__in=Domain.objects.filter(
interface_parent__in=Interface.objects.exclude(ipv4=None)
)
).select_related('extension')
.select_related('cname__extension'))
seria = DomainSerializer(alias, many=True)
return JSONResponse(seria.data)
......@@ -1801,10 +1799,10 @@ def ns(_request):
""" API view to list the NS records """
ns = (Ns.objects
.exclude(
ns__in=Domain.objects.filter(
interface_parent__in=Interface.objects.filter(ipv4=None)
)
).select_related('zone')
ns__in=Domain.objects.filter(
interface_parent__in=Interface.objects.filter(ipv4=None)
)
).select_related('zone')
.select_related('ns__extension'))
seria = NsSerializer(ns, many=True)
return JSONResponse(seria.data)
......@@ -1859,9 +1857,9 @@ def ouverture_ports(_request):
""" API view to list the port policies for each IP """
r = {'ipv4': {}, 'ipv6': {}}
for o in (OuverturePortList.objects
.all()
.prefetch_related('ouvertureport_set')
.prefetch_related('interface_set', 'interface_set__ipv4')):
.all()
.prefetch_related('ouvertureport_set')
.prefetch_related('interface_set', 'interface_set__ipv4')):
pl = {
"tcp_in": set(map(
str,
......@@ -1926,17 +1924,16 @@ def regen_achieved(request):
""" API view to list the regen status for each (Service link, Server)
couple """
obj = (Service_link.objects
.filter(
service__in=Service.objects.filter(
service_type=request.POST['service']
),
server__in=Interface.objects.filter(
domain__in=Domain.objects.filter(
name=request.POST['server']
)
)
))
.filter(
service__in=Service.objects.filter(
service_type=request.POST['service']
),
server__in=Interface.objects.filter(
domain__in=Domain.objects.filter(
name=request.POST['server']
)
)
))
if obj:
obj.first().done_regen()
return HttpResponse("Ok")
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