diff options
Diffstat (limited to 'chimere/models.py')
-rw-r--r-- | chimere/models.py | 22 |
1 files changed, 14 insertions, 8 deletions
diff --git a/chimere/models.py b/chimere/models.py index 5fe213b..8b508a9 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -179,9 +179,6 @@ class SubCategory(models.Model): # if there some restrictions with categories limit them if area.subcategories.count(): sub_ids = [sub.id for sub in area.subcategories.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()] subcategories = subcategories.filter(id__in=sub_ids) for sub_category in subcategories: if sub_category.category not in sub_categories: @@ -715,10 +712,21 @@ class SimpleArea: return True return False - def getCategories(self, status='A', filter_available=True): + def getCategories(self, status='A', filter_available=True, area_name=None): """ Get categories for this area """ + wheres = [] + if area_name: + subcategory_pks = [] + for cat, subcats in SubCategory.getAvailable(area_name=area_name): + for subcat in subcats: + subcategory_pks.append(unicode(subcat.pk)) + if filter_available: + wheres += ['subcat.available = TRUE', 'cat.available = TRUE'] + wheres += ['subcat.id in (%s)' % ",".join(subcategory_pks)] + where = " where " + " and ".join(wheres) if wheres else "" + equal_status = '' if len(status) == 1: equal_status = "='%s'" % status[0] @@ -747,8 +755,7 @@ class SimpleArea: sql += ''' inner join chimere_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' + sql += where subcats = set(SubCategory.objects.raw(sql)) sql = sql_main + ''' inner join chimere_route rt on (ST_Intersects(%s, rt.route) or @@ -759,8 +766,7 @@ class SimpleArea: sql += ''' inner join chimere_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' + sql += where subcats.union(SubCategory.objects.raw(sql)) return subcats |