summaryrefslogtreecommitdiffstats
path: root/main/models.py
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2011-07-02 01:59:28 +0200
committerDan McGee <dan@archlinux.org>2011-07-05 16:47:20 +0200
commite37d465c4eb03259b3ce0c89e43c2c21badb0063 (patch)
treedbdf984b91a615e4ea7fcc21fafa141572ba9067 /main/models.py
parent1bcb2b7ed0bdb11ea3b22cdbc93192b4259303d5 (diff)
downloadarchweb-e37d465c4eb03259b3ce0c89e43c2c21badb0063.tar.gz
archweb-e37d465c4eb03259b3ce0c89e43c2c21badb0063.tar.xz
Allow more lenient data entry for PGP key field
Don't allow the max_length parameter to make it through to the HTML form, silently cutting off HTML cut and pastes. Trim out spaces automatically, as well as '0x' and '2048R/' type prefixes. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'main/models.py')
-rw-r--r--main/models.py21
1 files changed, 20 insertions, 1 deletions
diff --git a/main/models.py b/main/models.py
index 9010817..2f54908 100644
--- a/main/models.py
+++ b/main/models.py
@@ -22,6 +22,25 @@ class PositiveBigIntegerField(models.BigIntegerField):
defaults.update(kwargs)
return super(PositiveBigIntegerField, self).formfield(**defaults)
+class PGPKeyField(models.CharField):
+ _south_introspects = True
+
+ def to_python(self, value):
+ if value == '':
+ return None
+ value = super(PGPKeyField, self).to_python(value)
+ # remove all spaces
+ value = value.replace(' ', '')
+ # prune prefixes, either 0x or 2048R/ type
+ if value.startswith('0x'):
+ value = value[2:]
+ value = value.split('/')[-1]
+ return value
+
+ def formfield(self, **kwargs):
+ # override so we don't set max_length form field attribute
+ return models.Field.formfield(self, **kwargs)
+
def validate_pgp_key_length(value):
if len(value) not in (8, 16, 40):
raise ValidationError(
@@ -45,7 +64,7 @@ class UserProfile(models.Model):
max_length=50,
help_text="Required field")
other_contact = models.CharField(max_length=100, null=True, blank=True)
- pgp_key = models.CharField(max_length=40, null=True, blank=True,
+ pgp_key = PGPKeyField(max_length=40, null=True, blank=True,
verbose_name="PGP key", validators=[RegexValidator(r'^[0-9A-F]+$',
"Ensure this value consists of only hex characters.", 'hex_char'),
validate_pgp_key_length],