Package madrona :: Package layers :: Module models
[hide private]

Source Code for Module madrona.layers.models

  1  from django.db import models 
  2  from django.conf import settings 
  3  from django.contrib.auth.models import User, Group 
  4  from madrona.features.managers import ShareableGeoManager 
  5  from madrona.features.models import Feature, FeatureForm 
  6  from madrona.common.utils import get_logger 
  7  from django.core.urlresolvers import reverse 
  8  from django.utils.encoding import DjangoUnicodeDecodeError 
  9  import os 
 10   
 11  logger = get_logger() 
12 13 -class UserUploadedKml(Feature):
14 """ 15 Abstract Model for storing uploaded restricted-access kml files 16 17 Owned by a single user, can be shared with any group(s) 18 that the owner is a member of (assuming group has 19 can_share_features permissions) 20 21 These are features and will show up in the MyShapes/SharedShapes panels 22 """ 23 kml_file = models.FileField(upload_to='upload/private-kml-layers/%Y/%m/%d', help_text=""" 24 KML or KMZ file. Can use NetworkLinks pointing to remote kml datasets or WMS servers. 25 """, blank=False, max_length=510) 26 description = models.TextField(default="", null=True, blank=True) 27 28 @property
29 - def basename(self):
30 """ 31 Name of the file itself without the path 32 """ 33 return os.path.basename(self.kml_file.path)
34 35 @property
36 - def kml(self):
37 return "<!-- no kml representation, use network link to kml-file url -->"
38 39 @classmethod
40 - def css(klass):
41 return """ li.%s > .icon { 42 background: url('%scommon/images/kml_document_icon.png') no-repeat 0 0 ! important; 43 } 44 div.%s > .goog-menuitem-content { 45 background: url('%scommon/images/kml_document_icon.png') no-repeat 0 0 ! important; 46 display: block !important; 47 left: -22px; 48 padding-left: 22px; 49 position: relative; 50 height: 16px; 51 }""" % (klass.model_uid(), settings.MEDIA_URL, klass.model_uid(), settings.MEDIA_URL)
52 53 @property
54 - def kml_style(self):
55 return """ 56 <Style id="%(model_uid)s-default"> 57 </Style> 58 """ % {'model_uid': self.model_uid()}
59 60 @property
61 - def kml_style_id(self):
62 return "%s-default" % self.model_uid()
63 64 @property
65 - def kml_full(self):
66 from django.utils.encoding import smart_unicode 67 f = self.kml_file.read() 68 try: 69 return smart_unicode(f) 70 except DjangoUnicodeDecodeError: 71 # probably a binary kmz 72 return f 73 except: 74 logger.warn("%s.kml_full is failing .. returning an empty kml doc" % self) 75 return """<kml xmlns="http://www.opengis.net/kml/2.2"><Document></Document></kml>"""
76
77 - class Meta:
78 abstract = True
79
80 -class PrivateKml(models.Model):
81 """ 82 For presenting restricted-access KML datasets that don't belong to a particular user 83 These can be either: 84 * multi-file kml trees on disk (ie superoverlays) 85 * single kml/kmz files 86 87 Note that this is NOT a Feature so it doesn't have any of the sharing API, wont show in myshapes, etc 88 89 Admin must upload data to server and place in settings.PRIVATE_KML_ROOT 90 91 VERY IMPORTANT SECURITY CONSIDERATIONS... 92 * PRIVATE_KML_ROOT should not be web accessible! 93 * Each PrivateKml should have it's own subdirectory in PRIVATE_KML_ROOT! 94 THIS IS IMPORTANT; Every file in and below the base kml's directory path is accessible 95 if the user has proper permissions on the base kml. 96 97 Sharing and permissions must be implemented one-off in the views using the 98 sharing_groups many-to-many field. 99 """ 100 priority = models.FloatField(help_text="Floating point. Higher number = appears higher up on the KML tree.", 101 default=0.0) 102 name = models.CharField(verbose_name="Name", max_length="255",unique=True) 103 sharing_groups = models.ManyToManyField(Group,blank=True,null=True, 104 verbose_name="Share layer with the following groups") 105 base_kml = models.FilePathField(path=settings.PRIVATE_KML_ROOT, match="\.km.$", 106 recursive=True, max_length=255, 107 help_text=""" 108 Path to KML file. 109 If a superoverlay tree, use relative paths. 110 The user making the request only needs permissions for the base kml. 111 IMPORTANT: Every file in and below the base kml's directory path is accessible 112 if the user has proper permissions on the base kml.""") 113
114 - def __unicode__(self):
115 return u'%s at %s' % (self.name,self.base_kml)
116
117 -class PublicLayerList(models.Model):
118 """Model used for storing uploaded kml files that list all public layers. 119 120 ====================== ============================================== 121 Attribute Description 122 ====================== ============================================== 123 ``active`` Whether this kml file represents the currently 124 displayed data layers. If set to true and 125 another ``PublicLayerList`` is active, that 126 old list will be deactivated. 127 128 ``kml_file`` Django `FileField <http://docs.djangoproject.com/en/dev/ref/models/fields/#filefield>`_ 129 that references the actual kml 130 131 ``creation_date`` When the layer was created. Is not changed on 132 updates. 133 ====================== ============================================== 134 """ 135 136 creation_date = models.DateTimeField(auto_now=True) 137 138 active = models.BooleanField(default=True, help_text=""" 139 Checking here indicates that this layer list should be the one used in 140 the application. Copies of other layer lists are retained in the 141 system so you can go back to an old version if necessary. 142 """) 143 144 kml_file = models.FileField(upload_to='layers/uploaded-kml/', help_text=""" 145 KML file that represents the public layers list. This file can use 146 NetworkLinks pointing to remote kml datasets or WMS servers. 147 For more information on how to create this kml file see the 148 documentation. 149 """, blank=False, max_length=510) 150
151 - def __unicode__(self):
152 return "PublicLayerList, created: %s" % (self.creation_date)
153
154 - def save(self, *args, **kwargs):
155 super(PublicLayerList, self).save(*args, **kwargs) 156 if self.active and PublicLayerList.objects.filter(active=True).count() > 1: 157 # Ensure that any previously active layer is deactivated 158 # There can be only one! 159 PublicLayerList.objects.filter(active=True).exclude(pk=self.pk).update(active=False)
160
161 -class PrivateLayerList(UserUploadedKml):
162 """ 163 Note: This is just a wrapper to avoid breaking 164 old code that relies on this class name 165 """
166 - class Meta:
167 abstract = True
168