From 332a6a8149720cf68800e5022d00f18a2cc7b640 Mon Sep 17 00:00:00 2001 From: Daniel STAN Date: Mon, 9 Oct 2017 23:02:29 +0200 Subject: [PATCH] =?UTF-8?q?users:=20plus=20de=20granularit=C3=A9=20dans=20?= =?UTF-8?q?les=20bans?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- users/migrations/0056_1_bantype_alter.py | 31 +++++++++++++++++ users/migrations/0056_2_bantype_pop.py | 43 ++++++++++++++++++++++++ users/migrations/0056_3_bantype_clean.py | 25 ++++++++++++++ users/models.py | 19 +++++------ 4 files changed, 108 insertions(+), 10 deletions(-) create mode 100644 users/migrations/0056_1_bantype_alter.py create mode 100644 users/migrations/0056_2_bantype_pop.py create mode 100644 users/migrations/0056_3_bantype_clean.py diff --git a/users/migrations/0056_1_bantype_alter.py b/users/migrations/0056_1_bantype_alter.py new file mode 100644 index 00000000..67d81173 --- /dev/null +++ b/users/migrations/0056_1_bantype_alter.py @@ -0,0 +1,31 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-10-09 +# Modified by Tudor, with love +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0055_auto_20171003_0556'), + ] + + operations = [ + migrations.CreateModel( + name='BanType', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=255)), + ('description', models.TextField(help_text="Description de l'effet et des raisons de la blacklist")), + ], + ), + migrations.AddField( + model_name='ban', + name='ban_type', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='users.BanType'), + ), + ] diff --git a/users/migrations/0056_2_bantype_pop.py b/users/migrations/0056_2_bantype_pop.py new file mode 100644 index 00000000..82d6d15a --- /dev/null +++ b/users/migrations/0056_2_bantype_pop.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-10-09 +# Modified by Tudor, with love +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + +def add_foreign_bans(apps, schema_editor): + Ban = apps.get_model('users', 'Ban') + BanType = apps.get_model('users', 'BanType') + b_hard = BanType(name='HARD', description="aucun accès") + b_soft = BanType(name='SOFT', description="accès local seulement") + b_bridage = BanType(name='BRIDAGE', description="bridage du débit") + + # Ajoute et synchronise le nouveau field à partir du field state + db_alias = schema_editor.connection.alias + for b_type in [b_hard, b_soft, b_bridage]: + b_type.save(using=db_alias) + bans = Ban.objects.using(db_alias).all() + for b in bans: + if b.state == 0: + b.ban_type = b_hard + elif b.state == 1: + b.ban_type = b_soft + elif b.state == 2: + b.ban_type = b_bridage + else: + raise Exception("Un ban state inconnu %r n'a pu être converti" % b.state) + b.save(using=db_alias) + +def remove_foreign_bans(apps, schema_editor): + pass + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0056_1_bantype_alter'), + ] + + operations = [ + migrations.RunPython(add_foreign_bans, remove_foreign_bans), + ] diff --git a/users/migrations/0056_3_bantype_clean.py b/users/migrations/0056_3_bantype_clean.py new file mode 100644 index 00000000..9cd2a8b4 --- /dev/null +++ b/users/migrations/0056_3_bantype_clean.py @@ -0,0 +1,25 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2017-10-09 20:59 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0056_2_bantype_pop'), + ] + + operations = [ + migrations.RemoveField( + model_name='ban', + name='state', + ), + migrations.AlterField( + model_name='ban', + name='ban_type', + field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='users.BanType'), + ), + ] diff --git a/users/models.py b/users/models.py index 78b76156..f919a904 100644 --- a/users/models.py +++ b/users/models.py @@ -725,25 +725,24 @@ class ListShell(models.Model): def __str__(self): return self.shell +class BanType(models.Model): + """Type de bannissement""" + name = models.CharField(max_length=255) + description = models.TextField(help_text="Description de l'effet et des " + "raisons de la blacklist") + # d'autres champs pour décrire les effets viendront si besoin + + class Ban(models.Model): """ Bannissement. Actuellement a un effet tout ou rien. Gagnerait à être granulaire""" PRETTY_NAME = "Liste des bannissements" - STATE_HARD = 0 - STATE_SOFT = 1 - STATE_BRIDAGE = 2 - STATES = ( - (0, 'HARD (aucun accès)'), - (1, 'SOFT (accès local seulement)'), - (2, 'BRIDAGE (bridage du débit)'), - ) - user = models.ForeignKey('User', on_delete=models.PROTECT) raison = models.CharField(max_length=255) date_start = models.DateTimeField(auto_now_add=True) date_end = models.DateTimeField(help_text='%d/%m/%y %H:%M:%S') - state = models.IntegerField(choices=STATES, default=STATE_HARD) + ban_type = models.ForeignKey(BanType) def notif_ban(self): """ Prend en argument un objet ban, envoie un mail de notification """