Skip to content

Commit

Permalink
Models now sync successfully.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Dec 12, 2010
1 parent 5de550a commit 89daeb7
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
@@ -0,0 +1,2 @@
*.pyc
*.swp
Binary file modified boundaries/apps/api/.models.py.swp
Binary file not shown.
40 changes: 16 additions & 24 deletions boundaries/apps/api/models.py
Expand Up @@ -9,21 +9,22 @@ class BoundarySet(SluggedModel):
"""
A set of related boundaries, such as all Wards or Neighborhoods.
"""
name = models.CharField(max_length=64, unique=True
help_text="Category of boundaries, e.g. \"Community Areas\".")
name = models.CharField(max_length=64, unique=True,
help_text='Category of boundaries, e.g. "Community Areas".')
singular = models.CharField(max_length=64,
help_text="Name of a single boundary, e.g. \"Community Area\".")
help_text='Name of a single boundary, e.g. "Community Area".')
authority = models.CharField(max_length=256,
help_text="The entity responsible for this data's accuracy, e.g. \"City of Chicago\".")
help_text='The entity responsible for this data\'s accuracy, e.g. "City of Chicago".')
last_updated = models.DateField(
help_text="The last time this data was updated by its authority.")
help_text='The last time this data was updated by its authority.')
href = models.URLField(blank=True,
help_text="The url this data was found at, if any.")
help_text='The url this data was found at, if any.')
notes = models.TextField(blank=True,
help_text="Notes about loading this data, including any transformations that were applied to it.")
help_text='Notes about loading this data, including any transformations that were applied to it.')
count = models.IntegerField(
help_text="Total number of features in this boundary set.")
metadata_fields = ListField()
help_text='Total number of features in this boundary set.')
metadata_fields = ListField(separator='|', blank=True,
help_text='What, if any, metadata fields were loaded from the original dataset.')

def __unicode__(self):
"""
Expand All @@ -36,15 +37,15 @@ class Boundary(SluggedModel):
A boundary object, such as a Ward or Neighborhood.
"""
kind = models.CharField(max_length=64, db_index=True,
help_text="A category, e.g. \"Community Area\".")
help_text='A category, e.g. "Community Area".')
external_id = models.CharField(max_length=64,
help_text="The boundaries' unique id in the source dataset, or a generated one.")
help_text='The boundaries\' unique id in the source dataset, or a generated one.')
name = models.CharField(max_length=256, db_index=True,
help_text="The name of this boundary, e.g. \"Austin\".")
json_metadata = models.JSONField(blank=True,
help_text="The complete contents of the attribute table for this boundary in the source , structured as json.")
help_text='The name of this boundary, e.g. "Austin".')
metadata = JSONField(blank=True,
help_text='The complete contents of the attribute table for this boundary in the source , structured as json.')
shape = models.MultiPolygonField(srid=4269,
help_text="The geometry of this boundary in EPSG:4269.")
help_text='The geometry of this boundary in EPSG:4269.')

objects = models.GeoManager()

Expand All @@ -56,12 +57,3 @@ def __unicode__(self):
and will slug like "austin-community-area".
"""
return unicode(" ".join((self.name, self.kind)))

@property
def metadata(self):
"""
Fetch de-jsonified metadata.
"""
if self.__metadata is None:
self.__metadata = loads(self.json_metadata)
return self.__metadata
4 changes: 2 additions & 2 deletions boundaries/configs/common/settings.py
Expand Up @@ -87,9 +87,9 @@
'django.contrib.gis',
'django.contrib.sitemaps',

'tastypie'.
'tastypie',

'api',
'boundaries.apps.api',
)

# Predefined domain
Expand Down
81 changes: 75 additions & 6 deletions boundaries/lib/fields.py
@@ -1,11 +1,80 @@
"""
Custom model fields.
TODO
"""
from datetime import datetime

from django.core.serializers.json import DjangoJSONEncoder
from django.db import models

class ListField(models.TextField):
"""
Store a list of values in a Model field.
"""
__metaclass__ = models.SubfieldBase

def __init__(self, *args, **kwargs):
self.separator = kwargs.pop('separator', ',')
super(ListField, self).__init__(*args, **kwargs)

def to_python(self, value):
if not value: return

if isinstance(value, list):
return value

return value.split(self.separator)

def get_db_prep_value(self, value):
if not value: return

if not isinstance(value, list) and not isinstance(value, tuple):
raise ValueError('Value for ListField must be either a list or tuple.')

return self.separator.join([unicode(s) for s in value])

def value_to_string(self, obj):
value = self._get_val_from_obj(obj)

return self.get_db_prep_value(value)

class JSONField(models.TextField):
"""
Store arbitrary JSON in a Model field.
"""
# Used so to_python() is called
__metaclass__ = models.SubfieldBase

def to_python(self, value):
"""
Convert string value to JSON after its loaded from the database.
"""
if value == "":
return None

try:
if isinstance(value, basestring):
return json.loads(value)
except ValueError:
pass

return value

def get_prep_value(self, value):
"""
Convert our JSON object to a string before being saved.
"""
if value == "":
return None

if isinstance(value, dict) or isinstance(value, list):
value = json.dumps(value, cls=DjangoJSONEncoder)

return super(JSONField, self).get_prep_value(value)

class ListField():
pass
def value_to_string(self, obj):
"""
Called by the serializer.
"""
value = self._get_val_from_obj(obj)

class JSONField():
pass
return self.get_db_prep_value(value)
Empty file modified manage 100644 → 100755
Empty file.

0 comments on commit 89daeb7

Please sign in to comment.