Created
December 6, 2012 11:03
-
-
Save vkgtaro/4223716 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| from django.core.exceptions import ValidationError | |
| from django.forms.fields import Field, CharField, IntegerField | |
| from jcconv import * | |
| class HiraganaFiled(CharField): | |
| """ | |
| >>> h = HiraganaFiled() | |
| >>> 'ひらがなはんかく' == h.to_python('ヒラガナハンカク') | |
| True | |
| >>> h.validate('ひらがな') | |
| >>> try: | |
| ... h.validate('ひら123がな') | |
| ... except: | |
| ... print 'ok' | |
| ok | |
| >>> 'ひらがなぜんかく' == h.clean('ヒラガナゼンカク') | |
| True | |
| """ | |
| def to_python(self, value): | |
| value = kata2hira(value) | |
| value = half2hira(value) | |
| return value | |
| def validate(self, value): | |
| if not check_hira(value): | |
| raise ValidationError(u'ひらがなで入力する必要があります。') | |
| class KatakanaFiled(CharField): | |
| """ | |
| >>> k = KatakanaFiled() | |
| >>> 'カタカナハンカク' == k.to_python('かたかなハンカク') | |
| True | |
| >>> k.validate('カタカナ') | |
| >>> try: | |
| ... k.validate('カタabcカナ') | |
| ... except: | |
| ... print 'ok' | |
| ok | |
| >>> 'カタカナゼンカク' == k.clean('かたかなゼンカク') | |
| True | |
| """ | |
| def to_python(self, value): | |
| value = hira2kata(value) | |
| value = half2kata(value) | |
| return value | |
| def validate(self, value): | |
| if not check_kata(value): | |
| raise ValidationError(u'カタカナで入力する必要があります。') | |
| class HalfCharFiled(CharField): | |
| """ | |
| >>> c = HalfCharFiled() | |
| >>> 'ほげ123Abc' == c.to_python('ほげ123Abc') | |
| True | |
| >>> 'ほげ123Abc' == c.clean('ほげ123Abc') | |
| True | |
| """ | |
| def to_python(self, value): | |
| value = wide2half(value) | |
| return value | |
| class HalfIntegerField(IntegerField): | |
| """ | |
| >>> i = HalfIntegerField() | |
| >>> '123' == i.to_python('123') | |
| True | |
| >>> '123' == i.clean('123') | |
| True | |
| """ | |
| def to_python(self, value): | |
| value = wide2half(value) | |
| return value | |
| if __name__ == "__main__": | |
| import doctest | |
| doctest.testmod() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment