~doctormo/+junk/ashmont-valley-website

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#
# Copyright 2013 Martin Owens
#

from django.db.models import *
from django.utils.translation import ugettext_lazy as _
from geoposition.fields import GeopositionField

from community.fields import *

ADDRESS_TYPES = (
  ('-', 'Unknown'),
  ('R', 'Residence'),
  ('B', 'Business'),
  ('C', 'Community Space'),
  ('G', 'Governmental'),
)

class Country(Model):
    name     = CharField(max_length=128)
    code     = CharField(max_length=2)
    
    def __unicode__(self):
        return self.name

class State(Model):
    name     = CharField(max_length=128)
    code     = CharField(max_length=2)
    country  = ForeignKey(Country)
    
    def __unicode__(self):
        return self.name

class City(Model):
    name     = CharField(max_length=128)
    state    = ForeignKey(State)

    def __unicode__(self):
        return self.name

class Road(Model):
    name     = CharField(max_length=128)
    alias    = CharField(max_length=128, **blank)
    city     = ForeignKey(City)

    def __unicode__(self):
        return self.name

class Building(Model):
    """Provides a way to specify the co-ords for a plot/address"""
    number   = CharField(max_length=6)
    name     = CharField(max_length=64)

    road     = ForeignKey(Road)    
    postcode = CharField(max_length=22)
    location = GeopositionField(**blank)

    plot     = TextField(_('Building Plot'), **blank)
    foot     = TextField(_('Building Footprint'), **blank)

    class Meta:
        unique_together = (('number', 'road'),)

    def coords(self, c_latt, c_long):
        if self.location:
            return (
                coord(self.location.latitude, *c_latt),
                coord(self.location.longitude, *c_long),
            )
        return None

    def __unicode__(self, extra=''):
        return "%s %s%s\n%s\n%s" % (self.number, self.road, extra, self.road.city, self.road.city.state)

from decimal import Decimal

def coord(point, start, size):
    if isinstance(start, tuple):
        start = to_degrees(*start)
    return (abs(Decimal(start)) - abs(point)) * 60 * 60 * Decimal(size)

def to_degrees(degrees, minutes, seconds):
    degrees = Decimal(degrees)
    degrees += Decimal(minutes) / 60
    degrees += Decimal(seconds) / 3600
    return degrees

def to_minutes(degrees):
    """Convert decimal latt/long to seconds/minutes"""
    minutes = abs(degrees) % 1 * 60
    seconds = minutes % 1 * 60
    return (int(degrees), int(minutes), seconds)


class Address(Model):
    """An address is some relatable information plus a building"""
    building = ForeignKey(Building)
    apt      = CharField(max_length=8, **blank)

    kind      = CharField(_('Type'), max_length=1, choices=ADDRESS_TYPES, default='-')
    phone     = CharField(_('Phone'), max_length=32, **blank)

    importid  = CharField(_('Whitepages ID'), max_length=128, **blank)

    class Meta:
        unique_together = (('apt', 'building'),)

    def __unicode__(self):
        if self.apt:
            return self.building.__unicode__(', (Apt %s)' % self.apt)
        return unicode(self.building)

 
RES_STATUS = (
  ('-', 'No Contact'),
  ('G', 'Greeted'),
  ('I', 'Interested'),
  ('D', 'Duplicate'),
  ('M', 'Member'),
  ('!', 'Moved Away'),
  ('X', 'Passed Away'),
)

class Resident(Model):
    """A bunch of information from whitepages about people who live in places"""
    address   = ForeignKey(Address, related_name='residents')
    name      = CharField(max_length=128)
    vcard     = TextField(_('Raw vCard'))
    importid  = CharField(_('Whitepages ID'), max_length=128)

    created   = DateTimeField(default=now)
    edited    = DateTimeField(auto_now=True)

    status    = CharField(max_length=1, default='-', choices=RES_STATUS)
    duplicate = ForeignKey('Resident', related_name='duplicates', **blank)

    def __unicode__(self):
        if self.duplicate:
            return "%s (DUPLICATE)" % self.name
        return self.name

    def save(self, *args, **kwargs):
        self.edited = now()
        Model.save(self, *args, **kwargs)