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()
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
30 """
31 Name of the file itself without the path
32 """
33 return os.path.basename(self.kml_file.path)
34
35 @property
37 return "<!-- no kml representation, use network link to kml-file url -->"
38
39 @classmethod
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
55 return """
56 <Style id="%(model_uid)s-default">
57 </Style>
58 """ % {'model_uid': self.model_uid()}
59
60 @property
63
64 @property
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
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
79
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
116
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
153
154 - def save(self, *args, **kwargs):
160
162 """
163 Note: This is just a wrapper to avoid breaking
164 old code that relies on this class name
165 """
168