summaryrefslogtreecommitdiff
path: root/chimere/main/models.py
blob: 710623cb8541c6207285d2349836073ebcbba32d (plain)
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2008-2010  Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# See the file COPYING for details.

"""
Models description
"""
from django.utils.translation import ugettext_lazy as _

from django.contrib.gis.db import models
from django.contrib import admin

from chimere import settings
from chimere.main.widgets import PointField, RouteField, \
                                 ManyToManyField_NoSyncdb

class News(models.Model):
    """News of the site
    """
    title = models.CharField(_("Name"), max_length=150)
    available = models.BooleanField(_("Available"))
    date = models.DateField(_("Date"), auto_now_add=True)
    content = models.TextField()
    def __unicode__(self):
        ordering = ["-date"]
        return self.title
    class Meta:
        verbose_name = _("News")

class ColorTheme(models.Model):
    """Color theme
    """
    name = models.CharField(_("Name"), max_length=150)
    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _("Color theme")

class Color(models.Model):
    """Color
    """
    code = models.CharField(_("Code"), max_length=6)
    order = models.IntegerField(_("Order"))
    color_theme = models.ForeignKey(ColorTheme, verbose_name=_("Color theme"))
    def __unicode__(self):
        return self.code
    class Meta:
        ordering = ["order"]
        verbose_name = _("Color")

class Category(models.Model):
    """Category of Point Of Interest (POI)
    """
    name = models.CharField(_("Name"), max_length=150)
    available = models.BooleanField(_("Available"))
    order = models.IntegerField(_("Order"))
    description = models.TextField(blank=True, null=True)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ["order"]
        verbose_name = _("Category")

class Icon(models.Model):
    '''Icon
    '''
    name = models.CharField(_("Name"), max_length=150)
    image = models.ImageField(_("Image"), upload_to='icons',
                              height_field='height', width_field='width')
    def __unicode__(self):
        return self.name
    class Meta:
        verbose_name = _("Icon")

class SubCategory(models.Model):
    '''Sub-category
    '''
    category = models.ForeignKey(Category, verbose_name=_("Category"))
    name = models.CharField(_("Name"), max_length=150)
    available = models.BooleanField(_("Available"))
    areas = models.ManyToManyField('Area', related_name='areas',
                         blank=True, null=True, db_table=u'subcategory_areas')
    icon = models.ForeignKey(Icon, verbose_name=_("Icon"))
    color_theme = models.ForeignKey(ColorTheme, verbose_name=_("Color theme"),
                                    blank=True, null=True)
    order = models.IntegerField(_("Order"))
    TYPE = (('M', _('Marker')),
            ('R', _('Route')),
            ('B', _('Both')),)
    item_type = models.CharField(_("Item type"), max_length=1, choices=TYPE)
    def __unicode__(self):
        return u"%s / %s" % (self.category.name, self.name)
    class Meta:
        ordering = ["category", "order"]
        verbose_name = _("Subcategory")

    @classmethod
    def getAvailable(cls, item_types=None, area_name=None):
        '''Get list of tuples with first the category and second the associated
        subcategories
        '''
        sub_categories = {}
        subcategories = None
        subcategories = cls.objects.filter(category__available=True)
        if not item_types:
            subcategories = subcategories.filter(available=True)
        else:
            subcategories = subcategories.filter(item_type__in=item_types)
        if area_name:
            area = Area.objects.get(urn=area_name)
            if area.subcategories.count():
                sub_ids = [sub.id for sub in area.subcategories.all()]
                subcategories = subcategories.filter(id__in=sub_ids)
        for sub_category in subcategories:
            if sub_category.category not in sub_categories:
                sub_categories[sub_category.category] = []
            if sub_category.id in settings.DEFAULT_CATEGORIES:
                sub_category.selected = True
                sub_category.category.selected = True
            sub_categories[sub_category.category].append(sub_category)
        return [(category, sub_cats) for category, sub_cats \
                                         in sub_categories.items()]

