This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
/
models.py
221 lines (178 loc) · 7.3 KB
/
models.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import json
from django.contrib.gis.db import models
from django.db.models import Count
from krprj.world.models import WorldBorder
from krprj.wikipedia.models import KircheWikipedia
class KircheChecks(models.Model):
"""
"""
osm = models.BooleanField("OpenStreetMap place",
default=False)
osm_name = models.BooleanField("OpenStreetMap place is named",
default=False)
osm_religion = models.BooleanField("OpenStreetMap has information "
"about religion", default=False)
osm_denomination = models.BooleanField("OpenStreetMap knows the "
"denomination", default=False)
osm_address_complete = models.BooleanField("Full address in "
"OpenStreetMap", default=False)
wikipedia = models.BooleanField("Wikipedia article", default=False)
wikipedia_infobox = models.BooleanField("Wikipedia article has infobox",
default=False)
last_update = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return "%d [%s] (%s/%s)" % (self.id, self.kircheunite.name or '',
len(self.achieved), len(self.available))
@property
def available(self):
"""This property returns a list of available checks."""
checks = []
for field in self._meta.fields:
if isinstance(field, models.BooleanField):
checks.append(field.name)
return checks
@property
def achieved(self):
"""Instead to available() this property returns the list of achieved
checks by the unite object."""
checks = []
for field in self._meta.fields:
if isinstance(field, models.BooleanField) \
and getattr(self, field.name):
checks.append(field.name)
return checks
@property
def percent_reached(self):
return float(len(self.achieved)) / float(len(self.available)) * 100
@property
def pretty(self):
checks = []
for check in self.available:
field = self._meta.get_field_by_name(check)[0]
checks.append({
'name': check,
'description': field.verbose_name,
'value': getattr(self, check)
})
return checks
def _run(self):
"""Don't run this method directly!
Use KircheUnite.update_checks() instead.
Run some quality checks over the osm and wikipedia objects.
"""
if self.kircheunite:
for osm in self.kircheunite.kircheosm_set.all():
self.osm = True
if osm.name:
self.osm_name = True
if osm.religion:
self.osm_religion = True
if osm.denomination:
self.osm_denomination = True
d = json.loads(osm.addional_fields)
if ('addr:housenumer' in d and
'addr:city' in d and
'addr:postcode' in d and
'addr:street' in d):
self.osm_address_complete = True
for wiki in self.kircheunite.kirchewikipedia_set.all():
self.wikipedia = True
if len(wiki.infobox) > 3:
self.wikipedia_infobox = True
self.save()
class KircheUniteManager(models.GeoManager):
def get_by_osm_or_create(self, osm):
"""Get a KircheUnit object for KircheOsm object. If there no
KircheUnite object than it will be create a new one and set name
and point in the unite object from the Osm object.
"""
unite_objs = self.filter(kircheosm=osm)
if unite_objs.count() == 0:
unite = self.model()
unite.name = osm.name
unite.point = osm.point
unite.save()
osm.unite = unite
osm.save()
# This is not perfect but it's working
return self.filter(kircheosm=osm)
return unite_objs
class KircheUnite(models.Model):
""" Model for joining osm and wikipedia objects.
ForeignKeys are from KircheOsm (kircheosm) and
KircheWikipedia (kirchewikipedia)
"""
# name field is only for convenience
name = models.TextField(blank=True, null=True, default=None)
religion = models.TextField(blank=True, null=True, default=None)
denomination = models.TextField(blank=True, null=True, default=None)
point = models.PointField(srid=4326, blank=True, null=True)
country = models.ForeignKey(WorldBorder, blank=True, null=True)
checks = models.OneToOneField(KircheChecks, related_name='kircheunite',
blank=True, null=True)
last_update = models.DateTimeField(auto_now=True)
created = models.DateTimeField(auto_now_add=True)
# manager
objects = KircheUniteManager()
def __unicode__(self):
return "%d [%s]" % (self.id, self.name or '')
def update(self):
"""Update basedata, wikipedia; country; checks"""
self.update_basedata()
self.update_wikipedia()
self.update_country()
self.update_checks()
def update_basedata(self):
"""If the unite doesn't have the basedata than it will try to get it
from OSM or maybe wikipedia.
"""
religion = self.kircheosm_set.values_list('religion') \
.annotate(count=Count("id")) \
.order_by('count')
if len(religion) > 0:
religion = religion[0][0]
denomination = self.kircheosm_set.values_list('denomination') \
.annotate(count=Count("id")) \
.order_by('count')
if len(denomination) > 0:
denomination = denomination[0][0]
if religion:
self.religion = religion
if denomination:
self.denomination = denomination
if religion or denomination:
self.save()
def update_wikipedia(self):
"""Try to find some wikipedia objects within a radius of 100m to the
point of the unite object.
"""
if self.point is None:
return
articles = KircheWikipedia.objects.filter(
point__distance_lte=(self.point, 100)
)
self.kirchewikipedia_set.add(*articles)
return articles
def update_country(self):
"""Take the point of the unite object and update the country into the
database and return the result.
"""
if self.point is None:
return None
try:
self.country = WorldBorder.objects.get(
mpoly__intersects=self.point
)
self.save()
return self.country
except WorldBorder.DoesNotExist:
return None
def update_checks(self):
"""Update/Create the check object and run the available checks"""
if self.checks is None:
self.checks = KircheChecks()
self.checks.save()
self.checks_id = self.checks.id
self.save()
self.checks._run()