From 334433f25d49e433c393090e5af20db97bbd887c Mon Sep 17 00:00:00 2001 From: root Date: Wed, 14 Dec 2016 03:35:34 +0100 Subject: [PATCH 01/65] Revert "Revert "Fix static css loading issue"" This reverts commit 93ff47d0e90141dd00cd8522a32a25da3ba15e81. --- templates/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/base.html b/templates/base.html index d087b0f9..298aad2e 100644 --- a/templates/base.html +++ b/templates/base.html @@ -10,7 +10,7 @@ {% bootstrap_css %} {% bootstrap_javascript %} - + {{ site_name }} : {% block title %}Accueil{% endblock %} From 6a80d55389a613a07481d66014823746e66c72f5 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 14 Dec 2016 03:48:50 +0100 Subject: [PATCH 02/65] Change le comportement de /login/ dans django 1.10, fix redirect url --- re2o/settings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/re2o/settings.py b/re2o/settings.py index 7cf10b42..d8842df8 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -28,7 +28,7 @@ PASSWORD_HASHERS = ( ) AUTH_USER_MODEL = 'users.User' -LOGIN_URL = '/login' +LOGIN_URL = '/login/' LOGIN_REDIRECT_URL = '/' From 5d0cf0d8d9da970f1f0ef689c3084db44e1054f3 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 14 Dec 2016 03:52:59 +0100 Subject: [PATCH 03/65] Oubli d'un render_to_response --- search/views.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/search/views.py b/search/views.py index 75b899ec..fc281766 100644 --- a/search/views.py +++ b/search/views.py @@ -2,7 +2,7 @@ # Augustin lemesle, Gabriel Détraz, Goulven Kermarec # Gplv2 from django.shortcuts import render -from django.shortcuts import render_to_response, get_object_or_404 +from django.shortcuts import get_object_or_404 from django.template.context_processors import csrf from django.template import Context, RequestContext, loader from django.contrib.auth.decorators import login_required @@ -19,7 +19,7 @@ from re2o.settings import SEARCH_RESULT def form(ctx, template, request): c = ctx c.update(csrf(request)) - return render_to_response(template, c, context_instance=RequestContext(request)) + return render(request, template, c) def search_result(search, type, request): date_deb = None From 62aaa31e63a5a0d054b902209299746912336291 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 14 Dec 2016 18:09:24 +0100 Subject: [PATCH 04/65] Supprime les machines orphelines --- machines/models.py | 4 ++- machines/views.py | 3 ++ .../migrations/0022_auto_20161211_1622.py | 19 ++++++++++++ users/migrations/0042_auto_20161126_2028.py | 29 +++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 topologie/migrations/0022_auto_20161211_1622.py create mode 100644 users/migrations/0042_auto_20161126_2028.py diff --git a/machines/models.py b/machines/models.py index 0bd879d7..30494cd1 100644 --- a/machines/models.py +++ b/machines/models.py @@ -137,5 +137,7 @@ def interface_post_save(sender, **kwargs): @receiver(post_delete, sender=Interface) def interface_post_delete(sender, **kwargs): - user = kwargs['instance'].machine.user + interface = kwargs['instance'] + user = interface.machine.user user.ldap_sync(base=False, access_refresh=False, mac_refresh=True) + diff --git a/machines/views.py b/machines/views.py index 724168a7..7b07872d 100644 --- a/machines/views.py +++ b/machines/views.py @@ -217,8 +217,11 @@ def del_interface(request, interfaceid): messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) if request.method == "POST": + machine = interface.machine with transaction.atomic(), reversion.create_revision(): interface.delete() + if not machine.interface_set.all(): + machine.delete() reversion.set_user(request.user) messages.success(request, "L'interface a été détruite") return redirect("/users/profil/" + str(request.user.id)) diff --git a/topologie/migrations/0022_auto_20161211_1622.py b/topologie/migrations/0022_auto_20161211_1622.py new file mode 100644 index 00000000..dfcd1524 --- /dev/null +++ b/topologie/migrations/0022_auto_20161211_1622.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('topologie', '0021_port_radius'), + ] + + operations = [ + migrations.AlterField( + model_name='port', + name='radius', + field=models.CharField(max_length=32, default='NO', choices=[('NO', 'NO'), ('STRICT', 'STRICT'), ('BLOQ', 'BLOQ'), ('COMMON', 'COMMON'), ('7', '7'), ('8', '8'), ('42', '42'), ('69', '69')]), + ), + ] diff --git a/users/migrations/0042_auto_20161126_2028.py b/users/migrations/0042_auto_20161126_2028.py new file mode 100644 index 00000000..3dd87426 --- /dev/null +++ b/users/migrations/0042_auto_20161126_2028.py @@ -0,0 +1,29 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0041_listright_details'), + ] + + operations = [ + migrations.AlterField( + model_name='ldapserviceuser', + name='dn', + field=models.CharField(serialize=False, primary_key=True, max_length=200), + ), + migrations.AlterField( + model_name='ldapuser', + name='dn', + field=models.CharField(serialize=False, primary_key=True, max_length=200), + ), + migrations.AlterField( + model_name='ldapusergroup', + name='dn', + field=models.CharField(serialize=False, primary_key=True, max_length=200), + ), + ] From 2c63c3f9f608ef525dd547c5fe6cd19e47e3600e Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Wed, 14 Dec 2016 19:07:20 +0100 Subject: [PATCH 05/65] Oublie d'un __in --- machines/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machines/models.py b/machines/models.py index 30494cd1..26a3166f 100644 --- a/machines/models.py +++ b/machines/models.py @@ -113,7 +113,7 @@ class Alias(models.Model): def clean(self, *args, **kwargs): if hasattr(self, 'alias') and hasattr(self, 'extension'): - if Interface.objects.filter(dns=self.alias).filter(ipv4=IpList.objects.filter(ip_type=IpType.objects.filter(extension=self.extension))): + if Interface.objects.filter(dns=self.alias).filter(ipv4__in=IpList.objects.filter(ip_type__in=IpType.objects.filter(extension=self.extension))): raise ValidationError("Impossible d'ajouter l'alias, déjà utilisé par une machine") def __str__(self): From e09ab28bec027c0d6a5aa3325e916110e2ed5615 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sun, 18 Dec 2016 09:52:20 +0000 Subject: [PATCH 06/65] =?UTF-8?q?No=20crash=20=C3=A0=20la=20migration=20in?= =?UTF-8?q?itiale?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- users/models.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/users/models.py b/users/models.py index 1cd19787..01f7a34c 100644 --- a/users/models.py +++ b/users/models.py @@ -44,7 +44,10 @@ def linux_user_validator(login): def get_fresh_user_uid(): uids = list(range(int(min(UID_RANGES['users'])),int(max(UID_RANGES['users'])))) - used_uids = [ user.uid_number for user in User.objects.all()] + try: + used_uids = [ user.uid_number for user in User.objects.all()] + except: + used_uids = [] free_uids = [ id for id in uids if id not in used_uids] return min(free_uids) From 0cfed8c90a11a9c31a190764e4163af0f1e7f5ae Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sun, 18 Dec 2016 22:25:21 +0000 Subject: [PATCH 07/65] =?UTF-8?q?Code=20dupliqu=C3=A9,=20simplifie=20+=20d?= =?UTF-8?q?jango=201.10?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- re2o/context_processors.py | 2 +- users/models.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/re2o/context_processors.py b/re2o/context_processors.py index 87dc3a9c..73a1721f 100644 --- a/re2o/context_processors.py +++ b/re2o/context_processors.py @@ -4,7 +4,7 @@ from .settings import SITE_NAME def context_user(request): user = request.user if user.is_authenticated(): - interfaces = Interface.objects.filter(machine=Machine.objects.filter(user=user, active=True)) + interfaces = user.user_interfaces() else: interfaces = None is_cableur = user.has_perms(('cableur',)) diff --git a/users/models.py b/users/models.py index 01f7a34c..6bff8adf 100644 --- a/users/models.py +++ b/users/models.py @@ -231,7 +231,7 @@ class User(AbstractBaseUser): and not self.is_ban() and (self.is_adherent() or self.is_whitelisted()) def user_interfaces(self): - return Interface.objects.filter(machine__in=Machine.objects.filter(user=self)) + return Interface.objects.filter(machine__in=Machine.objects.filter(user=self, active=True)) def has_module_perms(self, app_label): # Simplest version again From e6b6467ae2520a0f304995ed1e89d07be5836e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Mon, 19 Dec 2016 19:55:35 +0000 Subject: [PATCH 08/65] Fix django 1.10 code incorrect --- machines/serializers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/machines/serializers.py b/machines/serializers.py index 64d51d94..575faacd 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -23,7 +23,7 @@ class InterfaceSerializer(serializers.ModelSerializer): class ExtensionNameField(serializers.RelatedField): def to_representation(self, value): - return value.alias + return value.name class TypeSerializer(serializers.ModelSerializer): extension = ExtensionNameField(read_only=True) From 6b846ad0d4f7b7d0dd1feba3ec515735df2abfe1 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Mon, 19 Dec 2016 22:59:54 +0000 Subject: [PATCH 09/65] Serializer django 1.10 --- machines/serializers.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/machines/serializers.py b/machines/serializers.py index 575faacd..b19294c4 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -16,11 +16,15 @@ class IpListSerializer(serializers.ModelSerializer): class InterfaceSerializer(serializers.ModelSerializer): ipv4 = IpListSerializer(read_only=True) - + mac_address = serializers.SerializerMethodField('get_macaddress') + class Meta: model = Interface fields = ('ipv4', 'mac_address', 'dns') + def get_macaddress(self, obj): + return str(obj.mac_address) + class ExtensionNameField(serializers.RelatedField): def to_representation(self, value): return value.name From e2661a668537a1aff743dc0f00defc30f56b98f4 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 24 Dec 2016 18:07:09 +0100 Subject: [PATCH 10/65] Creation de domain, transfert --- machines/admin.py | 8 ++--- .../migrations/0035_auto_20161224_1201.py | 15 +++++++++ .../migrations/0036_auto_20161224_1204.py | 28 ++++++++++++++++ machines/migrations/0037_domain_cname.py | 19 +++++++++++ .../migrations/0038_auto_20161224_1721.py | 19 +++++++++++ .../migrations/0039_auto_20161224_1732.py | 19 +++++++++++ machines/models.py | 32 ++++++++----------- 7 files changed, 118 insertions(+), 22 deletions(-) create mode 100644 machines/migrations/0035_auto_20161224_1201.py create mode 100644 machines/migrations/0036_auto_20161224_1204.py create mode 100644 machines/migrations/0037_domain_cname.py create mode 100644 machines/migrations/0038_auto_20161224_1721.py create mode 100644 machines/migrations/0039_auto_20161224_1732.py diff --git a/machines/admin.py b/machines/admin.py index aa7d6bc5..23fd068e 100644 --- a/machines/admin.py +++ b/machines/admin.py @@ -1,7 +1,7 @@ from django.contrib import admin from reversion.admin import VersionAdmin -from .models import IpType, Machine, MachineType, Alias, IpList, Interface, Extension, Mx, Ns +from .models import IpType, Machine, MachineType, Domain, IpList, Interface, Extension, Mx, Ns class MachineAdmin(VersionAdmin): list_display = ('user','name','active') @@ -28,8 +28,8 @@ class IpListAdmin(VersionAdmin): class InterfaceAdmin(VersionAdmin): list_display = ('machine','type','dns','mac_address','ipv4','details') -class AliasAdmin(VersionAdmin): - list_display = ('interface_parent', 'alias', 'extension') +class DomainAdmin(VersionAdmin): + list_display = ('interface_parent', 'name', 'extension', 'cname') admin.site.register(Machine, MachineAdmin) admin.site.register(MachineType, MachineTypeAdmin) @@ -39,4 +39,4 @@ admin.site.register(Mx, MxAdmin) admin.site.register(Ns, NsAdmin) admin.site.register(IpList, IpListAdmin) admin.site.register(Interface, InterfaceAdmin) -admin.site.register(Alias, AliasAdmin) +admin.site.register(Domain, DomainAdmin) diff --git a/machines/migrations/0035_auto_20161224_1201.py b/machines/migrations/0035_auto_20161224_1201.py new file mode 100644 index 00000000..bda8e9b9 --- /dev/null +++ b/machines/migrations/0035_auto_20161224_1201.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0034_iplist_need_infra'), + ] + + operations = [ + migrations.RenameModel('Alias', 'Domain') + ] diff --git a/machines/migrations/0036_auto_20161224_1204.py b/machines/migrations/0036_auto_20161224_1204.py new file mode 100644 index 00000000..3eb97a6c --- /dev/null +++ b/machines/migrations/0036_auto_20161224_1204.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0035_auto_20161224_1201'), + ] + + operations = [ + migrations.RenameField( + model_name='domain', + old_name='alias', + new_name='name', + ), + migrations.AlterField( + model_name='domain', + name='interface_parent', + field=models.ForeignKey(to='machines.Interface', null=True, blank=True), + ), + migrations.AlterUniqueTogether( + name='domain', + unique_together=set([('name', 'extension')]), + ), + ] diff --git a/machines/migrations/0037_domain_cname.py b/machines/migrations/0037_domain_cname.py new file mode 100644 index 00000000..ee901817 --- /dev/null +++ b/machines/migrations/0037_domain_cname.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0036_auto_20161224_1204'), + ] + + operations = [ + migrations.AddField( + model_name='domain', + name='cname', + field=models.OneToOneField(related_name='related_domain', null=True, to='machines.Domain', blank=True), + ), + ] diff --git a/machines/migrations/0038_auto_20161224_1721.py b/machines/migrations/0038_auto_20161224_1721.py new file mode 100644 index 00000000..36f00f71 --- /dev/null +++ b/machines/migrations/0038_auto_20161224_1721.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0037_domain_cname'), + ] + + operations = [ + migrations.AlterField( + model_name='domain', + name='cname', + field=models.ForeignKey(null=True, to='machines.Domain', related_name='related_domain', blank=True), + ), + ] diff --git a/machines/migrations/0039_auto_20161224_1732.py b/machines/migrations/0039_auto_20161224_1732.py new file mode 100644 index 00000000..48f8a0d6 --- /dev/null +++ b/machines/migrations/0039_auto_20161224_1732.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0038_auto_20161224_1721'), + ] + + operations = [ + migrations.AlterField( + model_name='domain', + name='interface_parent', + field=models.OneToOneField(blank=True, null=True, to='machines.Interface'), + ), + ] diff --git a/machines/models.py b/machines/models.py index 26a3166f..791b4575 100644 --- a/machines/models.py +++ b/machines/models.py @@ -55,7 +55,7 @@ class Mx(models.Model): zone = models.ForeignKey('Extension', on_delete=models.PROTECT) priority = models.IntegerField(unique=True) - name = models.OneToOneField('Alias', on_delete=models.PROTECT) + name = models.OneToOneField('Domain', on_delete=models.PROTECT) def __str__(self): return str(self.zone) + ' ' + str(self.priority) + ' ' + str(self.name) @@ -91,33 +91,29 @@ class Interface(models.Model): def clean(self, *args, **kwargs): self.mac_address = str(EUI(self.mac_address)) or None - if self.ipv4: - alias = Alias.objects.filter(alias=self.dns).filter(extension=self.ipv4.ip_type.extension) - else: - alias = Alias.objects.filter(alias=self.dns) - if alias: - raise ValidationError("Impossible, le dns est déjà utilisé par un alias (%s)" % alias[0]) def __str__(self): - return self.dns + return self.domain_set.all().first() -class Alias(models.Model): - PRETTY_NAME = "Alias dns" +class Domain(models.Model): + PRETTY_NAME = "Domaine dns" - interface_parent = models.ForeignKey('Interface', on_delete=models.CASCADE) - alias = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255) + interface_parent = models.OneToOneField('Interface', on_delete=models.CASCADE, blank=True, null=True) + name = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255) extension = models.ForeignKey('Extension', on_delete=models.PROTECT) + cname = models.ForeignKey('self', null=True, blank=True, related_name='related_domain') class Meta: - unique_together = ("alias", "extension") + unique_together = ("name", "extension") - def clean(self, *args, **kwargs): - if hasattr(self, 'alias') and hasattr(self, 'extension'): - if Interface.objects.filter(dns=self.alias).filter(ipv4__in=IpList.objects.filter(ip_type__in=IpType.objects.filter(extension=self.extension))): - raise ValidationError("Impossible d'ajouter l'alias, déjà utilisé par une machine") + def clean(self): + if self.interface_parent and self.cname: + raise ValidationError("On ne peut créer à la fois A et CNAME") + if self.related==self: + raise ValidationError("On ne peut créer un cname sur lui même") def __str__(self): - return str(self.alias) + str(self.extension) + return str(self.name) + str(self.extension) class IpList(models.Model): PRETTY_NAME = "Addresses ipv4" From 861bae43b940ec0222a8dfc331939c18634b6b23 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 24 Dec 2016 20:04:53 +0100 Subject: [PATCH 11/65] Phase 2 modification domain --- logs/views.py | 4 +- machines/admin.py | 2 +- machines/forms.py | 21 +++--- .../migrations/0040_remove_interface_dns.py | 18 +++++ machines/models.py | 9 ++- machines/serializers.py | 20 ++++-- machines/templates/machines/aff_machines.html | 8 +-- machines/templates/machines/machine.html | 3 + machines/views.py | 66 ++++++++++++------- search/views.py | 2 +- 10 files changed, 102 insertions(+), 51 deletions(-) create mode 100644 machines/migrations/0040_remove_interface_dns.py diff --git a/logs/views.py b/logs/views.py index 116e8350..a1dfb361 100644 --- a/logs/views.py +++ b/logs/views.py @@ -19,7 +19,7 @@ from reversion.models import Version from users.models import User, ServiceUser, Right, School, ListRight, ListShell, Ban, Whitelist from cotisations.models import Facture, Vente, Article, Banque, Paiement, Cotisation -from machines.models import Machine, MachineType, IpType, Extension, Interface, Alias, IpList +from machines.models import Machine, MachineType, IpType, Extension, Interface, Domain, IpList from topologie.models import Switch, Port, Room from re2o.settings import PAGINATION_NUMBER, PAGINATION_LARGE_NUMBER @@ -99,7 +99,7 @@ def stats_models(request): 'typeip' : [IpType.PRETTY_NAME, IpType.objects.count()], 'extension' : [Extension.PRETTY_NAME, Extension.objects.count()], 'interface' : [Interface.PRETTY_NAME, Interface.objects.count()], - 'alias' : [Alias.PRETTY_NAME, Alias.objects.count()], + 'alias' : [Domain.PRETTY_NAME, Domain.objects.exclude(cname=None).count()], 'iplist' : [IpList.PRETTY_NAME, IpList.objects.count()], }, 'Topologie' : { diff --git a/machines/admin.py b/machines/admin.py index 23fd068e..1eddaa38 100644 --- a/machines/admin.py +++ b/machines/admin.py @@ -26,7 +26,7 @@ class IpListAdmin(VersionAdmin): list_display = ('ipv4','ip_type') class InterfaceAdmin(VersionAdmin): - list_display = ('machine','type','dns','mac_address','ipv4','details') + list_display = ('machine','type','mac_address','ipv4','details') class DomainAdmin(VersionAdmin): list_display = ('interface_parent', 'name', 'extension', 'cname') diff --git a/machines/forms.py b/machines/forms.py index 140eacfd..4b65c609 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -1,6 +1,6 @@ from django.forms import ModelForm, Form, ValidationError from django import forms -from .models import Alias, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, IpType +from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, IpType from django.db.models import Q class EditMachineForm(ModelForm): @@ -27,14 +27,13 @@ class EditInterfaceForm(ModelForm): def __init__(self, *args, **kwargs): super(EditInterfaceForm, self).__init__(*args, **kwargs) - self.fields['dns'].label = 'Nom dns de la machine' self.fields['mac_address'].label = 'Adresse mac' self.fields['type'].label = 'Type de machine' self.fields['type'].empty_label = "Séléctionner un type de machine" class AddInterfaceForm(EditInterfaceForm): class Meta(EditInterfaceForm.Meta): - fields = ['ipv4','mac_address','dns','type','details'] + fields = ['ipv4','mac_address','type','details'] def __init__(self, *args, **kwargs): infra = kwargs.pop('infra') @@ -48,11 +47,11 @@ class AddInterfaceForm(EditInterfaceForm): class NewInterfaceForm(EditInterfaceForm): class Meta(EditInterfaceForm.Meta): - fields = ['mac_address','dns','type','details'] + fields = ['mac_address','type','details'] class BaseEditInterfaceForm(EditInterfaceForm): class Meta(EditInterfaceForm.Meta): - fields = ['ipv4','mac_address','dns','type','details'] + fields = ['ipv4','mac_address','type','details'] def __init__(self, *args, **kwargs): infra = kwargs.pop('infra') @@ -66,8 +65,8 @@ class BaseEditInterfaceForm(EditInterfaceForm): class AliasForm(ModelForm): class Meta: - model = Alias - fields = ['alias','extension'] + model = Domain + fields = ['name','extension'] def __init__(self, *args, **kwargs): infra = kwargs.pop('infra') @@ -76,16 +75,16 @@ class AliasForm(ModelForm): self.fields['extension'].queryset = Extension.objects.filter(need_infra=False) class DelAliasForm(ModelForm): - alias = forms.ModelMultipleChoiceField(queryset=Alias.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple) + alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple) def __init__(self, *args, **kwargs): interface = kwargs.pop('interface') super(DelAliasForm, self).__init__(*args, **kwargs) - self.fields['alias'].queryset = Alias.objects.filter(interface_parent=interface) + self.fields['alias'].queryset = Domain.objects.filter(cname__in=Domain.objects.filter(interface_parent=interface)) class Meta: - exclude = ['interface_parent', 'alias', 'extension'] - model = Alias + exclude = ['interface_parent', 'name', 'extension', 'cname'] + model = Domain class MachineTypeForm(ModelForm): class Meta: diff --git a/machines/migrations/0040_remove_interface_dns.py b/machines/migrations/0040_remove_interface_dns.py new file mode 100644 index 00000000..46e926fb --- /dev/null +++ b/machines/migrations/0040_remove_interface_dns.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0039_auto_20161224_1732'), + ] + + operations = [ + migrations.RemoveField( + model_name='interface', + name='dns', + ), + ] diff --git a/machines/models.py b/machines/models.py index 791b4575..315627f7 100644 --- a/machines/models.py +++ b/machines/models.py @@ -78,7 +78,6 @@ class Interface(models.Model): machine = models.ForeignKey('Machine', on_delete=models.CASCADE) type = models.ForeignKey('MachineType', on_delete=models.PROTECT) details = models.CharField(max_length=255, blank=True) - dns = models.CharField(help_text="Obligatoire et unique, ne doit pas comporter de points", max_length=255, unique=True) def is_active(self): """ Renvoie si une interface doit avoir accès ou non """ @@ -93,7 +92,11 @@ class Interface(models.Model): self.mac_address = str(EUI(self.mac_address)) or None def __str__(self): - return self.domain_set.all().first() + try: + domain = self.domain + except: + domain = None + return str(domain) class Domain(models.Model): PRETTY_NAME = "Domaine dns" @@ -109,7 +112,7 @@ class Domain(models.Model): def clean(self): if self.interface_parent and self.cname: raise ValidationError("On ne peut créer à la fois A et CNAME") - if self.related==self: + if self.cname==self: raise ValidationError("On ne peut créer un cname sur lui même") def __str__(self): diff --git a/machines/serializers.py b/machines/serializers.py index b19294c4..df6edd47 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -1,7 +1,7 @@ #Augustin Lemesle from rest_framework import serializers -from machines.models import Interface, IpType, Extension, IpList, MachineType, Alias, Mx, Ns +from machines.models import Interface, IpType, Extension, IpList, MachineType, Domain, Mx, Ns class IpTypeField(serializers.RelatedField): def to_representation(self, value): @@ -17,10 +17,14 @@ class IpListSerializer(serializers.ModelSerializer): class InterfaceSerializer(serializers.ModelSerializer): ipv4 = IpListSerializer(read_only=True) mac_address = serializers.SerializerMethodField('get_macaddress') + dns = serializers.SerializerMethodField('get_dns') class Meta: model = Interface - fields = ('ipv4', 'mac_address', 'dns') + fields = ('ipv4', 'mac_address') + + def get_dns(self, obj): + return obj.domain_set.all().first() def get_macaddress(self, obj): return str(obj.mac_address) @@ -74,16 +78,20 @@ class NsSerializer(serializers.ModelSerializer): def get_interface_name(self, obj): return obj.interface.dns + obj.interface.ipv4.ip_type.extension.name -class AliasSerializer(serializers.ModelSerializer): +class DomainSerializer(serializers.ModelSerializer): interface_parent = serializers.SerializerMethodField('get_interface_name') extension = serializers.SerializerMethodField('get_zone_name') + cname = serializers.SerializerMethodField('get_cname') class Meta: - model = Alias - fields = ('interface_parent', 'alias', 'extension') + model = Domain + fields = ('interface_parent', 'name', 'extension', 'cname') def get_zone_name(self, obj): return obj.extension.name + def get_cname(self, obj): + return obj.cname.name + obj.cname.extension.name + def get_interface_name(self, obj): - return obj.interface_parent.dns + obj.interface_parent.ipv4.ip_type.extension.name + return obj.name + obj.extension.name diff --git a/machines/templates/machines/aff_machines.html b/machines/templates/machines/aff_machines.html index e1c11fe3..b238acfa 100644 --- a/machines/templates/machines/aff_machines.html +++ b/machines/templates/machines/aff_machines.html @@ -28,13 +28,13 @@ {% endif %} - {% if interface.alias_set.all %} + {% if interface.domain.related_domain.all %} {% else %} - {{ interface.dns }} + {{ interface.domain }} {% endif %} {{ interface.type }} diff --git a/machines/templates/machines/machine.html b/machines/templates/machines/machine.html index a8d287f6..f1ae661d 100644 --- a/machines/templates/machines/machine.html +++ b/machines/templates/machines/machine.html @@ -19,6 +19,9 @@ {% if interfaceform %} {% bootstrap_form interfaceform %} {% endif %} + {% if domainform %} + {% bootstrap_form domainform %} + {% endif %} {% bootstrap_button "Créer ou modifier" button_type="submit" icon="star" %}
diff --git a/machines/views.py b/machines/views.py index 7b07872d..69abf6dc 100644 --- a/machines/views.py +++ b/machines/views.py @@ -16,21 +16,21 @@ from django.contrib.auth import authenticate, login from django.views.decorators.csrf import csrf_exempt from rest_framework.renderers import JSONRenderer -from machines.serializers import InterfaceSerializer, TypeSerializer, AliasSerializer, MxSerializer, ExtensionSerializer, NsSerializer +from machines.serializers import InterfaceSerializer, TypeSerializer, DomainSerializer, MxSerializer, ExtensionSerializer, NsSerializer from reversion import revisions as reversion from reversion.models import Version import re from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm from .forms import IpTypeForm, DelIpTypeForm, AliasForm, DelAliasForm, NsForm, DelNsForm, MxForm, DelMxForm -from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Alias +from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Domain from users.models import User from re2o.settings import PAGINATION_NUMBER, PAGINATION_LARGE_NUMBER, MAX_INTERFACES, MAX_ALIAS -def full_domain_validator(request, interface): +def full_domain_validator(request, domain): """ Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """ HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(? 63: messages.error(request, "Le nom de domaine %s est trop long (maximum de 63 caractères)." % dns) @@ -99,11 +99,13 @@ def new_machine(request, userid): return redirect("/users/profil/" + str(request.user.id)) machine = NewMachineForm(request.POST or None) interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) + domain = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',))) if machine.is_valid() and interface.is_valid(): new_machine = machine.save(commit=False) new_machine.user = user new_interface = interface.save(commit=False) - if full_domain_validator(request, new_interface): + new_domain = domain.save(commit=False) + if full_domain_validator(request, new_domain): with transaction.atomic(), reversion.create_revision(): new_machine.save() reversion.set_user(request.user) @@ -117,9 +119,14 @@ def new_machine(request, userid): new_interface.save() reversion.set_user(request.user) reversion.set_comment("Création") + new_domain.interface_parent = new_interface + with transaction.atomic(), reversion.create_revision(): + new_domain.save() + reversion.set_user(request.user) + reversion.set_comment("Création") messages.success(request, "La machine a été crée") return redirect("/users/profil/" + userid) - return form({'machineform': machine, 'interfaceform': interface}, 'machines/machine.html', request) + return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'machines/machine.html', request) @login_required def edit_interface(request, interfaceid): @@ -137,10 +144,12 @@ def edit_interface(request, interfaceid): else: machine_form = EditMachineForm(request.POST or None, instance=interface.machine) interface_form = EditInterfaceForm(request.POST or None, instance=interface) - if machine_form.is_valid() and interface_form.is_valid(): + domain_form = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)), instance=interface.domain) + if machine_form.is_valid() and interface_form.is_valid() and domain_form.is_valid(): new_interface = interface_form.save(commit=False) new_machine = machine_form.save(commit=False) - if full_domain_validator(request, new_interface): + new_domain = domain_form.save(commit=False) + if full_domain_validator(request, new_domain): with transaction.atomic(), reversion.create_revision(): new_machine.save() reversion.set_user(request.user) @@ -151,9 +160,13 @@ def edit_interface(request, interfaceid): new_interface.save() reversion.set_user(request.user) reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in interface_form.changed_data)) + with transaction.atomic(), reversion.create_revision(): + new_domain.save() + reversion.set_user(request.user) + reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in domain_form.changed_data)) messages.success(request, "La machine a été modifiée") return redirect("/users/profil/" + str(interface.machine.user.id)) - return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request) + return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request) @login_required def del_machine(request, machineid): @@ -189,10 +202,12 @@ def new_interface(request, machineid): messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % MAX_INTERFACES) return redirect("/users/profil/" + str(request.user.id)) interface_form = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) + domain_form = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',))) if interface_form.is_valid(): new_interface = interface_form.save(commit=False) new_interface.machine = machine - if full_domain_validator(request, new_interface): + new_domain = domain_form.save(commit=False) + if full_domain_validator(request, new_domain): if free_ip(new_interface.type.ip_type) and not new_interface.ipv4: new_interface = assign_ipv4(new_interface) elif not new_interface.ipv4: @@ -201,9 +216,14 @@ def new_interface(request, machineid): new_interface.save() reversion.set_user(request.user) reversion.set_comment("Création") + new_domain.interface_parent = new_interface + with transaction.atomic(), reversion.create_revision(): + new_domain.save() + reversion.set_user(request.user) + reversion.set_comment("Création") messages.success(request, "L'interface a été ajoutée") return redirect("/users/profil/" + str(machine.user.id)) - return form({'interfaceform': interface_form}, 'machines/machine.html', request) + return form({'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request) @login_required def del_interface(request, interfaceid): @@ -478,13 +498,13 @@ def add_alias(request, interfaceid): if interface.machine.user != request.user: messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) - if Alias.objects.filter(interface_parent__in=interface.machine.user.user_interfaces()).count() >= MAX_ALIAS: + if Domain.objects.filter(cname__in=Domain.objects.filter(interface_parent__in=interface.machine.user.user_interfaces())).count() >= MAX_ALIAS: messages.error(request, "Vous avez atteint le maximum d'alias autorisées que vous pouvez créer vous même (%s) " % MAX_ALIAS) return redirect("/users/profil/" + str(request.user.id)) alias = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',))) if alias.is_valid(): alias = alias.save(commit=False) - alias.interface_parent = interface + alias.cname = interface.domain with transaction.atomic(), reversion.create_revision(): alias.save() reversion.set_user(request.user) @@ -496,11 +516,11 @@ def add_alias(request, interfaceid): @login_required def edit_alias(request, aliasid): try: - alias_instance = Alias.objects.get(pk=aliasid) - except Alias.DoesNotExist: + alias_instance = Domain.objects.get(pk=aliasid) + except Domain.DoesNotExist: messages.error(request, u"Entrée inexistante" ) return redirect("/machines/index_extension/") - if not request.user.has_perms(('cableur',)) and alias_instance.interface_parent.machine.user != request.user: + if not request.user.has_perms(('cableur',)) and alias_instance.cname.interface_parent.machine.user != request.user: messages.error(request, "Vous ne pouvez pas ajouter un alias à une machine d'un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) alias = AliasForm(request.POST or None, instance=alias_instance, infra=request.user.has_perms(('infra',))) @@ -510,7 +530,7 @@ def edit_alias(request, aliasid): reversion.set_user(request.user) reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in alias.changed_data)) messages.success(request, "Alias modifié") - return redirect("/machines/index_alias/" + str(alias_instance.interface_parent.id)) + return redirect("/machines/index_alias/" + str(alias_instance.cname.interface_parent.id)) return form({'machineform': alias}, 'machines/machine.html', request) @login_required @@ -583,7 +603,7 @@ def index_alias(request, interfaceid): if not request.user.has_perms(('cableur',)) and interface.machine.user != request.user: messages.error(request, "Vous ne pouvez pas éditer une machine d'un autre user que vous sans droit") return redirect("/users/profil/" + str(request.user.id)) - alias_list = Alias.objects.filter(interface_parent=interface).order_by('alias') + alias_list = Domain.objects.filter(cname=Domain.objects.filter(interface_parent=interface)).order_by('name') return render(request, 'machines/index_alias.html', {'alias_list':alias_list, 'interface_id': interfaceid}) @login_required @@ -608,11 +628,11 @@ def history(request, object, id): return redirect("/users/profil/" + str(request.user.id)) elif object == 'alias': try: - object_instance = Alias.objects.get(pk=id) - except Alias.DoesNotExist: + object_instance = Domain.objects.get(pk=id) + except Domain.DoesNotExist: messages.error(request, "Alias inexistant") return redirect("/machines/") - if not request.user.has_perms(('cableur',)) and object_instance.interface_parent.machine.user != request.user: + if not request.user.has_perms(('cableur',)) and object_instance.cname.interface_parent.machine.user != request.user: messages.error(request, "Vous ne pouvez pas afficher l'historique d'un alias d'un autre user que vous sans droit cableur") return redirect("/users/profil/" + str(request.user.id)) elif object == 'machinetype' and request.user.has_perms(('cableur',)): @@ -686,8 +706,8 @@ def interface_list(request): @login_required @permission_required('serveur') def alias(request): - alias = Alias.objects.filter(interface_parent__in=Interface.objects.exclude(ipv4=None)) - seria = AliasSerializer(alias, many=True) + alias = Domain.objects.filter(interface_parent=None).filter(cname=Domain.objects.filter(interface_parent__in=Interface.objects.exclude(ipv4=None))) + seria = DomainSerializer(alias, many=True) return JSONResponse(seria.data) @csrf_exempt diff --git a/search/views.py b/search/views.py index fc281766..8d5651cc 100644 --- a/search/views.py +++ b/search/views.py @@ -55,7 +55,7 @@ def search_result(search, type, request): if i == '0': recherche['users_list'] = User.objects.filter((Q(room__name__icontains = search) | Q(pseudo__icontains = search) | Q(name__icontains = search) | Q(surname__icontains = search)) & query1).order_by('state', 'surname') if i == '1': - data = Interface.objects.filter(Q(machine__user__pseudo__icontains = search) | Q(machine__user__name__icontains = search) | Q(machine__user__surname__icontains = search) | Q(dns__icontains = search) | Q(mac_address__icontains = search) | Q(ipv4__ipv4__icontains = search)) + data = Interface.objects.filter(Q(machine__user__pseudo__icontains = search) | Q(machine__user__name__icontains = search) | Q(machine__user__surname__icontains = search) | Q(mac_address__icontains = search) | Q(ipv4__ipv4__icontains = search)) for d in data: recherche['machines_list'].append(d.machine) if i == '2': From 17638985e9f96c34f692c8d1373ebdf6298c0ff3 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Sat, 24 Dec 2016 20:20:25 +0100 Subject: [PATCH 12/65] Bricoles --- machines/templates/machines/machine.html | 4 ++++ templates/base.html | 2 +- topologie/templates/topologie/aff_switch.html | 2 +- topologie/templates/topologie/switch.html | 7 +++++++ topologie/views.py | 19 ++++++++++++++++--- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/machines/templates/machines/machine.html b/machines/templates/machines/machine.html index f1ae661d..d330b03c 100644 --- a/machines/templates/machines/machine.html +++ b/machines/templates/machines/machine.html @@ -10,6 +10,10 @@ {% if interfaceform %} {% bootstrap_form_errors interfaceform %} {% endif %} +{% if domainform %} +{% bootstrap_form_errors interfaceform %} +{% endif %} +
{% csrf_token %} diff --git a/templates/base.html b/templates/base.html index 298aad2e..174c7afa 100644 --- a/templates/base.html +++ b/templates/base.html @@ -138,7 +138,7 @@