/
models.py
1508 lines (1235 loc) · 53.2 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import division
import hashlib
import re
from copy import copy
from django.conf import settings
from django.contrib.gis.geos import Point, MultiPolygon
from django.core.mail import send_mail
from django.core.exceptions import (ValidationError, MultipleObjectsReturned,
ObjectDoesNotExist)
from django.core import validators
from django.http import Http404
from django.contrib.gis.db import models
from django.contrib.gis.measure import D
from django.db import IntegrityError, transaction
from django.db.models.signals import post_save, post_delete
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import (UserManager, AbstractBaseUser,
PermissionsMixin)
from django.template.loader import get_template
from treemap.species.codes import ITREE_REGIONS, get_itree_code
from treemap.audit import Auditable, Role, Dictable, Audit, PendingAuditable
# Import this even though it's not referenced, so Django can find it
from treemap.audit import UserTrackable, FieldPermission # NOQA
from treemap.util import leaf_models_of_class, to_object_name
from treemap.decorators import classproperty
from treemap.images import save_uploaded_image
from treemap.units import Convertible
from treemap.udf import UDFModel
from treemap.instance import Instance
from treemap.lib.object_caches import invalidate_adjuncts
def _action_format_string_for_location(action):
"""A helper that allows multiple auditable models to return the
same action format string for a field value that should be displayed
as a location"""
lang = {
Audit.Type.Insert: _('set the location'),
Audit.Type.Update: _('updated the location'),
Audit.Type.Delete: _('deleted the location'),
Audit.Type.PendingApprove: _('approved an edit of the location'),
Audit.Type.PendingReject: _('rejected an edit of the location')
}
return lang[action]
def _action_format_string_for_readonly(action, readonly):
"""A helper that allows multiple auditable models to return the
the state of a readonly boolean"""
if readonly:
value = _("read only")
else:
value = _("editable")
lang = {
Audit.Type.Insert: _('made the tree %(value)s'),
Audit.Type.Update: _('made the tree %(value)s'),
Audit.Type.Delete: _('made the tree %(value)s'),
Audit.Type.PendingApprove: _('approved making the tree %(value)s'),
Audit.Type.PendingReject: _('approved making the tree %(value)s')
}
return lang[action] % {'value': value}
class StaticPage(models.Model):
instance = models.ForeignKey(Instance)
name = models.CharField(max_length=100)
content = models.TextField()
DEFAULT_CONTENT = {
'resources': 'treemap/partials/Resources.html',
'about': 'treemap/partials/About.html',
'faq': 'treemap/partials/FAQ.html'
}
@staticmethod
def built_in_names():
return ['Resources', 'FAQ', 'About', 'Partners']
@staticmethod
def get_or_new(instance, page_name, only_create_built_ins=True):
'''
If static page exists, return it;
otherwise construct one (without saving).
Make sure the returned object's name is cased correctly
if it matches an existing object name or built-in name.
'''
try:
static_page = StaticPage.objects.get(name__iexact=page_name,
instance=instance)
except StaticPage.DoesNotExist:
built_in_name = StaticPage._get_built_in_name(page_name)
if built_in_name:
page_name = built_in_name
elif only_create_built_ins:
raise Http404('Static page does not exist')
if page_name.lower() in StaticPage.DEFAULT_CONTENT:
template = get_template(
StaticPage.DEFAULT_CONTENT[page_name.lower()])
content = template.render()
else:
content = 'There is no content for this page yet.'
static_page = StaticPage(instance=instance, name=page_name,
content=content)
return static_page
@staticmethod
def _get_built_in_name(page_name):
page_name_lower = page_name.lower()
for name in StaticPage.built_in_names():
if page_name_lower == name.lower():
return name
return None
def __unicode__(self):
return self.name
class BenefitCurrencyConversion(Dictable, models.Model):
"""
These conversion factors are used to convert a unit of benefit
into a currency unit.
While there is currently a 1-to-1 relationship between a given
benefit currency conversion and an instance, this provides an
easy way to note that there is no conversion available (setting
the FK to None). It also provides a mechanism for naming different
conversions or working with geography in the future
"""
"""
Symbol to display ($,£, etc)
"""
currency_symbol = models.CharField(max_length=5)
# Energy conversions
electricity_kwh_to_currency = models.FloatField()
natural_gas_kbtu_to_currency = models.FloatField()
# Stormwater conversions
h20_gal_to_currency = models.FloatField()
# CO₂ conversions
co2_lb_to_currency = models.FloatField()
# Air quality conversions
o3_lb_to_currency = models.FloatField()
nox_lb_to_currency = models.FloatField()
pm10_lb_to_currency = models.FloatField()
sox_lb_to_currency = models.FloatField()
voc_lb_to_currency = models.FloatField()
def clean(self):
errors = {}
if len(self.currency_symbol) > 4:
errors['currency_symbol'] = _(
'Symbol is too long')
positive_fields = ['electricity_kwh_to_currency',
'natural_gas_kbtu_to_currency',
'h20_gal_to_currency',
'co2_lb_to_currency',
'o3_lb_to_currency',
'nox_lb_to_currency',
'pm10_lb_to_currency',
'sox_lb_to_currency',
'voc_lb_to_currency']
for field in positive_fields:
value = getattr(self, field)
try:
value = float(value or '')
if value < 0:
errors[field] = [_('Values must be not be negative')]
except ValueError:
pass
if errors:
raise ValidationError(errors)
def save(self, *args, **kwargs):
self.full_clean()
super(BenefitCurrencyConversion, self).save(*args, **kwargs)
def get_factor_conversions_config(self):
return {
'electricity': self.electricity_kwh_to_currency,
'natural_gas': self.natural_gas_kbtu_to_currency,
'hydro_interception': self.h20_gal_to_currency,
'co2_sequestered': self.co2_lb_to_currency,
'co2_avoided': self.co2_lb_to_currency,
'co2_storage': self.co2_lb_to_currency,
'aq_ozone_dep': self.o3_lb_to_currency,
'aq_nox_dep': self.nox_lb_to_currency,
'aq_nox_avoided': self.nox_lb_to_currency,
'aq_pm10_dep': self.pm10_lb_to_currency,
'aq_sox_dep': self.sox_lb_to_currency,
'aq_sox_avoided': self.sox_lb_to_currency,
'aq_voc_avoided': self.voc_lb_to_currency
# TODO It is unclear if the 'bvoc' factor uses the 'VOC' costs
# Leaving it alone for now, as it seems better to incorrectly have
# lower eco-benefit money saved than higher.
}
@classmethod
def get_default_for_instance(cls, instance):
"""
Returns a new BenefitCurrencyConversion for the instance's (first)
i-Tree region. The instance must have bounds for this to work.
"""
regions_covered = instance.itree_regions()
if len(regions_covered) == 0:
return None
region_code = regions_covered[0].code
return cls.get_default_for_region(region_code)
@classmethod
def get_default_for_region(cls, region_code):
"""
Returns a new BenefitCurrencyConversion for the given i-Tree region
"""
config = ITREE_REGIONS.get(region_code, {})\
.get('currency_conversion')
if config:
benefits_conversion = cls()
benefits_conversion.currency_symbol = '$'
for field, conversion in config.iteritems():
setattr(benefits_conversion, field, conversion)
return benefits_conversion
else:
return None
# This is copy and pasted with syntax mods from the source for `AbstractUser`
# from the django source code, which is suboptimal. This was done because you
# can't have your cake and eat it too: inheriting AbstractUser but modifying
# one of the core fields.
#
# This code caused failures in gunicorn but not django runserver or tests:
#
# # dynamically modify User.email to be unique, instead of
# # inheriting and overriding AbstractBaseUser and PermissionMixin
# email_field, __, __, __ = User._meta.get_field_by_name('email')
# email_field._unique = True
#
# TODO: Fix this abstraction, and/or prune out parts of this class that
# are not needed, and merge with the User class.
#
# see the following code sample for the original `AbstractUser` source
# https://raw.github.com/django/django/53c7d66869636a6cf2b8c03c4de01ddff16f9892/django/contrib/auth/models.py # NOQA
class AbstractUniqueEmailUser(AbstractBaseUser, PermissionsMixin):
"""
An abstract base class implementing a fully featured User model with
admin-compliant permissions.
Username, password and email are required. Other fields are optional.
"""
username = models.CharField(
_('username'), max_length=30, unique=True,
help_text=_(
'Required. 30 characters or fewer. Letters, numbers and '
'@/./+/-/_ characters'),
validators=[
validators.RegexValidator(
re.compile('^[\w.@+-]+$'),
_('Enter a valid username.'), 'invalid')
])
email = models.EmailField(_('email address'), blank=True, unique=True)
is_staff = models.BooleanField(
_('staff status'), default=False,
help_text=_('Designates whether the user can log into this admin '
'site.'))
is_active = models.BooleanField(
_('active'), default=True,
help_text=_('Designates whether this user should be treated as '
'active. Unselect this instead of deleting accounts.'))
date_joined = models.DateTimeField(_('date joined'),
default=timezone.now)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
class Meta:
verbose_name = _('user')
verbose_name_plural = _('users')
abstract = True
def get_full_name(self):
"""
Returns the first_name plus the last_name, with a space in between.
"""
full_name = '%s %s' % (self.get_first_name(), self.get_last_name())
return full_name.strip()
def get_short_name(self):
"Returns the short name for the user."
return self.get_first_name()
def email_user(self, subject, message, from_email=None, **kwargs):
"""
Sends an email to this User.
"""
send_mail(subject, message, from_email, [self.email], **kwargs)
class User(AbstractUniqueEmailUser, Auditable):
_system_user = None
photo = models.ImageField(upload_to='users', null=True, blank=True)
thumbnail = models.ImageField(upload_to='users', null=True, blank=True)
first_name = models.CharField(
_('first name'), max_length=30, default='', blank=True)
last_name = models.CharField(
_('last name'), max_length=30, default='', blank=True)
organization = models.CharField(max_length=255, default='', blank=True)
make_info_public = models.BooleanField(default=False)
allow_email_contact = models.BooleanField(default=False)
def __init__(self, *args, **kwargs):
super(User, self).__init__(*args, **kwargs)
self.populate_previous_state()
@classmethod
def system_user(clazz):
if not User._system_user:
try:
User._system_user = User.objects.get(
pk=settings.SYSTEM_USER_ID)
except User.DoesNotExist:
raise User.DoesNotExist('System user does not exist. You may '
'want to run '
'`manage.py create_system_user`')
return User._system_user
@property
def created(self):
try:
return Audit.objects.filter(instance=None,
model='User',
model_id=self.pk)\
.order_by('created')[0]\
.created
except IndexError:
# A user has no audit records?
return None
@property
def email_hash(self):
return hashlib.sha512(self.email).hexdigest()
def dict(self):
return {'id': self.pk,
'username': self.username}
def get_first_name(self):
return self.first_name if self.make_info_public else ''
def get_last_name(self):
return self.last_name if self.make_info_public else ''
def get_organization(self):
return self.organization if self.make_info_public else ''
def get_instance_user(self, instance):
try:
return InstanceUser.objects.get(user=self, instance=instance)
except InstanceUser.DoesNotExist:
return None
except MultipleObjectsReturned:
msg = ("User '%s' found more than once in instance '%s'"
% (self, instance))
raise IntegrityError(msg)
def get_effective_instance_user(self, instance):
if instance is None:
return None
instance_user = self.get_instance_user(instance)
# If the user has no instance user yet, we need to provide a default so
# that template filters can determine whether that user can perform an
# action that will make them into an instance user
if (instance_user is None and
instance.feature_enabled('auto_add_instance_user')):
return InstanceUser(user=self,
instance=instance,
role=instance.default_role)
else:
return instance_user
def get_role(self, instance):
iuser = self.get_instance_user(instance)
role = iuser.role if iuser else instance.default_role
return role
def get_reputation(self, instance):
iuser = self.get_instance_user(instance)
reputation = iuser.reputation if iuser else 0
return reputation
def clean(self):
super(User, self).clean()
if re.search('\\s', self.username):
raise ValidationError(_('Cannot have spaces in a username'))
def save_with_user(self, user, *args, **kwargs):
self.full_clean()
self.email = self.email.lower()
super(User, self).save_with_user(user, *args, **kwargs)
def save(self, *args, **kwargs):
system_user = User.system_user()
self.save_with_user(system_user, *args, **kwargs)
class Species(PendingAuditable, models.Model):
"""
http://plants.usda.gov/adv_search.html
"""
DEFAULT_MAX_DIAMETER = 200
DEFAULT_MAX_HEIGHT = 800
# Base required info
instance = models.ForeignKey(Instance)
# ``otm_code`` is the key used to link this instance
# species row to a cannonical species. An otm_code
# is usually the USDA code, but this is not guaranteed.
otm_code = models.CharField(max_length=255)
common_name = models.CharField(max_length=255, verbose_name='Common Name')
genus = models.CharField(max_length=255, verbose_name='Genus')
species = models.CharField(max_length=255, blank=True,
verbose_name='Species')
cultivar = models.CharField(max_length=255, blank=True,
verbose_name='Cultivar')
other_part_of_name = models.CharField(max_length=255, blank=True,
verbose_name='Other Part of Name')
# From original OTM (some renamed) ###
is_native = models.NullBooleanField(verbose_name='Native to Region')
flowering_period = models.CharField(max_length=255, blank=True,
verbose_name='Flowering Period')
fruit_or_nut_period = models.CharField(max_length=255, blank=True,
verbose_name='Fruit or Nut Period')
fall_conspicuous = models.NullBooleanField(verbose_name='Fall Conspicuous')
flower_conspicuous = models.NullBooleanField(
verbose_name='Flower Conspicuous')
palatable_human = models.NullBooleanField(verbose_name='Edible')
has_wildlife_value = models.NullBooleanField(
verbose_name='Has Wildlife Value')
fact_sheet_url = models.URLField(max_length=255, blank=True,
verbose_name='Fact Sheet URL')
plant_guide_url = models.URLField(max_length=255, blank=True,
verbose_name='Plant Guide URL')
# Used for validation
max_diameter = models.IntegerField(default=DEFAULT_MAX_DIAMETER,
verbose_name='Max Diameter')
max_height = models.IntegerField(default=DEFAULT_MAX_HEIGHT,
verbose_name='Max Height')
# Included for the sake of cache busting
updated_at = models.DateTimeField( # TODO: remove null=True
null=True, auto_now=True, editable=False, db_index=True)
objects = models.GeoManager()
def __init__(self, *args, **kwargs):
super(Species, self).__init__(*args, **kwargs)
self.populate_previous_state()
@property
def display_name(self):
return "%s [%s]" % (self.common_name, self.scientific_name)
@classmethod
def get_scientific_name(clz, genus, species, cultivar, other_part_of_name):
name = genus
if species:
name += " " + species
if other_part_of_name:
name += " " + other_part_of_name
if cultivar:
name += " '%s'" % cultivar
return name
@property
def scientific_name(self):
return Species.get_scientific_name(
self.genus, self.species, self.cultivar, self.other_part_of_name)
def dict(self):
props = self.as_dict()
props['scientific_name'] = self.scientific_name
return props
@classmethod
def get_by_code(cls, instance, otm_code, region_code):
"""
Get a Species with the specified otm_code in the specified instance. If
a matching Species does not exists, attempt to find and
ITreeCodeOverride that has a itree_code matching the specified otm_code
in the specified region.
"""
species = Species.objects.filter(instance=instance, otm_code=otm_code)
if species.exists():
return species[0]
else:
species_ids = \
Species.objects.filter(instance=instance).values('pk')
region = ITreeRegion.objects.get(code=region_code)
itree_code = get_itree_code(region_code, otm_code)
overrides = ITreeCodeOverride.objects.filter(
itree_code=itree_code,
region=region,
instance_species_id__in=species_ids)
if overrides.exists():
return overrides[0].instance_species
else:
return None
def get_itree_code(self, region_code=None):
if not region_code:
regions = self.instance.itree_regions()
if len(regions) == 1:
region_code = regions[0].code
else:
return None
override = ITreeCodeOverride.objects.filter(
instance_species=self,
region=ITreeRegion.objects.get(code=region_code),
)
if override.exists():
return override[0].itree_code
else:
return get_itree_code(region_code, self.otm_code)
def __unicode__(self):
return self.display_name
class Meta:
verbose_name = "Species"
verbose_name_plural = "Species"
unique_together = ('instance', 'common_name', 'genus', 'species',
'cultivar', 'other_part_of_name',)
class InstanceUser(Auditable, models.Model):
instance = models.ForeignKey(Instance)
user = models.ForeignKey(User)
role = models.ForeignKey(Role)
reputation = models.IntegerField(default=0)
admin = models.BooleanField(default=False)
last_seen = models.DateField(null=True, blank=True)
def __init__(self, *args, **kwargs):
super(InstanceUser, self).__init__(*args, **kwargs)
self._do_not_track |= self.do_not_track
self.populate_previous_state()
class Meta:
unique_together = ('instance', 'user',)
def save_with_user(self, user, *args, **kwargs):
self.full_clean()
super(InstanceUser, self).save_with_user(user, *args, **kwargs)
def save(self, *args, **kwargs):
system_user = User.system_user()
self.save_with_user(system_user, *args, **kwargs)
@classproperty
def do_not_track(cls):
return Auditable.do_not_track | {'last_seen'}
def __unicode__(self):
# protect against not being logged in
username = ''
if getattr(self, 'user', None) is not None:
username = self.user.get_username() + '/'
if not username and not self.instance.name:
return ''
return '%s %s' % (username, self.instance.name)
post_save.connect(invalidate_adjuncts, sender=InstanceUser)
post_delete.connect(invalidate_adjuncts, sender=InstanceUser)
# UDFModel overrides implementations of methods in
# authorizable and auditable, thus needs to be inherited
# before PendingAuditable.
class MapFeature(Convertible, UDFModel, PendingAuditable):
"Superclass for map feature subclasses like Plot, RainBarrel, etc."
instance = models.ForeignKey(Instance)
geom = models.PointField(srid=3857, db_column='the_geom_webmercator')
address_street = models.CharField(max_length=255, blank=True, null=True,
verbose_name=_("Address"))
address_city = models.CharField(max_length=255, blank=True, null=True,
verbose_name=_("City"))
address_zip = models.CharField(max_length=30, blank=True, null=True,
verbose_name=_("Postal Code"))
readonly = models.BooleanField(default=False)
# Although this can be retrieved with a MAX() query on the audit
# table, we store a "cached" value here to keep filtering easy and
# efficient.
updated_at = models.DateTimeField(default=timezone.now,
verbose_name=_("Last Updated"))
updated_by = models.ForeignKey(User, null=True, blank=True,
verbose_name=_("Last Updated By"))
objects = models.GeoManager()
# subclass responsibilities
area_field_name = None
is_editable = None
# When querying MapFeatures (as opposed to querying a subclass like Plot),
# we get a heterogenous collection (some Plots, some RainBarrels, etc.).
# The feature_type attribute tells us the type of each object.
feature_type = models.CharField(max_length=255)
hide_at_zoom = models.IntegerField(
null=True, blank=True, default=None, db_index=True)
users_can_delete_own_creations = True
@classproperty
def always_writable(cls):
# `hide_at_zoom` and `geom` never need to be checked.
# If we ever implement the ability to lock down a model instance,
# `readonly` should be removed from this list.
return PendingAuditable.always_writable | {
'hide_at_zoom', 'geom', 'readonly'}
def __init__(self, *args, **kwargs):
super(MapFeature, self).__init__(*args, **kwargs)
if self.feature_type == '':
self.feature_type = self.map_feature_type
self._do_not_track |= self.do_not_track
self.populate_previous_state()
@classproperty
def do_not_track(cls):
return PendingAuditable.do_not_track | UDFModel.do_not_track | {
'feature_type', 'mapfeature_ptr', 'hide_at_zoom'}
@property
def _is_generic(self):
return self.__class__.__name__ == 'MapFeature'
@classproperty
def geom_field_name(cls):
return "%s.geom" % to_object_name(cls.map_feature_type)
@property
def latlon(self):
latlon = Point(self.geom.x, self.geom.y, srid=3857)
latlon.transform(4326)
return latlon
@property
def is_plot(self):
return getattr(self, 'feature_type', None) == 'Plot'
def update_updated_fields(self, user):
"""Changing a child object of a map feature (tree, photo,
etc.) demands that we update the updated_at field on the
parent map_feature, however there is likely code throughout
the application that saves updates to a child object without
calling save on the parent MapFeature. This method intended to
by called in the save method of those child objects."""
self.updated_at = timezone.now()
self.updated_by = user
MapFeature.objects.filter(pk=self.pk).update(
updated_at=self.updated_at, updated_by=user)
def save_with_user(self, user, *args, **kwargs):
self.full_clean_with_user(user)
if self._is_generic:
raise Exception(
'Never save a MapFeature -- only save a MapFeature subclass')
self.updated_at = timezone.now()
self.updated_by = user
super(MapFeature, self).save_with_user(user, *args, **kwargs)
def clean(self):
super(MapFeature, self).clean()
if self.geom is None:
raise ValidationError({
"geom": [_("Feature location is not specified")]})
if not self.instance.bounds.geom.contains(self.geom):
raise ValidationError({
"geom": [
_(
"%(model)s must be created inside the map boundaries")
% {'model': self.terminology(self.instance)['plural']}]
})
def delete_with_user(self, user, *args, **kwargs):
self.instance.update_revs('geo_rev', 'eco_rev', 'universal_rev')
super(MapFeature, self).delete_with_user(user, *args, **kwargs)
def photos(self):
return self.mapfeaturephoto_set.order_by('-created_at')
def add_photo(self, image, user):
photo = MapFeaturePhoto(map_feature=self, instance=self.instance)
photo.set_image(image)
photo.save_with_user(user)
return photo
@classproperty
def map_feature_type(cls):
# Map feature type defaults to subclass name (e.g. 'Plot').
# Subclasses can override it if they want something different.
# (But note that the value gets stored in the database, so should not
# be changed for a subclass once objects have been saved.)
return cls.__name__
@classmethod
def subclass_dict(cls):
return {C.map_feature_type: C
for C in leaf_models_of_class(MapFeature)}
@classmethod
def has_subclass(cls, type):
return type in cls.subclass_dict()
@classmethod
def get_subclass(cls, type):
try:
return cls.subclass_dict()[type]
except KeyError as e:
raise ValidationError('Map feature type %s not found' % e)
@classmethod
def get_config(cls, instance):
"""
Get configuration properties for this map feature type on the
specified instance.
Note that the map feature config is assumed to be flat.
"""
config = copy(getattr(cls, 'default_config', {}))
overrides = instance.map_feature_config.get(cls.__name__, {})
config.update(overrides)
return config
@classmethod
def set_config_property(cls, instance, key, value, save=True):
"""
Set a configuration property for this map feature type on the
specified instance.
"""
config = instance.map_feature_config
class_name = cls.__name__
if class_name not in config:
config[class_name] = {}
config[class_name][key] = value
instance.map_feature_config = config
if save:
instance.save()
@property
def address_full(self):
components = []
if self.address_street:
components.append(self.address_street)
if self.address_city:
components.append(self.address_city)
if self.address_zip:
components.append(self.address_zip)
return ', '.join(components)
@classmethod
def action_format_string_for_audit(clz, audit):
if audit.field in set(['geom', 'readonly']):
if audit.field == 'geom':
return _action_format_string_for_location(audit.action)
else: # field == 'readonly'
return _action_format_string_for_readonly(
audit.action,
audit.clean_current_value)
else:
return super(MapFeature, clz).action_format_string_for_audit(audit)
@property
def hash(self):
string_to_hash = super(MapFeature, self).hash
if self.is_plot:
# The hash for a plot includes the hash for its trees
tree_hashes = [t.hash for t in self.plot.tree_set.all()]
string_to_hash += "," + ",".join(tree_hashes)
# Need to include nearby features in the hash, as they are in the
# detail sidebar & popup.
for feature in self.nearby_map_features():
string_to_hash += "," + str(feature.pk)
return hashlib.md5(string_to_hash).hexdigest()
def title(self):
# Cast allows the map feature subclass to handle generating
# the display name
feature = self.cast_to_subtype()
if feature.is_plot:
tree = feature.current_tree()
if tree:
if tree.species:
title = tree.species.common_name
else:
title = _("Missing Species")
else:
title = _("Empty Planting Site")
else:
title = feature.display_name(self.instance)
return title
def contained_plots(self):
if self.area_field_name is not None:
plots = Plot.objects \
.filter(instance=self.instance) \
.filter(geom__within=getattr(self, self.area_field_name)) \
.prefetch_related('tree_set', 'tree_set__species')
def key_sort(plot):
tree = plot.current_tree()
if tree is None:
return (0, None)
if tree.species is None:
return (1, None)
return (2, tree.species.common_name)
return sorted(plots, key=key_sort)
return None
def cast_to_subtype(self):
"""
Return the concrete subclass instance. For example, if self is
a MapFeature with subtype Plot, return self.plot
"""
if type(self) is not MapFeature:
# This shouldn't really ever happen, but there's no point trying to
# cast a subclass to itself
return self
ft = self.feature_type
if hasattr(self, ft.lower()):
return getattr(self, ft.lower())
else:
return getattr(self.polygonalmapfeature, ft.lower())
def safe_get_current_tree(self):
if hasattr(self, 'current_tree'):
return self.current_tree()
else:
return None
def nearby_map_features(self, distance_in_meters=None):
if distance_in_meters is None:
distance_in_meters = settings.NEARBY_TREE_DISTANCE
distance_filter = MapFeature.objects.filter(
geom__distance_lte=(self.geom, D(m=distance_in_meters)))
return distance_filter\
.filter(instance=self.instance)\
.exclude(pk=self.pk)
def __unicode__(self):
geom = getattr(self, 'geom', None)
x = geom and geom.x or '?'
y = geom and geom.y or '?'
address = getattr(self, 'address_street', "Address Unknown")
feature_type = getattr(self, 'feature_type', "Type Unknown")
if not feature_type and not address and not x and not y:
return ''
text = "%s (%s, %s) %s" % (feature_type, x, y, address)
return text
@classproperty
def _terminology(cls):
return {'singular': cls.__name__}
@classproperty
def benefits(cls):
from treemap.ecobenefits import CountOnlyBenefitCalculator
return CountOnlyBenefitCalculator(cls)
@property
def itree_region(self):
regions = self.instance.itree_regions(geometry__contains=self.geom)
if regions:
return regions[0]
else:
return ITreeRegionInMemory(None)
class ValidationMixin(object):
def validate_positive_nullable_float_field(
self, field_name, max_value=None, zero_ok=False):
if getattr(self, field_name) is not None:
pretty_field_name = field_name.replace('_', ' ')
def error(message):
return ValidationError({field_name: [
message % {'field_name': pretty_field_name}]})
try:
# The value could be a string at this point so we
# cast to make sure we are comparing two numeric values
new_value = float(getattr(self, field_name))
except ValueError:
raise error(_('The %(field_name)s must be a decimal number'))
if zero_ok:
if new_value < 0:
raise error(_(
'The %(field_name)s must be zero or greater'))
else:
if new_value <= 0:
raise error(_(
'The %(field_name)s must be greater than zero'))
if max_value is not None:
if new_value > max_value:
raise error(_('The %(field_name)s is too large'))
# TODO:
# Exclusion Zones
# Proximity validation
# authorizable and auditable, thus needs to be inherited first
class Plot(MapFeature, ValidationMixin):
width = models.FloatField(null=True, blank=True,
verbose_name=_("Planting Site Width"))
length = models.FloatField(null=True, blank=True,
verbose_name=_("Planting Site Length"))
owner_orig_id = models.CharField(max_length=255, null=True, blank=True,
verbose_name=_("Custom ID"))
objects = models.GeoManager()
is_editable = True
_terminology = {'singular': _('Planting Site'),
'plural': _('Planting Sites')}
search_settings = {
'owner_orig_id': {'search_type': 'IS'}
}
udf_settings = {
'Stewardship': {
'iscollection': True,
'range_field_key': 'Date',
'action_field_key': 'Action',
'action_verb': _('that have been'),
'defaults': [
{'name': 'Action',
'choices': ['Enlarged',
'Changed to Include a Guard',
'Changed to Remove a Guard',
'Filled with Herbaceous Plantings'],
'type': 'choice'},
{'type': 'date',
'name': 'Date'}],