# Path: chain/core/models.py
# class GeoLocation(models.Model):
#     latitude = models.FloatField()
#     longitude = models.FloatField()
#     elevation = models.FloatField(null=True, blank=True)
# 
# Path: chain/settings.py
# USE_X_FORWARDED_HOST = True
# CORS_ORIGIN_ALLOW_ALL = True
# SITE_ID = 1
# USE_I18N = True
# USE_L10N = True
# USE_TZ = True
# MEDIA_ROOT = ''
# MEDIA_URL = ''
# STATIC_ROOT = '/srv/chain/static/'
# STATIC_URL = '/static/'
# STATICFILES_DIRS = (
#     # Put strings here, like "/home/html/static" or "C:/www/django/static".
#     # Always use forward slashes, even on Windows.
#     # Don't forget to use absolute paths, not relative paths.
# )
# STATICFILES_FINDERS = (
#     'django.contrib.staticfiles.finders.FileSystemFinder',
#     'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#     # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
# )
# TEMPLATE_LOADERS = (
#     'django.template.loaders.filesystem.Loader',
#     'django.template.loaders.app_directories.Loader',
#     # 'django.template.loaders.eggs.Loader',
# )
# MIDDLEWARE_CLASSES = (
#     # 'debug_toolbar.middleware.DebugToolbarMiddleware',
#     'corsheaders.middleware.CorsMiddleware',
#     'django.middleware.common.CommonMiddleware',
#     'django.contrib.sessions.middleware.SessionMiddleware',
#     'django.middleware.csrf.CsrfViewMiddleware',
#     'django.contrib.auth.middleware.AuthenticationMiddleware',
#     'django.contrib.messages.middleware.MessageMiddleware',
#     # Uncomment the next line for simple clickjacking protection:
#     # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
# )
# ROOT_URLCONF = 'chain.urls'
# WSGI_APPLICATION = 'chain.wsgi.application'
# TEMPLATE_DIRS = (
#     # Put strings here, like "/home/html/django_templates"
#     # Always use forward slashes, even on Windows.
#     # Don't forget to use absolute paths, not relative paths.
# )
# INSTALLED_APPS = (
#     'django.contrib.auth',
#     'django.contrib.contenttypes',
#     'django.contrib.sessions',
#     'django.contrib.sites',
#     'django.contrib.messages',
#     'django.contrib.staticfiles',
#     # 'debug_toolbar',
#     'chain.core',
#     'django_extensions',
#     'corsheaders',
#     # Uncomment the next line to enable the admin:
#     'django.contrib.admin',
#     # Uncomment the next line to enable admin documentation:
#     # 'django.contrib.admindocs',
# )
# SOUTH_TESTS_MIGRATE = False
# TEST_RUNNER = 'django.test.runner.DiscoverRunner'
# DEBUG_TOOLBAR_PATCH_SETTINGS = False
# INTERNAL_IPS = ['127.0.0.1', '18.85.58.156']
# LOGGING = {
#     'version': 1,
#     'disable_existing_loggers': False,
#     'filters': {
#         'require_debug_false': {
#             '()': 'django.utils.log.RequireDebugFalse'
#         }
#     },
#     'handlers': {
#         'mail_admins': {
#             'level': 'ERROR',
#             'filters': ['require_debug_false'],
#             'class': 'chain.helpers.logging.ThrottledAdminEmailHandler'
#         },
#         # null handler to throw away messages
#         'null': {
#             'level': 'DEBUG',
#             'class': 'logging.NullHandler',
#         },
#     },
#     'loggers': {
#         # Silence SuspiciousOperation.DisallowedHost exception ('Invalid
#         # HTTP_HOST' header messages). Set the handler to 'null' so we don't
#         # get those annoying emails.
#         'django.security.DisallowedHost': {
#             'handlers': ['null'],
#             'propagate': False,
#         },
#         'django.request': {
#             'handlers': ['mail_admins'],
#             'level': 'ERROR',
#             'propagate': True,
#         },
#     }
# }


