summaryrefslogtreecommitdiff
path: root/chimere/main/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'chimere/main/models.py')
-rw-r--r--chimere/main/models.py79
1 files changed, 38 insertions, 41 deletions
diff --git a/chimere/main/models.py b/chimere/main/models.py
index f633b3f..564552f 100644
--- a/chimere/main/models.py
+++ b/chimere/main/models.py
@@ -27,8 +27,7 @@ from django.contrib.gis.gdal import SpatialReference
from django.contrib import admin
from chimere import settings
-from chimere.main.widgets import PointField, RouteField, \
- ManyToManyField_NoSyncdb
+from chimere.main.widgets import PointField, RouteField, SelectMultipleField
class News(models.Model):
"""News of the site
@@ -42,6 +41,7 @@ class News(models.Model):
return self.title
class Meta:
verbose_name = _("News")
+ verbose_name_plural = _("News")
class TinyUrl(models.Model):
"""Tinyfied version of permalink parameters
@@ -122,6 +122,8 @@ class Icon(models.Model):
name = models.CharField(_("Name"), max_length=150)
image = models.ImageField(_("Image"), upload_to='icons',
height_field='height', width_field='width')
+ height = models.IntegerField(_("Height"))
+ width = models.IntegerField(_("Width"))
def __unicode__(self):
return self.name
class Meta:
@@ -133,8 +135,7 @@ class SubCategory(models.Model):
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)
+ areas = SelectMultipleField('Area', related_name='areas', blank=True)
icon = models.ForeignKey(Icon, verbose_name=_("Icon"))
color_theme = models.ForeignKey(ColorTheme, verbose_name=_("Color theme"),
blank=True, null=True)
@@ -163,8 +164,8 @@ class SubCategory(models.Model):
if area_name:
area = Area.objects.get(urn=area_name)
# if there some restrictions with categories limit them
- if area.subcategories.count():
- sub_ids = [sub.id for sub in area.subcategories.all()]
+ if area.subcategory_set.count():
+ sub_ids = [sub.id for sub in area.subcategory_set.all()]
# if no area is defined for a category don't filter it
sub_ids += [sub.id for sub in subcategories
if not sub.areas.count()]
@@ -183,10 +184,12 @@ 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"))
+ categories = SelectMultipleField(SubCategory)
+ point = PointField(_("Localisation"), srid=settings.EPSG_DISPLAY_PROJECTION)
picture = models.ImageField(_("Image"), upload_to='upload', blank=True,
- height_field='height', width_field='width')
+ null=True, height_field='height', width_field='width')
+ height = models.IntegerField(_("Height"), blank=True, null=True)
+ width = models.IntegerField(_("Width"), blank=True, null=True)
STATUS = (('S', _('Submited')),
('A', _('Available')),
('D', _('Disabled')),)
@@ -200,7 +203,7 @@ class Marker(models.Model):
return self.name
class Meta:
- ordering = ('subcategory__category', 'subcategory', 'status', 'name')
+ ordering = ('status', 'name')
verbose_name = _("Point of interest")
def getLatitude(self):
@@ -239,22 +242,26 @@ class Marker(models.Model):
def getGeoJSON(self):
'''Return a GeoJSON string
'''
- return """{"type":"Feature", "geometry":%(geometry)s, \
+ jsons = []
+ for cat in self.categories.all():
+ jsons.append("""{"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,}
+'icon_path':cat.icon.image, 'geometry':self.point.geojson,
+'icon_width':cat.icon.image.width, 'icon_height':cat.icon.image.height,})
+ return ",".join(jsons)
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"))
+ categories = SelectMultipleField(SubCategory)
+ route = RouteField(_("Route"), srid=settings.EPSG_DISPLAY_PROJECTION)
picture = models.ImageField(_("Image"), upload_to='upload', blank=True,
- height_field='height', width_field='width')
+ null=True, height_field='height', width_field='width')
+ height = models.IntegerField(_("Height"), blank=True, null=True)
+ width = models.IntegerField(_("Width"), blank=True, null=True)
STATUS = (('S', _('Submited')),
('A', _('Available')),
('D', _('Disabled')),)
@@ -268,7 +275,7 @@ class Route(models.Model):
return self.name
class Meta:
- ordering = ('subcategory__category', 'subcategory', 'status', 'name')
+ ordering = ('status', 'name')
verbose_name = _("Route")
def getProperty(self, propertymodel, safe=None):
@@ -358,33 +365,26 @@ subcat.color_theme_id as color_theme_id, subcat.order as order,
subcat.item_type as item_type from main_subcategory subcat
inner join main_category cat on cat.id=subcat.category_id'''
sql = sql_main + '''
-inner join main_marker mark on mark.subcategory_id=subcat.id
- and ST_Contains(%s, mark.point)''' % area
+inner join main_marker mark on ST_Contains(%s, mark.point)''' % area
if equal_status:
sql += ' and mark.status' + equal_status
+ sql += '''
+inner join main_marker_categories mc on mc.subcategory_id=subcat.id and
+mc.marker_id=mark.id'''
if filter_available:
sql += ' where subcat.available = TRUE and cat.available = TRUE'
- print sql
- # 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]))
+ subcats = set(SubCategory.objects.raw(sql))
sql = sql_main + '''
-inner join main_route rt on rt.subcategory_id=subcat.id
-and (ST_Intersects(%s, rt.route) or ST_Contains(%s, rt.route))''' % (area, area)
+inner join main_route rt on (ST_Intersects(%s, rt.route) or
+ST_Contains(%s, rt.route))''' % (area, area)
if equal_status:
sql += ' and rt.status' + equal_status
+ sql += '''
+inner join main_route_categories rc on rc.subcategory_id=subcat.id and
+rc.route_id=rt.id'''
if filter_available:
sql += ' where subcat.available = TRUE and cat.available = TRUE'
- # django > 1.1
- #subcats += SubCategory.objects.raw(sql)
- cursor.execute(sql, [])
- for r in cursor.fetchall():
- subcats.add(SubCategory.objects.get(id=r[0]))
+ subcats.union(SubCategory.objects.raw(sql))
return subcats
class Area(models.Model, SimpleArea):
@@ -393,15 +393,12 @@ class Area(models.Model, SimpleArea):
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)')
+ default='POINT(0 0)', srid=settings.EPSG_DISPLAY_PROJECTION)
lower_right_corner = models.PointField(_("Lower right corner"),
- default='POINT(0 0)')
+ default='POINT(0 0)', srid=settings.EPSG_DISPLAY_PROJECTION)
objects = models.GeoManager()
def __unicode__(self):