From bfd79d44eb20276c4efa9d05e524e4d55279f939 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Fri, 28 Dec 2018 20:58:43 +0100 Subject: [PATCH] Fix #192 : Gpgfp validation et formatage --- users/forms.py | 7 +----- users/migrations/0079_auto_20181228_2039.py | 20 ++++++++++++++++ users/models.py | 26 ++++++++++++++++----- 3 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 users/migrations/0079_auto_20181228_2039.py diff --git a/users/forms.py b/users/forms.py index ca01c52d..81c31cae 100644 --- a/users/forms.py +++ b/users/forms.py @@ -368,6 +368,7 @@ class AdherentForm(FormRevMixin, FieldPermissionFormMixin, ModelForm): remove_user_room(self.cleaned_data.get('room')) return + class AdherentCreationForm(AdherentForm): """Formulaire de création d'un user. AdherentForm auquel on ajoute une checkbox afin d'éviter les @@ -398,12 +399,6 @@ class AdherentEditForm(AdherentForm): if 'shell' in self.fields: self.fields['shell'].empty_label = _("Default shell") - def clean_gpg_fingerprint(self): - """Format the GPG fingerprint""" - gpg_fingerprint = self.cleaned_data.get('gpg_fingerprint', None) - if gpg_fingerprint: - return gpg_fingerprint.replace(' ', '').upper() - class Meta: model = Adherent fields = [ diff --git a/users/migrations/0079_auto_20181228_2039.py b/users/migrations/0079_auto_20181228_2039.py new file mode 100644 index 00000000..79ab56d9 --- /dev/null +++ b/users/migrations/0079_auto_20181228_2039.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.7 on 2018-12-28 19:39 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('users', '0078_auto_20181011_1405'), + ] + + operations = [ + migrations.AlterField( + model_name='adherent', + name='gpg_fingerprint', + field=models.CharField(blank=True, max_length=49, null=True), + ), + ] diff --git a/users/models.py b/users/models.py index 9f17615d..b8f79942 100755 --- a/users/models.py +++ b/users/models.py @@ -1053,20 +1053,27 @@ class Adherent(User): null=True ) gpg_fingerprint = models.CharField( - max_length=40, + max_length=49, blank=True, null=True, - validators=[RegexValidator( - '^[0-9A-F]{40}$', - message=_("A GPG fingerprint must contain 40 hexadecimal" - " characters.") - )] ) class Meta(User.Meta): verbose_name = _("member") verbose_name_plural = _("members") + def format_gpgfp(self): + """Format gpg finger print as AAAA BBBB... from a string AAAABBBB....""" + self.gpg_fingerprint = ' '.join([self.gpg_fingerprint[i:i + 4] for i in range(0, len(self.gpg_fingerprint), 4)]) + + def validate_gpgfp(self): + """Validate from raw entry if is it a valid gpg fp""" + if self.gpg_fingerprint: + gpg_fingerprint = self.gpg_fingerprint.replace(' ', '').upper() + if not re.compile("^[0-9A-F]{40}$").match(gpg_fingerprint): + raise ValidationError(_("A gpg fingerprint must contain 40 hexadecimal carracters")) + self.gpg_fingerprint = gpg_fingerprint + @classmethod def get_instance(cls, adherentid, *_args, **_kwargs): """Try to find an instance of `Adherent` with the given id. @@ -1097,6 +1104,13 @@ class Adherent(User): _("You don't have the right to create a user.") ) + def clean(self, *args, **kwargs): + """Format the GPG fingerprint""" + super(Adherent, self).clean(*args, **kwargs) + if self.gpg_fingerprint: + self.validate_gpgfp() + self.format_gpgfp() + class Club(User): """ A class representing a club (it is considered as a user