import logging
import json
import zmq
import re
from django.conf.urls import patterns, url
from django.db import models
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.views.decorators.csrf import csrf_exempt
from django.db import IntegrityError
from datetime import datetime
from jinja2 import Environment, PackageLoader
from urlparse import urlparse, urlunparse, parse_qs
from urllib import urlencode
from chain.core.models import GeoLocation
from chain.settings import WEBSOCKET_PATH, WEBSOCKET_HOST, \
    ZMQ_PASSTHROUGH_URL_PULL
from pytz import AmbiguousTimeError
from django.contrib.contenttypes.models import ContentType
from django.utils import six
from django.utils.encoding import smart_text
from django.utils.cache import patch_vary_headers
                new_obj_data, stub_field_name, self._data[stub_field_name])
        if self.model_has_field('geo_location') and \
                'geoLocation' in self._data:
            dataloc = self._data['geoLocation']
            loc = GeoLocation(elevation=dataloc.get('elevation', None),
                              latitude=dataloc['latitude'],
                              longitude=dataloc['longitude'])
            loc.save()
            new_obj_data['geo_location'] = loc
        # the query string may contain more object data, for instance if
        # we're posting to a child collection resource

        new_obj_data.update(self._filters)
        self._obj = self.model(**new_obj_data)
        self._state = 'object'
        return self._obj

    def update(self, data):
        '''Updates this resource given the data, which is expected to be a dict
        in the format given by this resource's schema'''
        for k, v in data.iteritems():
            if k in self.model_fields:
                sanitized_value = type(self).sanitize_field_value(k, v)
                setattr(self._obj, k, sanitized_value)
            elif k in self.stub_fields:
                setattr(self._obj, k,
                        self.stub_object_finding(self._obj, k, v))
            elif k == 'geoLocation':
                loc = self._obj.geo_location
                if loc is None:

# Path: chain/core/models.py
# class GeoLocation(models.Model):
#     latitude = models.FloatField()
#     longitude = models.FloatField()
#     elevation = models.FloatField(null=True, blank=True)
# 
# Path: chain/settings.py
# USE_X_FORWARDED_HOST = True
# CORS_ORIGIN_ALLOW_ALL = True
# SITE_ID = 1
# USE_I18N = True
# USE_L10N = True
# USE_TZ = True
# MEDIA_ROOT = ''
# MEDIA_URL = ''
# STATIC_ROOT = '/srv/chain/static/'
# STATIC_URL = '/static/'
# STATICFILES_DIRS = (
#     # Put strings here, like "/home/html/static" or "C:/www/django/static".
#     # Always use forward slashes, even on Windows.
#     # Don't forget to use absolute paths, not relative paths.
# )
# STATICFILES_FINDERS = (
#     'django.contrib.staticfiles.finders.FileSystemFinder',
#     'django.contrib.staticfiles.finders.AppDirectoriesFinder',
#     # 'django.contrib.staticfiles.finders.DefaultStorageFinder',
# )
# TEMPLATE_LOADERS = (
#     'django.template.loaders.filesystem.Loader',
#     'django.template.loaders.app_directories.Loader',
#     # 'django.template.loaders.eggs.Loader',
# )
# MIDDLEWARE_CLASSES = (
#     # 'debug_toolbar.middleware.DebugToolbarMiddleware',
#     'corsheaders.middleware.CorsMiddleware',
#     'django.middleware.common.CommonMiddleware',
#     'django.contrib.sessions.middleware.SessionMiddleware',
#     'django.middleware.csrf.CsrfViewMiddleware',
#     'django.contrib.auth.middleware.AuthenticationMiddleware',
#     'django.contrib.messages.middleware.MessageMiddleware',
#     # Uncomment the next line for simple clickjacking protection:
#     # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
# )
# ROOT_URLCONF = 'chain.urls'
# WSGI_APPLICATION = 'chain.wsgi.application'
# TEMPLATE_DIRS = (
#     # Put strings here, like "/home/html/django_templates"
#     # Always use forward slashes, even on Windows.
#     # Don't forget to use absolute paths, not relative paths.
# )
# INSTALLED_APPS = (
#     'django.contrib.auth',
#     'django.contrib.contenttypes',
#     'django.contrib.sessions',
#     'django.contrib.sites',
#     'django.contrib.messages',
#     'django.contrib.staticfiles',
#     # 'debug_toolbar',
#     'chain.core',
#     'django_extensions',
#     'corsheaders',
#     # Uncomment the next line to enable the admin:
#     'django.contrib.admin',
#     # Uncomment the next line to enable admin documentation:
#     # 'django.contrib.admindocs',
# )
# SOUTH_TESTS_MIGRATE = False
# TEST_RUNNER = 'django.test.runner.DiscoverRunner'
# DEBUG_TOOLBAR_PATCH_SETTINGS = False
# INTERNAL_IPS = ['127.0.0.1', '18.85.58.156']
# LOGGING = {
#     'version': 1,
#     'disable_existing_loggers': False,
#     'filters': {
#         'require_debug_false': {
#             '()': 'django.utils.log.RequireDebugFalse'
#         }
#     },
#     'handlers': {
#         'mail_admins': {
#             'level': 'ERROR',
#             'filters': ['require_debug_false'],
#             'class': 'chain.helpers.logging.ThrottledAdminEmailHandler'
#         },
#         # null handler to throw away messages
#         'null': {
#             'level': 'DEBUG',
#             'class': 'logging.NullHandler',
#         },
#     },
#     'loggers': {
#         # Silence SuspiciousOperation.DisallowedHost exception ('Invalid
#         # HTTP_HOST' header messages). Set the handler to 'null' so we don't
#         # get those annoying emails.
#         'django.security.DisallowedHost': {
#             'handlers': ['null'],
#             'propagate': False,
#         },
#         'django.request': {
#             'handlers': ['mail_admins'],
#             'level': 'ERROR',
#             'propagate': True,
#         },
#     }
# }


