# Path: safecity/apps/signup/models.py
# class Resident(models.Model):
#     """
#     Individuals who want to receive alerts when activity happens near them.
#     
#     Privacy notes:
#     1) The text used to register the resident is intentionally not associated with them.
#     2) Created/updated times are not maintained so that any potentially sensitive information
#         in their messages can never be associated with them.
#     3) Residents are DELETED when a request is made to deregister them.
#     """
#     phone_number = models.CharField(
#         primary_key=True,
#         max_length=16,
#         help_text='Phone number this resident registered with in e164 format.')
#     
#     location = models.PointField(    
#         spatial_index=True,
#         help_text='Location extracted from request message.')
#     
#     objects = models.GeoManager()
#         
#     def __unicode__(self):
#         return self.phone_number


from django.contrib.gis.db import models
from django.contrib.gis.measure import D
from safecity.apps.signup.models import Resident
    
    TODO: support multi-part messages.
    """
    location = models.PointField(
        spatial_index=True,
        help_text='Location extracted from the report.')
        
    text = models.CharField(
        max_length=160,
        help_text='Body of the message.')
        
    sender = models.CharField(
        max_length=16,
        null=True,
        help_text='Phone number of the reporter in e164 format. None if anonymized.')
        
    received = models.DateTimeField(
        auto_now_add=True,
        help_text='Date and time this message was received. Approximate if anonymized.')

    objects = models.GeoManager()
    
    class Meta:
        ordering = ['-received']
        get_latest_by = 'received'
        
    def find_nearby_residents(self):
        """
        Get a list of residents near this report.
        """

# Path: safecity/apps/signup/models.py
# class Resident(models.Model):
#     """
#     Individuals who want to receive alerts when activity happens near them.
#     
#     Privacy notes:
#     1) The text used to register the resident is intentionally not associated with them.
#     2) Created/updated times are not maintained so that any potentially sensitive information
#         in their messages can never be associated with them.
#     3) Residents are DELETED when a request is made to deregister them.
#     """
#     phone_number = models.CharField(
#         primary_key=True,
#         max_length=16,
#         help_text='Phone number this resident registered with in e164 format.')
#     
#     location = models.PointField(    
#         spatial_index=True,
#         help_text='Location extracted from request message.')
#     
#     objects = models.GeoManager()
#         
#     def __unicode__(self):
#         return self.phone_number


# Path: safecity/apps/danger/models.py
from django.contrib.gis.db import models
from django.contrib.gis.measure import D
from safecity.apps.signup.models import Resident

    
    TODO: support multi-part messages.
    """
    location = models.PointField(
        spatial_index=True,
        help_text='Location extracted from the report.')
        
    text = models.CharField(
        max_length=160,
        help_text='Body of the message.')
        
    sender = models.CharField(
        max_length=16,
        null=True,
        help_text='Phone number of the reporter in e164 format. None if anonymized.')
        
    received = models.DateTimeField(
        auto_now_add=True,
        help_text='Date and time this message was received. Approximate if anonymized.')

    objects = models.GeoManager()
    
    class Meta:
        ordering = ['-received']
        get_latest_by = 'received'
        
    def find_nearby_residents(self):
        """
        Get a list of residents near this report.
        """

        return Resident.objects.filter(location__distance_lte=(self.location, D(km=1)))