class Marker(models.Model):
    '''Marker for a POI
    '''
    name = models.CharField(_("Name"), max_length=150)
    subcategory = models.ForeignKey(SubCategory, verbose_name=_("Subcategory"))
    point = PointField(_("Localisation"))
    picture = models.ImageField(_("Image"), upload_to='upload', blank=True,
                                height_field='height', width_field='width')
    STATUS = (('S', _('Submited')),
              ('A', _('Available')),
              ('D', _('Disabled')),)
    STATUS_DCT = {}
    for key, label in STATUS:
        STATUS_DCT[key] = label
    status = models.CharField(_("Status"), max_length=1, choices=STATUS)
    objects = models.GeoManager()

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ('subcategory__category', 'subcategory', 'status', 'name')
        verbose_name = _("Point of interest")

    def getLatitude(self):
        '''Return the latitude
        '''
        return self.point.y

    def getLongitude(self):
        '''Return the longitude
        '''
        return self.point.x

    def getProperty(self, propertymodel, safe=None):
        """Get the property of an associated property model.
        If safe set to True, verify if the property is available
        """
        if safe and not propertymodel.available:
            return
        try:
            property = Property.objects.get(propertymodel=propertymodel,
                                            marker=self)
        except Property.DoesNotExist:
            return
        return property

    def getProperties(self):
        """Get all the property availables
        """
        properties = []
        for pm in PropertyModel.objects.filter(available=True):
            property = self.getProperty(pm)
            if property:
                properties.append(property)
        return properties

    def getGeoJSON(self):
        '''Return a GeoJSON string
        '''
        return """{"type":"Feature", "geometry":%(geometry)s, \
"properties":{"pk": %(id)d, "name": "%(name)s", \
"icon_path":"%(icon_path)s", "icon_width":%(icon_width)d, \
"icon_height":%(icon_height)d}}""" % {'id':self.id, 'name':self.name,
'icon_path':self.subcategory.icon.image, 'geometry':self.point.geojson,
'icon_width':self.subcategory.icon.image.width,
'icon_height':self.subcategory.icon.image.height,}

class Route(models.Model):
    '''Route on the map
    '''
    name = models.CharField(_("Name"), max_length=150)
    subcategory = models.ForeignKey(SubCategory, verbose_name=_("Subcategory"))
    route = RouteField(_("Route"))
    picture = models.ImageField(_("Image"), upload_to='upload', blank=True,
                                height_field='height', width_field='width')
    STATUS = (('S', _('Submited')),
              ('A', _('Available')),
              ('D', _('Disabled')),)
    STATUS_DCT = {}
    for key, label in STATUS:
        STATUS_DCT[key] = label
    status = models.CharField(_("Status"), max_length=1, choices=STATUS)
    objects = models.GeoManager()

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ('subcategory__category', 'subcategory', 'status', 'name')
        verbose_name = _("Route")

    def getProperty(self, propertymodel, safe=None):
        """Get the property of an associated property model.
        If safe set to True, verify if the property is available
        """
        if safe and not propertymodel.available:
            return
        try:
            property = Property.objects.get(propertymodel=propertymodel,
                                            marker=self)
        except Property.DoesNotExist:
            return
        return property

    def getProperties(self):
        """Get all the property availables
        """
        properties = []
        for pm in PropertyModel.objects.filter(available=True):
            property = self.getProperty(pm)
            if property:
                properties.append(property)
        return properties

    def getGeoJSON(self, color="#000"):
        '''Return a GeoJSON string
        '''
        if '#' not in color:
            color = '#' + color
        return """{"type":"Feature", "geometry":%(geometry)s, \
"properties":{"pk": %(id)d, "name": "%(name)s", \
"color":"%(color)s"}}""" % {'id':self.id, 'name':self.name,
'color':color, 'geometry':self.route.geojson,}

class SimplePoint:
    """
    Point in the map (not in the database)
    """
    def __init__(self, x, y):
        self.x, self.y = x, y

