2008-09-02 06:15:35 +08:00
|
|
|
from django.conf import settings
|
2009-12-17 02:13:34 +08:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2009-12-14 05:56:18 +08:00
|
|
|
from django.db.models.fields import Field, CharField
|
|
|
|
from django.contrib.localflavor.us.us_states import STATE_CHOICES
|
2009-12-17 02:13:34 +08:00
|
|
|
|
2009-12-14 05:56:18 +08:00
|
|
|
class USStateField(CharField):
|
2009-12-17 02:13:34 +08:00
|
|
|
|
|
|
|
description = _("U.S. state (two uppercase letters)")
|
|
|
|
|
2009-12-14 05:56:18 +08:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
kwargs['choices'] = STATE_CHOICES
|
|
|
|
kwargs['max_length'] = 2
|
|
|
|
super(USStateField, self).__init__(*args, **kwargs)
|
2009-12-17 02:13:34 +08:00
|
|
|
|
2008-09-02 06:15:35 +08:00
|
|
|
class PhoneNumberField(Field):
|
2009-12-17 02:13:34 +08:00
|
|
|
|
|
|
|
description = _("Phone number")
|
|
|
|
|
2008-09-02 06:15:35 +08:00
|
|
|
def get_internal_type(self):
|
|
|
|
return "PhoneNumberField"
|
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def db_type(self, connection):
|
|
|
|
if connection.settings_dict['ENGINE'] == 'django.db.backends.oracle':
|
2008-09-02 06:15:35 +08:00
|
|
|
return 'VARCHAR2(20)'
|
|
|
|
else:
|
|
|
|
return 'varchar(20)'
|
|
|
|
|
|
|
|
def formfield(self, **kwargs):
|
|
|
|
from django.contrib.localflavor.us.forms import USPhoneNumberField
|
|
|
|
defaults = {'form_class': USPhoneNumberField}
|
|
|
|
defaults.update(kwargs)
|
|
|
|
return super(PhoneNumberField, self).formfield(**defaults)
|
|
|
|
|