# Path: chain/core/api.py
import logging
import json
import zmq
import re
from django.conf.urls import patterns, url
from django.db import models
from django.http import HttpResponse
from django.core.urlresolvers import reverse
from django.views.decorators.csrf import csrf_exempt
from django.db import IntegrityError
from datetime import datetime
from jinja2 import Environment, PackageLoader
from urlparse import urlparse, urlunparse, parse_qs
from urllib import urlencode
from chain.core.models import GeoLocation
from chain.settings import WEBSOCKET_PATH, WEBSOCKET_HOST, \
    ZMQ_PASSTHROUGH_URL_PULL
from pytz import AmbiguousTimeError
from django.contrib.contenttypes.models import ContentType
from django.utils import six
from django.utils.encoding import smart_text
from django.utils.cache import patch_vary_headers

                new_obj_data, stub_field_name, self._data[stub_field_name])
        if self.model_has_field('geo_location') and \
                'geoLocation' in self._data:
            dataloc = self._data['geoLocation']
            loc = GeoLocation(elevation=dataloc.get('elevation', None),
                              latitude=dataloc['latitude'],
                              longitude=dataloc['longitude'])
            loc.save()
            new_obj_data['geo_location'] = loc
        # the query string may contain more object data, for instance if
        # we're posting to a child collection resource

        new_obj_data.update(self._filters)
        self._obj = self.model(**new_obj_data)
        self._state = 'object'
        return self._obj

    def update(self, data):
        '''Updates this resource given the data, which is expected to be a dict
        in the format given by this resource's schema'''
        for k, v in data.iteritems():
            if k in self.model_fields:
                sanitized_value = type(self).sanitize_field_value(k, v)
                setattr(self._obj, k, sanitized_value)
            elif k in self.stub_fields:
                setattr(self._obj, k,
                        self.stub_object_finding(self._obj, k, v))
            elif k == 'geoLocation':
                loc = self._obj.geo_location
                if loc is None:

                    loc = GeoLocation(elevation=v.get('elevation', None),