From aab6fa2ba06fbdda3f8703bffb370c0001fc3d3b Mon Sep 17 00:00:00 2001 From: Hugo Levy-Falk Date: Wed, 2 Oct 2019 22:48:33 +0200 Subject: [PATCH 1/3] Fix #212 --- machines/forms.py | 16 ++++---- .../migrations/0103_auto_20191002_2222.py | 37 +++++++++++++++++++ .../migrations/0104_auto_20191002_2231.py | 19 ++++++++++ machines/models.py | 32 +++++++++++++++- machines/templates/machines/aff_alias.html | 2 + machines/templates/machines/aff_mx.html | 2 + machines/templates/machines/aff_ns.html | 2 + machines/templates/machines/aff_txt.html | 2 + .../0064_optionalmachine_default_dns_ttl.py | 20 ++++++++++ preferences/models.py | 6 ++- .../preferences/display_preferences.html | 6 ++- 11 files changed, 132 insertions(+), 12 deletions(-) create mode 100644 machines/migrations/0103_auto_20191002_2222.py create mode 100644 machines/migrations/0104_auto_20191002_2231.py create mode 100644 preferences/migrations/0064_optionalmachine_default_dns_ttl.py diff --git a/machines/forms.py b/machines/forms.py index 6a9f287d..b85fd967 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -135,16 +135,16 @@ class AddInterfaceForm(EditInterfaceForm): fields = ['machine_type', 'ipv4', 'mac_address', 'details'] -class AliasForm(FormRevMixin, ModelForm): +class AliasForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): """Ajout d'un alias (et edition), CNAME, contenant nom et extension""" class Meta: model = Domain - fields = ['name', 'extension'] + fields = ['name', 'extension', 'ttl'] def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) - user = kwargs.pop('user') + user = kwargs['user'] super(AliasForm, self).__init__(*args, prefix=prefix, **kwargs) can_use_all, _reason, _permissions = Extension.can_use_all(user) if not can_use_all: @@ -153,16 +153,16 @@ class AliasForm(FormRevMixin, ModelForm): ) -class DomainForm(FormRevMixin, ModelForm): +class DomainForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): """Ajout et edition d'un enregistrement de nom, relié à interface""" class Meta: model = Domain - fields = ['name'] + fields = ['name', 'ttl'] def __init__(self, *args, **kwargs): if 'user' in kwargs: - user = kwargs.pop('user') + user = kwargs['user'] initial = kwargs.get('initial', {}) initial['name'] = user.get_next_domain_name() kwargs['initial'] = initial @@ -339,7 +339,7 @@ class MxForm(FormRevMixin, ModelForm): class Meta: model = Mx - fields = ['zone', 'priority', 'name'] + fields = ['zone', 'priority', 'name', 'ttl'] def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) @@ -373,7 +373,7 @@ class NsForm(FormRevMixin, ModelForm): class Meta: model = Ns - fields = ['zone', 'ns'] + fields = ['zone', 'ns', 'ttl'] def __init__(self, *args, **kwargs): prefix = kwargs.pop('prefix', self.Meta.model.__name__) diff --git a/machines/migrations/0103_auto_20191002_2222.py b/machines/migrations/0103_auto_20191002_2222.py new file mode 100644 index 00000000..ef0bc830 --- /dev/null +++ b/machines/migrations/0103_auto_20191002_2222.py @@ -0,0 +1,37 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.23 on 2019-10-02 20:22 +from __future__ import unicode_literals + +from django.db import migrations, models +import machines.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0102_auto_20190303_1611'), + ('preferences', '0064_optionalmachine_default_dns_ttl'), + ] + + operations = [ + migrations.AddField( + model_name='domain', + name='ttl', + field=models.PositiveIntegerField(default=machines.models.get_default_ttl, verbose_name='Time To Live (TTL)'), + ), + migrations.AddField( + model_name='mx', + name='ttl', + field=models.PositiveIntegerField(default=172800, verbose_name='Time To Live (TTL)'), + ), + migrations.AddField( + model_name='ns', + name='ttl', + field=models.PositiveIntegerField(default=172800, verbose_name='Time To Live (TTL)'), + ), + migrations.AddField( + model_name='txt', + name='ttl', + field=models.PositiveIntegerField(default=172800, verbose_name='Time To Live (TTL)'), + ), + ] diff --git a/machines/migrations/0104_auto_20191002_2231.py b/machines/migrations/0104_auto_20191002_2231.py new file mode 100644 index 00000000..344bddb7 --- /dev/null +++ b/machines/migrations/0104_auto_20191002_2231.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.23 on 2019-10-02 20:31 +from __future__ import unicode_literals + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0103_auto_20191002_2222'), + ] + + operations = [ + migrations.AlterModelOptions( + name='domain', + options={'permissions': (('view_domain', 'Can view a domain object'), ('change_ttl', 'Can change TTL of a domain object')), 'verbose_name': 'domain', 'verbose_name_plural': 'domains'}, + ), + ] diff --git a/machines/models.py b/machines/models.py index 97881438..7b3c0dab 100644 --- a/machines/models.py +++ b/machines/models.py @@ -834,6 +834,10 @@ class Mx(RevMixin, AclMixin, models.Model): zone = models.ForeignKey('Extension', on_delete=models.PROTECT) priority = models.PositiveIntegerField() name = models.ForeignKey('Domain', on_delete=models.PROTECT) + ttl = models.PositiveIntegerField( + verbose_name=_("Time To Live (TTL)"), + default=172800, # 2 days + ) class Meta: permissions = ( @@ -859,6 +863,10 @@ class Ns(RevMixin, AclMixin, models.Model): """Liste des enregistrements name servers par zone considéérée""" zone = models.ForeignKey('Extension', on_delete=models.PROTECT) ns = models.ForeignKey('Domain', on_delete=models.PROTECT) + ttl = models.PositiveIntegerField( + verbose_name=_("Time To Live (TTL)"), + default=172800, # 2 days + ) class Meta: permissions = ( @@ -881,6 +889,10 @@ class Txt(RevMixin, AclMixin, models.Model): zone = models.ForeignKey('Extension', on_delete=models.PROTECT) field1 = models.CharField(max_length=255) field2 = models.TextField(max_length=2047) + ttl = models.PositiveIntegerField( + verbose_name=_("Time To Live (TTL)"), + default=172800, # 2 days + ) class Meta: permissions = ( @@ -1544,7 +1556,11 @@ class Ipv6List(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): return str(self.ipv6) -class Domain(RevMixin, AclMixin, models.Model): +def get_default_ttl(): + return preferences.models.OptionalMachine.get_cached_value('default_dns_ttl') + + +class Domain(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): """ Objet domain. Enregistrement A et CNAME en même temps : permet de stocker les alias et les nom de machines, suivant si interface_parent ou cname sont remplis""" @@ -1566,11 +1582,16 @@ class Domain(RevMixin, AclMixin, models.Model): blank=True, related_name='related_domain' ) + ttl = models.PositiveIntegerField( + verbose_name=_("Time To Live (TTL)"), + default=get_default_ttl + ) class Meta: unique_together = (("name", "extension"),) permissions = ( ("view_domain", _("Can view a domain object")), + ("change_ttl", _("Can change TTL of a domain object")), ) verbose_name = _("domain") verbose_name_plural = _("domains") @@ -1728,6 +1749,15 @@ class Domain(RevMixin, AclMixin, models.Model): ) return True, None, None + @staticmethod + def can_change_ttl(user_request, *_args, **_kwargs): + can = user_request.has_perm('machines.change_ttl') + return ( + can, + _("You don't have the right to change the domain's TTL.") if not can else None, + ('machines.change_ttl',) + ) + def __str__(self): return str(self.name) + str(self.extension) diff --git a/machines/templates/machines/aff_alias.html b/machines/templates/machines/aff_alias.html index 15b765c9..8fe7260c 100644 --- a/machines/templates/machines/aff_alias.html +++ b/machines/templates/machines/aff_alias.html @@ -30,12 +30,14 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Aliases" %} + {% trans "TTL" %} {% for alias in alias_list %} {{ alias }} + {{ alias.ttl }} {% can_edit alias %} {% include 'buttons/edit.html' with href='machines:edit-alias' id=alias.id %} diff --git a/machines/templates/machines/aff_mx.html b/machines/templates/machines/aff_mx.html index 1393423d..f6fe5fd8 100644 --- a/machines/templates/machines/aff_mx.html +++ b/machines/templates/machines/aff_mx.html @@ -32,6 +32,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Concerned zone" %} {% trans "Priority" %} {% trans "Record" %} + {% trans "TTL" %} @@ -40,6 +41,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ mx.zone }} {{ mx.priority }} {{ mx.name }} + {{ mx.ttl }} {% can_edit mx %} {% include 'buttons/edit.html' with href='machines:edit-mx' id=mx.id %} diff --git a/machines/templates/machines/aff_ns.html b/machines/templates/machines/aff_ns.html index 97859cfd..96534f8a 100644 --- a/machines/templates/machines/aff_ns.html +++ b/machines/templates/machines/aff_ns.html @@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Concerned zone" %} {% trans "Authoritarian interface for the concerned zone" %} + {% trans "TTL" %} @@ -38,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ ns.zone }} {{ ns.ns }} + {{ ns.ttl }} {% can_edit ns %} {% include 'buttons/edit.html' with href='machines:edit-ns' id=ns.id %} diff --git a/machines/templates/machines/aff_txt.html b/machines/templates/machines/aff_txt.html index ca0988d0..bb140ce8 100644 --- a/machines/templates/machines/aff_txt.html +++ b/machines/templates/machines/aff_txt.html @@ -31,6 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Concerned zone" %} {% trans "Record" %} + {% trans "TTL" %} @@ -38,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ txt.zone }} {{ txt.dns_entry }} + {{ txt.ttl }} {% can_edit txt %} {% include 'buttons/edit.html' with href='machines:edit-txt' id=txt.id %} diff --git a/preferences/migrations/0064_optionalmachine_default_dns_ttl.py b/preferences/migrations/0064_optionalmachine_default_dns_ttl.py new file mode 100644 index 00000000..f87cafca --- /dev/null +++ b/preferences/migrations/0064_optionalmachine_default_dns_ttl.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.23 on 2019-10-02 20:22 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('preferences', '0063_mandate'), + ] + + operations = [ + migrations.AddField( + model_name='optionalmachine', + name='default_dns_ttl', + field=models.PositiveIntegerField(default=172800, verbose_name='Default Time To Live (TTL) for CNAME, A and AAA records.'), + ), + ] diff --git a/preferences/models.py b/preferences/models.py index e52998cd..b7153e72 100644 --- a/preferences/models.py +++ b/preferences/models.py @@ -173,6 +173,10 @@ class OptionalMachine(AclMixin, PreferencesModel): create_machine = models.BooleanField( default=True ) + default_dns_ttl = models.PositiveIntegerField( + verbose_name=_("Default Time To Live (TTL) for CNAME, A and AAA records."), + default=172800, # 2 days + ) @cached_property def ipv6(self): @@ -545,7 +549,7 @@ class Mandate(RevMixin, AclMixin, models.Model): def is_over(self): return self.end_date is None - + def __str__(self): return str(self.president) + ' ' + str(self.start_date.year) diff --git a/preferences/templates/preferences/display_preferences.html b/preferences/templates/preferences/display_preferences.html index ff186f11..fcc9064f 100644 --- a/preferences/templates/preferences/display_preferences.html +++ b/preferences/templates/preferences/display_preferences.html @@ -177,10 +177,12 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Maximum number of DNS aliases allowed for a standard user" %} {{ machineoptions.max_lambdauser_aliases }} - {% trans "IPv6 support" %} - {{ machineoptions.ipv6_mode }} + {% trans "Default Time To Live (TTL) for CNAME, A and AAA records." %} + {{ machineoptions.default_dns_ttl }} + {% trans "IPv6 support" %} + {{ machineoptions.ipv6_mode }} {% trans "Creation of machines" %} {{ machineoptions.create_machine|tick }} From 13a06c1a67ec659995a05d0af9f8ff5f89f733e1 Mon Sep 17 00:00:00 2001 From: Hugo Levy-Falk Date: Wed, 2 Oct 2019 23:53:27 +0200 Subject: [PATCH 2/3] TTL for DNAMEs --- machines/migrations/0105_dname_ttl.py | 20 ++++++++++++++++++++ machines/models.py | 4 ++++ machines/templates/machines/aff_dname.html | 2 ++ 3 files changed, 26 insertions(+) create mode 100644 machines/migrations/0105_dname_ttl.py diff --git a/machines/migrations/0105_dname_ttl.py b/machines/migrations/0105_dname_ttl.py new file mode 100644 index 00000000..d3c38073 --- /dev/null +++ b/machines/migrations/0105_dname_ttl.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.23 on 2019-10-02 21:47 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('machines', '0104_auto_20191002_2231'), + ] + + operations = [ + migrations.AddField( + model_name='dname', + name='ttl', + field=models.PositiveIntegerField(default=172800, verbose_name='Time To Live (TTL)'), + ), + ] diff --git a/machines/models.py b/machines/models.py index 7b3c0dab..708dd8c9 100644 --- a/machines/models.py +++ b/machines/models.py @@ -915,6 +915,10 @@ class DName(RevMixin, AclMixin, models.Model): """A DNAME entry for the DNS.""" zone = models.ForeignKey('Extension', on_delete=models.PROTECT) alias = models.CharField(max_length=255) + ttl = models.PositiveIntegerField( + verbose_name=_("Time To Live (TTL)"), + default=172800, # 2 days + ) class Meta: permissions = ( diff --git a/machines/templates/machines/aff_dname.html b/machines/templates/machines/aff_dname.html index 6d07a7bc..4073a388 100644 --- a/machines/templates/machines/aff_dname.html +++ b/machines/templates/machines/aff_dname.html @@ -29,6 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% trans "Target zone" %} {% trans "Record" %} + {% trans "TTL" %} @@ -36,6 +37,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {{ dname.zone }} {{ dname.dns_entry }} + {{ dname.ttl }} {% can_edit dname %} {% include 'buttons/edit.html' with href='machines:edit-dname' id=dname.id %} From 48ec3d353efd719e1343f1bf88e07b60a0d73e85 Mon Sep 17 00:00:00 2001 From: Hugo Levy-Falk Date: Sat, 12 Oct 2019 17:22:51 +0200 Subject: [PATCH 3/3] default ttl to 0 --- machines/migrations/0103_auto_20191002_2222.py | 4 ++-- machines/models.py | 7 ++----- ..._dns_ttl.py => 0066_optionalmachine_default_dns_ttl.py} | 2 +- 3 files changed, 5 insertions(+), 8 deletions(-) rename preferences/migrations/{0064_optionalmachine_default_dns_ttl.py => 0066_optionalmachine_default_dns_ttl.py} (90%) diff --git a/machines/migrations/0103_auto_20191002_2222.py b/machines/migrations/0103_auto_20191002_2222.py index ef0bc830..43adac74 100644 --- a/machines/migrations/0103_auto_20191002_2222.py +++ b/machines/migrations/0103_auto_20191002_2222.py @@ -10,14 +10,14 @@ class Migration(migrations.Migration): dependencies = [ ('machines', '0102_auto_20190303_1611'), - ('preferences', '0064_optionalmachine_default_dns_ttl'), + ('preferences', '0066_optionalmachine_default_dns_ttl'), ] operations = [ migrations.AddField( model_name='domain', name='ttl', - field=models.PositiveIntegerField(default=machines.models.get_default_ttl, verbose_name='Time To Live (TTL)'), + field=models.PositiveIntegerField(default=0, verbose_name='Time To Live (TTL)'), ), migrations.AddField( model_name='mx', diff --git a/machines/models.py b/machines/models.py index 708dd8c9..ca881033 100644 --- a/machines/models.py +++ b/machines/models.py @@ -1560,10 +1560,6 @@ class Ipv6List(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): return str(self.ipv6) -def get_default_ttl(): - return preferences.models.OptionalMachine.get_cached_value('default_dns_ttl') - - class Domain(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): """ Objet domain. Enregistrement A et CNAME en même temps : permet de stocker les alias et les nom de machines, suivant si interface_parent @@ -1588,7 +1584,8 @@ class Domain(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): ) ttl = models.PositiveIntegerField( verbose_name=_("Time To Live (TTL)"), - default=get_default_ttl + default=0 # 0 means that the re2o-service for DNS should retrieve the + # default TTL ) class Meta: diff --git a/preferences/migrations/0064_optionalmachine_default_dns_ttl.py b/preferences/migrations/0066_optionalmachine_default_dns_ttl.py similarity index 90% rename from preferences/migrations/0064_optionalmachine_default_dns_ttl.py rename to preferences/migrations/0066_optionalmachine_default_dns_ttl.py index f87cafca..47e3519e 100644 --- a/preferences/migrations/0064_optionalmachine_default_dns_ttl.py +++ b/preferences/migrations/0066_optionalmachine_default_dns_ttl.py @@ -8,7 +8,7 @@ from django.db import migrations, models class Migration(migrations.Migration): dependencies = [ - ('preferences', '0063_mandate'), + ('preferences', '0065_auto_20191010_1227'), ] operations = [