# Path: observations/models.py
# class Measurement(models.Model):
#     """A measurement at a point in space/time."""
#     created_timestamp = models.DateTimeField(auto_now_add=True)
#     reference_timestamp = models.DateTimeField(
#         verbose_name="Observation date/time",
#         null=True, blank=True,
#         help_text="When was the sample/measurement taken?"
#     )
#     location = models.PointField()
#     location_reference = models.CharField(
#         verbose_name="Location name",
#         max_length=200, null=True, blank=True,
#         help_text="A friendly/reference name for this site")
#     observations = hstore.DictionaryField(null=True, blank=True)
#     parameters = models.ManyToManyField(Test, through='TestValue')
# 
#     observer = models.EmailField(
#         verbose_name="Email address",
#         null=True, blank=True,
#         help_text="Your email address - don't worry, we won't share it with "
#         "anyone!")
#     source = models.ForeignKey(
#         'supplements.DataSource', null=True, blank=True,
#         help_text="If this data came from an historic source, what is it?"
#     )
# 
#     objects = models.GeoManager()
#     observations_manager = hstore.HStoreManager()
# 
#     class Meta:
#         ordering = ('-reference_timestamp',)
# 
#     def __unicode__(self):
#         return u"Observation at {0}".format(self.created_timestamp)
# 
#     def _observed_as_string(self):
#         """Returns a list of parameter names that were obeserved."""
#         return ", ".join([
#             test.parameter.name for test in self.parameters.all()])
# 
#     def get_timestamp(self):
#         """Return the most useful timestamp."""
#         return self.reference_timestamp or self.created_timestamp
# 
#     def get_location(self):
#         """Return a nice location name."""
#         return self.location_reference or "{0:0.2f}, {1:0.2f}".format(
#             self.location.x, self.location.y)


import json
from rest_framework import serializers
from rest_framework_gis import serializers as geo_serializers
from observations.models import Measurement




class DictionaryField(serializers.Field):
    def to_native(self, obj):
        # HStore doesn't allow nested dictionaries, so we parse the escaped
        # string values to JSON (assuming that's what they in fact are.
        if obj:
            ret = {
                k: json.loads(v) for k, v in obj.items()
            }
        else:
            ret = {}
        return ret


class CompactMeasurementSerializer(geo_serializers.GeoFeatureModelSerializer):
    class Meta:
        model = Measurement
        geo_field = 'location'
        fields = ('id', 'location')


class MeasurementSerializer(geo_serializers.GeoFeatureModelSerializer):
    observations = DictionaryField()

    class Meta:

# Path: observations/models.py
# class Measurement(models.Model):
#     """A measurement at a point in space/time."""
#     created_timestamp = models.DateTimeField(auto_now_add=True)
#     reference_timestamp = models.DateTimeField(
#         verbose_name="Observation date/time",
#         null=True, blank=True,
#         help_text="When was the sample/measurement taken?"
#     )
#     location = models.PointField()
#     location_reference = models.CharField(
#         verbose_name="Location name",
#         max_length=200, null=True, blank=True,
#         help_text="A friendly/reference name for this site")
#     observations = hstore.DictionaryField(null=True, blank=True)
#     parameters = models.ManyToManyField(Test, through='TestValue')
# 
#     observer = models.EmailField(
#         verbose_name="Email address",
#         null=True, blank=True,
#         help_text="Your email address - don't worry, we won't share it with "
#         "anyone!")
#     source = models.ForeignKey(
#         'supplements.DataSource', null=True, blank=True,
#         help_text="If this data came from an historic source, what is it?"
#     )
# 
#     objects = models.GeoManager()
#     observations_manager = hstore.HStoreManager()
# 
#     class Meta:
#         ordering = ('-reference_timestamp',)
# 
#     def __unicode__(self):
#         return u"Observation at {0}".format(self.created_timestamp)
# 
#     def _observed_as_string(self):
#         """Returns a list of parameter names that were obeserved."""
#         return ", ".join([
#             test.parameter.name for test in self.parameters.all()])
# 
#     def get_timestamp(self):
#         """Return the most useful timestamp."""
#         return self.reference_timestamp or self.created_timestamp
# 
#     def get_location(self):
#         """Return a nice location name."""
#         return self.location_reference or "{0:0.2f}, {1:0.2f}".format(
#             self.location.x, self.location.y)


# Path: observations/api/serializers.py
import json
from rest_framework import serializers
from rest_framework_gis import serializers as geo_serializers
from observations.models import Measurement





class DictionaryField(serializers.Field):
    def to_native(self, obj):
        # HStore doesn't allow nested dictionaries, so we parse the escaped
        # string values to JSON (assuming that's what they in fact are.
        if obj:
            ret = {
                k: json.loads(v) for k, v in obj.items()
            }
        else:
            ret = {}
        return ret


class CompactMeasurementSerializer(geo_serializers.GeoFeatureModelSerializer):
    class Meta:
        model = Measurement
        geo_field = 'location'
        fields = ('id', 'location')


class MeasurementSerializer(geo_serializers.GeoFeatureModelSerializer):
    observations = DictionaryField()

    class Meta:

        model = Measurement