Permalink
Browse files

added some of the extensions

  • Loading branch information...
1 parent 6bd7b9f commit 9a26157487c62984fb0298e1efab898c65bda59b @jkatz committed Sep 14, 2011
Showing with 161 additions and 1 deletion.
  1. 0 __init__.py
  2. +34 −0 ext/forms.py
  3. +126 −1 ext/models.py
  4. +1 −0 settings.py
View
No changes.
View
@@ -0,0 +1,34 @@
+from django import forms
+import re
+
+# list of custom fields that are tied to custom form elements.
+
+class IntegerArrayField(forms.Field):
+
+ def __init__(self, **kwargs):
+ super(IntegerArrayField, self).__init__(**kwargs)
+
+ def prepare_value(self, value):
+ if isinstance(value, list):
+ return re.sub(r'\[|\]', '', str(value))
+ return value
+
+ def validate(self, value):
+ super(IntegerArrayField, self).validate(value)
+ if not re.search('^[\s,0-9]*$', value):
+ raise forms.ValidationError, "Please use only integers in your data"
+
+class MoneyField(forms.Field):
+
+ def __init__(self, **kwargs):
+ super(MoneyField, self).__init__(**kwargs)
+
+ def clean(self, value):
+ value = re.sub(',|\$', '', value)
+ return super(MoneyField, self).clean(value)
+
+ def validate(self, value):
+ super(MoneyField, self).validate(value)
+ value = re.sub(',|\$', '', value)
+ if not re.search('^[1-9][0-9]*(\.[0-9]{2})?$', value):
+ raise forms.ValidationError, "Please enter in a proper money format (99 or 99.99)"
View
@@ -1,3 +1,128 @@
from django.db import models
+import forms
+import re
+from south.modelsinspector import add_introspection_rules
-# Below are all the custom fields used for DB models
+# Below are all the custom fields used for DB models
+class DayIntervalField(models.Field):
+ SECS_IN_DAY = 86400
+
+ description = "time interval"
+ __metaclass__ = models.SubfieldBase
+
+ def __init__(self, *args, **kwargs):
+ super(DayIntervalField, self).__init__(*args, **kwargs)
+
+ def db_type(self, connection):
+ return 'interval'
+
+ def get_db_prep_value(self, value, connection, prepared=False):
+ try:
+ value = int(value)
+ return "%d %s" % (value, 'days')
+ except:
+ if re.match(r"days$", value):
+ return value
+ elif value:
+ return "%s %s" % (value, 'days')
+ else:
+ return None
+
+class EnumField(models.Field):
+ """
+ generic enumerated type. have your custom types inherit this
+ """
+ description = "enumerated type"
+
+ def __init__(self, *args, **kwargs):
+ self.enum = kwargs['enum']
+ del kwargs['enum']
+ super(EnumField, self).__init__(*args, **kwargs)
+
+ def db_type(self, connection):
+ return self.enum
+
+
+class IntegerArrayField(models.Field):
+ description = "Use PostgreSQL integer arrays"
+ __metaclass__ = models.SubfieldBase
+
+ def __init__(self, *args, **kwargs):
+ super(IntegerArrayField, self).__init__(*args, **kwargs)
+
+ def db_type(self, connection):
+ return 'integer[]'
+
+ def formfield(self, **kwargs):
+ defaults = { 'form_class': forms.IntegerArrayField }
+ defaults.update(kwargs)
+ return super(IntegerArrayField, self).formfield(**defaults)
+
+ def get_prep_value(self, value):
+ if isinstance(value, list):
+ db_value = str(value)
+ db_value = re.sub(r'\[', '{', db_value)
+ db_value = re.sub(r'\]', '}', db_value)
+ return db_value
+ elif isinstance(value, (str, unicode)):
+ if not value: return None
+ return value
+
+ def to_python(self, value):
+ if isinstance(value, list):
+ return value
+ elif isinstance(value, (str, unicode)):
+ if not value: return None
+ value = re.sub(r'\{|\}', '', value).split(',')
+ return map(lambda x: int(x), value)
+
+class MoneyField(models.Field):
+ description = "Store money in it's proper integer format, but display it as [0-9]+.[0-9]{2} format"
+ __metaclass__ = models.SubfieldBase
+
+ def __init__(self, *args, **kwargs):
+ super(MoneyField, self).__init__(*args, **kwargs)
+
+ def db_type(self, connection):
+ return 'int'
+
+ def formfield(self, **kwargs):
+ defaults = { 'form_class': forms.MoneyField }
+ defaults.update(kwargs)
+ return super(MoneyField, self).formfield(**defaults)
+
+ def get_prep_value(self, value):
+ if isinstance(value, (str, unicode)):
+ if value:
+ value = value.replace(',', '')
+ return int(float(value) * 100)
+ else:
+ return None
+ elif isinstance(value, float):
+ return int(value * 100)
+ elif isinstance(value, (int, long)):
+ return value
+
+ def to_python(self, value):
+ if isinstance(value, (str, unicode)):
+ return value
+ elif isinstance(value, float):
+ return "%.2f" % value
+ elif isinstance(value, (int, long)):
+ return "%.2f" % (value / 100.0)
+
+class PointField(models.Field):
+ description = "point type"
+ __metaclass__ = models.SubfieldBase
+
+ def __init__(self, *args, **kwargs):
+ super(PointField, self).__init__(*args, **kwargs)
+
+ def db_type(self, connection):
+ return 'point'
+
+add_introspection_rules([], ["^ext\.DayIntervalField"])
+add_introspection_rules([], ["^ext\.EnumField"])
+add_introspection_rules([], ["^ext\.IntegerArrayField"])
+add_introspection_rules([], ["^ext\.MoneyField"])
+add_introspection_rules([], ["^ext\.PointField"])
View
@@ -122,6 +122,7 @@
'django.contrib.admin',
'django.contrib.admindocs',
'ext',
+ 'south', # handles migrations
)
# A sample logging configuration. The only tangible logging

0 comments on commit 9a26157

Please sign in to comment.