class SimpleArea:
    """
    Rectangular area of a map (not in the database)
    """
    def __init__(self, area):
        """
        Defining upper left corner ans lower right corner from a tuple
        """
        assert len(area) == 4
        x1, y1, x2, y2 = area
        self.upper_left_corner = SimplePoint(x1, y1)
        self.lower_right_corner = SimplePoint(x2, y2)

    def isIn(self, area):
        """
        Verify if the current area is in the designated area
        """
        if self.upper_left_corner.x >= area.upper_left_corner.x and \
           self.upper_left_corner.y <= area.upper_left_corner.x and \
           self.lower_right_corner.x <= area.lower_right_corner.x and \
           self.lower_right_corner.y >= area.lower_right_corner.y:
            return True
        return False

    def getCategories(self, status='A'):
        """
        Get categories for this area
        """
        equal_status = ''
        if len(status) == 1:
            equal_status = "='%s'" % status[0]
        else:
            equal_status = " in ('%s')" % "','".join(status)
        area = "ST_GeometryFromText('POLYGON((%f %f,%f %f,%f %f,%f %f, %f %f))'\
, %d)" % (self.upper_left_corner.x, self.upper_left_corner.y,
          self.lower_right_corner.x, self.upper_left_corner.y,
          self.lower_right_corner.x, self.lower_right_corner.y,
          self.upper_left_corner.x, self.lower_right_corner.y,
          self.upper_left_corner.x, self.upper_left_corner.y,
          settings.EPSG_DISPLAY_PROJECTION
          )
        sql_main = '''select subcat.id as id, subcat.category_id as category_id,
subcat.name as name, subcat.available as available, subcat.icon_id as icon_id,
subcat.color_theme_id as color_theme_id, subcat.order as order,
subcat.item_type as item_type from main_subcategory subcat'''
        sql = sql_main + '''
inner join main_marker mark on mark.subcategory_id=subcat.id and mark.status%s
and ST_Contains(%s, mark.point) where subcat.available = TRUE''' % (
                                                             equal_status, area)
        # django > 1.1
        #subcats = SubCategory.objects.raw(sql)
        from django.db import connection, transaction
        cursor = connection.cursor()
        cursor.execute(sql, [])
        subcats = set()
        for r in cursor.fetchall():
            subcats.add(SubCategory.objects.get(id=r[0]))
        sql = sql_main + '''
inner join main_route rt on rt.subcategory_id=subcat.id and rt.status%s
and (ST_Intersects(%s, rt.route) or ST_Contains(%s, rt.route))
where subcat.available = TRUE''' % (equal_status, area, area)
        # django > 1.1
        #subcats += SubCategory.objects.raw(sql)
        cursor.execute(sql, [])
        for r in cursor.fetchall():
            subcats.add(SubCategory.objects.get(id=r[0]))
        return subcats

class Area(models.Model, SimpleArea):
    """Rectangular area of the map
    """
    name = models.CharField(_("Name"), max_length=150)
    urn = models.SlugField(_("Area urn"), max_length=50, blank=True,
                           unique=True)
    subcategories = ManyToManyField_NoSyncdb(SubCategory,
                related_name='subcategories', blank=True, null=True,
                db_table=u'subcategory_areas')
    order = models.IntegerField(_("Order"))
    available = models.BooleanField(_("Available"))
    upper_left_corner = models.PointField(_("Upper left corner"),
                                          default='POINT(0 0)')
    lower_right_corner = models.PointField(_("Lower right corner"),
                                           default='POINT(0 0)')
    objects = models.GeoManager()

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ('order', 'name')
        verbose_name = _("Area")

    @classmethod
    def getAvailable(cls):
        '''Get available areas
        '''
        return cls.objects.filter(available=True)

class PropertyModel(models.Model):
    '''Model for a property
    '''
    name = models.CharField(_("Name"), max_length=150)
    order = models.IntegerField(_("Order"))
    available = models.BooleanField(_("Available"))
    TYPE = (('T', _('Text')),
            ('L', _('Long text')),
            ('P', _('Password')))
    TYPE_WIDGET = {'T':'forms.TextInput',
                   'L':'TextareaWidget',
                   'P':'forms.PasswordInput'}
    type = models.CharField(_("Type"), max_length=1, choices=TYPE)
    def __unicode__(self):
        return self.name
    class Meta:
        ordering = ('order',)
        verbose_name = _("Property model")

    def getNamedId(self):
        '''Get the name used as named id (easily sortable)
        '''
        return 'property_%d_%d' % (self.order, self.id)

class Property(models.Model):
    '''Property for a POI
    '''
    marker = models.ForeignKey(Marker, verbose_name=_("Point of interest"))
    propertymodel = models.ForeignKey(PropertyModel,
                                      verbose_name=_("Property model"))
    value = models.TextField(_("Value"))
    def __unicode__(self):
        return "%s : %s" % (str(self.propertymodel), self.value)
    class Meta:
        verbose_name = _("Property")