diff options
Diffstat (limited to 'chimere/models.py')
| -rw-r--r-- | chimere/models.py | 45 | 
1 files changed, 42 insertions, 3 deletions
| diff --git a/chimere/models.py b/chimere/models.py index 545d7e0..7db530a 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -171,7 +171,8 @@ class Color(models.Model):      """      code = models.CharField(_(u"Code"), max_length=6)      order = models.IntegerField(_(u"Order")) -    color_theme = models.ForeignKey(ColorTheme, verbose_name=_(u"Color theme")) +    color_theme = models.ForeignKey(ColorTheme, verbose_name=_(u"Color theme"), +                                    related_name='colors')      def __unicode__(self):          return self.code      class Meta: @@ -213,6 +214,8 @@ class SubCategory(models.Model):      available = models.BooleanField(_(u"Available"), default=True)      submission = models.BooleanField(_(u"Available for submission"),                                       default=True) +    weighted = models.BooleanField(_(u"Has an associated quantity"), +                                   default=False)      TYPE = (('M', _(u'Marker')),              ('R', _(u'Route')),              ('B', _(u'Both')),) @@ -465,6 +468,8 @@ class Marker(GeographicItem):                                            null=True) # used by feeds      route = models.ForeignKey(u"Route", blank=True, null=True,                                related_name='associated_marker') +    weight = models.IntegerField(_(u"Quantity"), blank=True, null=True, +                                 default=0)      description = models.TextField(_(u"Description"), blank=True, null=True)      is_front_page = models.NullBooleanField(_(u"Is front page"), blank=True,                                              null=True) @@ -534,6 +539,10 @@ class Marker(GeographicItem):      def geom_attr(self):          return 'point' +    @property +    def has_weight(self): +        return bool(self.categories.filter(weighted=True).count()) +      class Meta:          ordering = ('status', 'name')          verbose_name = _(u"Point of interest") @@ -615,6 +624,19 @@ class Marker(GeographicItem):                  val = values[unicode(propertymodel.id)]              self.setProperty(propertymodel, val) +    def _getItems(self, base_dct={"properties":{}}): +        '''Return a dict representation for json +        ''' +        item = base_dct +        item["geometry"] = {"type": "Point", +                            "coordinates": [ self.point.x, self.point.y ] +                            } +        item["properties"]['pk'] = self.pk +        item["properties"]['name'] = self.name +        if self.weight: +            item["properties"]['weight'] = self.weight +        return item +      def getGeoJSON(self, categories_id=[]):          '''Return a GeoJSON string          ''' @@ -629,14 +651,24 @@ class Marker(GeographicItem):                                         if cat.hover_icon else '',                       'icon_width':cat.icon.image.width,                       'icon_height':cat.icon.image.height, -                     'category_name':json.dumps(cat.name)} +                     'category_name':json.dumps(cat.name),} +            items['weight'] = '' +            if cat.weighted: +                if not self.weight: +                    continue +                items['weight'] = u', "weight":%d' % self.weight +                if cat.color_theme and cat.color_theme.colors.count(): +                    items['weight'] += u', "colors":["#%s"]' % '", "#'.join( +                        [color.code for color in cat.color_theme.colors.\ +                                                     order_by('order').all()])              jsons.append(u'{"type":"Feature", "geometry":%(geometry)s, '\                  u'"properties":{"pk": %(id)d, "name": %(name)s, '\                  u'"icon_path":"%(icon_path)s", '\                  u'"icon_hover_path":"%(icon_hover_path)s", '\                  u'"icon_width":%(icon_width)d, '\                  u'"icon_height":%(icon_height)d, '\ -                u'"category_name":%(category_name)s}}' % items) +                u'"category_name":%(category_name)s'\ +                u'%(weight)s}}' % items)          return ",".join(jsons)      @property @@ -1098,6 +1130,13 @@ class Route(GeographicItem):                  properties.append(property)          return properties +    def _getItems(self, dct={'properties':{}}): +        dct['geometry'] = { "type": "LineString", +                            "coordinates": [[point.x, point.y] +                                            for point in self.route]} +        dct['properties'].update({'id':self.id, 'name':self.name}) +        return dct +      def getGeoJSON(self, color="#000"):          '''Return a GeoJSON string          ''' | 
