Skip to content

Commit

Permalink
UUIDField 🧽
Browse files Browse the repository at this point in the history
  • Loading branch information
Sibyx committed Nov 14, 2019
1 parent a360b84 commit fce8a71
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.1 : 14.11.2019

- **Feature**: Introduced `UUIDField`

## 0.4.0 : 13.11.2019

- **Feature**: Introduced `DictionaryField`
Expand Down
2 changes: 1 addition & 1 deletion django_request_formatter/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.4.0'
__version__ = '0.4.1'
28 changes: 28 additions & 0 deletions django_request_formatter/fields.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import copy
import datetime
import enum
import math
import re
import typing
import uuid
from decimal import Decimal, DecimalException
from enum import Enum
from typing import List
Expand Down Expand Up @@ -294,6 +296,32 @@ def _set_regex(self, regex):
regex = property(_get_regex, _set_regex)


class UUIDField(CharField):
default_error_messages = {
'invalid': _('Enter a valid UUID.'),
}

def __init__(self, **kwargs):
kwargs['max_length'] = 36
kwargs['min_length'] = 36
super().__init__(**kwargs)

def to_python(self, value):
value = super().to_python(value)
if value in self.empty_values:
return None
if not isinstance(value, uuid.UUID):
value = uuid.UUID(value)
return value

def validate(self, value):
if value not in self.empty_values and not isinstance(value, uuid.UUID):
try:
uuid.UUID(value)
except ValueError:
raise ValidationError(self.error_messages['invalid'], code='invalid')


class EmailField(CharField):
default_validators = [validators.validate_email]

Expand Down

0 comments on commit fce8a71

Please sign in to comment.