summaryrefslogtreecommitdiffstats
path: root/main/fields.py
blob: 948cb5d991cac313166ce5423d14fc73be449f58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from django.db import models
from django.core.validators import RegexValidator


class PositiveBigIntegerField(models.BigIntegerField):
    _south_introspects = True

    def get_internal_type(self):
        return "BigIntegerField"

    def formfield(self, **kwargs):
        defaults = { 'min_value': 0 }
        defaults.update(kwargs)
        return super(PositiveBigIntegerField, self).formfield(**defaults)

class PGPKeyField(models.CharField):
    _south_introspects = True

    def __init__(self, *args, **kwargs):
        super(PGPKeyField, self).__init__(*args, **kwargs)
        self.validators.append(RegexValidator(r'^[0-9A-F]{40}$',
            "Ensure this value consists of 40 hex characters.", 'hex_char'))

    def to_python(self, value):
        if value == '' or value is None:
            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]
        # make all (hex letters) uppercase
        return value.upper()

    def formfield(self, **kwargs):
        # override so we don't set max_length form field attribute
        return models.Field.formfield(self, **kwargs)


# vim: set ts=4 sw=4 et: